Linux Administrator

How to Run Cron in Every 5 Seconds

Cron’s granularity is in minutes and was not designed to wake up every x seconds to run something. Run your repeating task within a loop. Crontab job can be used to schedule a job in minutes/hours/days, but not in seconds you need a alternative way.

In this article I will provide you alternative way to run cron every 5 seconds.

Run Cron in Every 5 Seconds

Cron job cannot be used to schedule a job in seconds interval. i.e You cannot schedule a cron job to run every 5 seconds. The alternative is to write a shell script that uses sleep 5 command in it.

# cd /home/sagar/
# vim five_seconds.sh
#!/bin/bash
while true
do
 /home/sagar/kill_long_query.sh
 sleep 5
done

Make this script executable typing below command:

# chmod +x /home/sagar/five_seconds.sh

Now, execute this shell script in the background using nohup as shown below. This will keep executing the script even after you logout from your session. This will execute your backup.sh shell script every 5 seconds.

# nohup ./five_seconds.sh &

You can also set cron job to execute this script like below:

* * * * * /home/sagar/five_seconds.sh > /dev/null

Thanks:)

Thank you! for visiting LookLinux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at Look Linux Submit Article. Stay connected to Facebook.

About the author

mm

Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

1 Comment

  • Version for cron

    # cd /home/sagar/
    # vim five_seconds.sh
    #!/bin/bash
    for i in `seq 1 12`
    do
    /home/sagar/kill_long_query.sh
    sleep 5
    done

Leave a Comment