For a client I needed to run a specific script every 10seconds. 24/24 and 7/7. A task I would normally perform with crons. But sadly enough, a cron can only be started once per minute. So I wrote a ‘runner’ script. A script that starts the other script
Things this script does:
- runs the child script 10s after the end of the previous try
- sends an e-mail if stderr is returned
- if the runner would stop, it will auto restart (due to a cronjob)
- only one instance will run at the same time.
#!/bin/bash sleep=10 if ! ps -p $(cat /path/to/runner.pid); then echo $$ > /path/to/runner.pid while : do error=$(/path/to/script 2>&1 >/dev/null) if [ -n "$error" ]; then echo "$error" | mail -s "<subject>" <firstname.lastname@example.org> -- -f <email@example.com> fi sleep $sleep done fi
Of course you can choose the time to restart the script by editing the ‘sleep’ value.
In the cronfile I added the following line:
*/30 * * * * /path/to/runner 2>&1 >/dev/null
This way the runner script can only be ‘offline’ for 30min! Which is still acceptable.