How To Run a Cron Job Every 30 Seconds in Linux

Brief: The cron job scheduler does not support scheduling jobs to run at an interval of seconds. In this article, we will show you a simple trick to help you run a cron job every 30 seconds or x seconds in Linux.

Are you new to the cron job scheduler and want to run a job every 30 seconds? Unfortunately, cron does not allow for it. You can not schedule a cron job to run every x second. Cron only supports a time interval of at least 60 seconds (i.e 1 minute). To run a cron job every 30 seconds, you need to employ the trick we have explained below.

In this guide, we will also cover many other examples to run a job or command, or script every x second. But let’s start by covering how to run a cron job every 30 seconds in Linux.

Run Cron Job Every 30 Seconds in Linux

To achieve the above task, create two entries in the crontab. The first job will run the date command after every minute (60 seconds), then the second entry makes use of the sleep command to delay for a specified amount of time (30 seconds in this case) and invoke the date command again.

You need to add the following entries in the crontab (cron table), and open it for editing using the following crontab command (the -e flag enables editing):

# crontab -e

Add the following cron entries to the file.

* * * * * date>> /tmp/date.log
* * * * * sleep 30; date>> /tmp/date.log 
Add Cron Job in Linux
Add Cron Job in Linux

Now if you check the contents of the /tmp/date.log file, you should see that the date command is run every 30 seconds. We can use the cat command to view the file and check the time column to confirm, as follows:

$ cat /tmp/date.log
Check Cron Runs Every 30 Seconds
Check Cron Runs Every 30 Seconds

You can also watch the file getting updated in real-time. To do that, use the tail command with the -f flag.

$ tail -f /tmp/date.log
View File Contents in Real Time
View File Contents in Real Time

Run Cron Job Every 10 Seconds in Linux

Let’s look at more examples. This one shows how to run a cron job every 10 seconds. The trick is to simply play around with the sleep command number of seconds:

* * * * * date>> /tmp/date.log
* * * * * sleep 10; date>> /tmp/date.log
* * * * * sleep 20; date>> /tmp/date.log
* * * * * sleep 30; date>> /tmp/date.log
* * * * * sleep 40; date>> /tmp/date.log
* * * * * sleep 50; date>> /tmp/date.log

Once again if we watch the /tmp/date.log file, it should be updated every 10 seconds based on the above crontab entries:

$ tail -f  /tmp/date.log

Here is another example of executing the date command after every 15 seconds:

* * * * * date>> /tmp/date.log
* * * * * sleep 15; date>> /tmp/date.log
* * * * * sleep 30; date>> /tmp/date.log
* * * * * sleep 45; date>> /tmp/date.log

Finally, to run a cron job every 20 seconds, you can have something like this:

* * * * * date>> /tmp/date.log
* * * * * sleep 20; date>> /tmp/date.log
* * * * * sleep 40; date>> /tmp/date.log

Also, here are more articles for you to learn job scheduling using cron:

Now you know it! We have shown you different examples to run a cron job every x second in Linux. Read the cron man pages (by running man cron and man crontab commands) for more information.

If you know any useful cron command tips or tricks, please share them in the comments below.

Tutorial Feedback...
Was this article helpful? If you don't find this article helpful or found some outdated info, issue or a typo, do post your valuable feedback or suggestions in the comments to help improve this article...

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

6 thoughts on “How To Run a Cron Job Every 30 Seconds in Linux”

  1. Hi,

    In my opinion, if a cronjob is needed to run so often, it is either time to daemonize it using systemd, and/or implement an event-driven task (e.g. use inotifywait to watch for filesystem changes).

    Either way, thank you for your work on this blog.

    Reply
    • @Biapy

      I will give systemd a try to implement a solution to the above problem.

      Many thanks for sharing your thoughts about this post.

      Reply
    • Hi,

      This method can be used but is really not recommended. The trick is to use a number of minutes that is a direct multiple of the number of seconds wanted. E.g. for the 90s, setup the cron task to be launched every 3 minutes (e.g. 90s * 2):

      */3 * * * * date>> /tmp/date.log
      */3 * * * * sleep 90; date>> /tmp/date.log
      
      Reply
    • @dragonmouth and @Biapy

      From the man page of the sleep command, the sleep command pauses for x number of seconds and the suffix is s for seconds, the default or m for minutes.

      To schedule a job for 77 seconds, simply add these entries in the crontab:

      * * * * * date>> /tmp/date.log
      * * * * * sleep 77; date>> /tmp/date.log
      

      For 90 seconds, use this:

      * * * * * date>> /tmp/date.log
      * * * * * sleep 90; date>> /tmp/date.log
      
      Reply
      • That doesn’t work. The first example runs a job at second 0 and another at second 17 of every minute. The second run is at second 0 and 30.

        To really run a job every 77 seconds, you need to run it:

        – at second 0 of the first minute.
        – at the second 17 of the second minute.
        – at second 34 of the third minute.
        – at second 51 of the fourth minute.
        – at second 8 of the sixth minute.

        and so on. The cycle repeats every 77 minutes.

        Reply

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.