How to Run a Cron Job Every 30 Seconds in Linux

How to Run a Cron Job Every 30 Seconds in Linux

Are you looking for ways to learn how to run a cron job every 30 seconds in Linux OS? Here’s everything you need to know.

By default, the cron job scheduler doesn’t support the scheduling of jobs at the level of seconds. Cronjob allows specifying the scheduling interval to be a minimum of 1 min. However, a trick can be employed to run a cron job for your time duration in seconds. 

This article will discuss two approaches to scheduling a cron job every 30 seconds in Linux OS.

Introduction to crontab

The crontab is used to schedule a job, such as a script running at regular intervals in Linux. It can be used by the system administrators for various purposes, such as automating backup tasks, directory cleaning, notifications, etc. The cron jobs are run in the background and look for /etc/crontab. 

In addition, other relevant directories for cron jobs are /etc/cron.*/ and /var/spool/cron/.

Note: The cron files should never be edited directly. Also, note that every user has a unique crontab file and data.

Syntax of a crontab file

The general syntax of a crontab entry is as follows:

A B C D E USERNAME /path/to/command

Here A is for minutes, B for hours, C for days, D for months, and E for days of the week. Username is the user’s name, and /path/to/command is the complete command along with the path to be scheduled. In addition, you can use operator symbols as follows:

  • (,) specifies a list of values.
  • – specifies a range of values.
  • / determines a step value.

As an example, the following specifies the script to be run every second day of the month at 4:30 pm:

30 16 2 * * /path/to/

Now we will discuss running a cron job every 30 seconds in Linux. We will discuss two approaches.

How to run a cron job every 30 seconds in Linux

What we will do is create two entries in crontab. First, we will create a job running the date command after every minute (i.e., 60 seconds). The second entry will use the sleep command to delay for a specific period (30 sec) and then invoke the date command

The net effect is as the date command runs after every 30 seconds. Follow the steps below to run a cron job every 30 seconds.

Open crontab for entry

Open your terminal and type the following command to open the crontab for editing:

# crontab –e

Add the entries in the crontab

Now, add the following entries in crontab:

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

Verify the cron job is scheduling the job every 30 seconds

Here, we are outputting the date command in /tmp/date.log file. So, if you check the content of the date.log file, you will see that the date command is run after every 30 seconds. You can check the output using the following command:

$ cat /tmp/date.log

Here we have used the cat command that outputs the content of the file. In addition, you can also add a watch to the file to see if the file is updated after 30 seconds. To do that, we will use the tail command with the –f flag as follows:

$ tail -f /tmp/date.log

The tail command prints the last few lines of the input specified. The –f option allows seeing the new data as it is appended to the file. So, it provides a live view of the data.

Running other commands/scripts instead of the date command

Instead of the date command in the above example, we can also run other scripts. The following crontab entry runs the file after every 30 seconds:

* * * * *
* * * * *  sleep 30;

Running a cron job after every 10 seconds

The same logic we used earlier can be used to schedule a job to run every 10 seconds. Here, we will run six jobs, each offset by a sleep of multiple of 10 seconds. Provide the following crontab entries:

* * * * * 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

You can watch the date.log file to see if the file is being updated after every 10 seconds as follows:

$ tail -f  /tmp/date.log

Using shell scripting and loop to run a cron job every 30 seconds

You can also use a shell script and loop to run a cron job after every 30 seconds. Use the following script for this purpose:

For  (( i=1; i <= 2; i++ ))
write Command here
sleep 3

Then, specify your script in the crontab as follows:

(* * * * *

Other non-cron-based solutions

Besides the cron job scheduling to run a cron job every 30 seconds, there are other options as well that can be tried out. You can try fcron and systemd. Some of these must be correctly installed on your computers and may require configuration. You can also use the SystemD timer unit to specify the scheduling interval at a low granularity level.

Notable cron job limitations

While cron does an amazing job as a job scheduler, it still comes with a couple of drawbacks. Here’s a list of cron’s limitations:

  • The first limitation is the interval time can’t be less than a minute. 
  • It is not possible to perform error handling if a cron job fails. 
  • By default, the cron job doesn’t do any type of logging to identify what has happened with the jobs. 
  • Cron is a system-level process. So, working with cron requires you to pull yourself from the application to the system level.

And that’s about everything to know on how to run a cron job every 30 seconds. Here, we discussed how we could schedule a job after every 30 seconds in a cron job. We started with a brief introduction to cron. Then, we covered the general syntax of a crontab entry. We even tackled the two approaches to scheduling cron jobs after every 30 seconds. 

We also discussed other non-cron-based solutions and their limitations as job schedulers. We hope this article can be used to schedule cron jobs at various granular levels to automate system-level tasks. 

If this guide helped you, please share it.

Previous Article
How to Run ChatGPT in Terminal on Linux

How to Run ChatGPT in Terminal on Linux

Next Article
How to Modify Kernel Variables Using sysctl Command on Linux

How to Modify Kernel Variables Using sysctl Command on Linux

Related Posts