0 votes
133 views
in Linux by
How can I set up a cron job to run every 30 seconds in Linux?

2 Answers

0 votes
by
Cron is designed to run jobs on a schedule specified in minutes. While it is technically possible to run a job every 30 seconds by running a command similar to the ones I provided earlier, it would be highly inadvisable to do so because it can put a significant load on the system. Cron will run the command repeatedly every 30 seconds causing high system resource usage and can cause stability issues.

A better approach would be to use a script that checks the current time and sleeps until the next 30 second interval, and then runs the desired command. This way, the script will only run every 30 seconds, but it will not be consuming resources when it is not running.

It is important to consider the impact of running a job every 30 seconds on your system, and whether or not it is necessary.
0 votes
by

Here is an example of how to set up a cron job to run every 30 seconds:

  1. Create a shell script called job.sh with the command you want to run:  

     #!/bin/bash
    
    command-to-run
  2. Make the script executable by running the command chmod +x /path/to/job.sh
     
  3. Edit your crontab by running the command crontab -e
     
  4. Add the following line to your crontab file: 

      * * * * * /path/to/job.sh  ( sleep 30; /path/to/job.sh )            

This tells cron to run the job.sh script every minute, and then wait for 30 seconds before running it again.

Please be aware that running a cron job every 30 seconds can put a significant load on your system, you may want to consider running a script that checks if it needs to run or not, instead of running it every 30 seconds.

Make sure you test the commands before saving the crontab file.

...