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.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

6 Comments

Leave a Reply
  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...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.