Search code examples
bashloopssleep

how to execute a bash script every 20 seconds? , good use sleep and a loop?


I have a script running now every minute within cron, and I would like to make it run every 20 seconds

On the other hand, server manager (the script is in a remote machine managed by others) is complaining about crons running oftener than 5 ~ 10 minutes

How can I program this?

Should I use a while true loop and a sleep 20 for each loop?


Solution

  • I think your idea of looping and sleeping is the right approach.

    You can launch the script in the background each time your system starts (from cron) with an entry that uses @reboot:

    @reboot /path/to/script argument1 argument2
    

    Be sure to write output to file(s), not STDOUT.

    You can test your script without restarting the system by using nohup:

    $ nohup /path/to/script argument1 argument2 &