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>" <to@mymailaddres.tld> -- -f <from@thisaddress.tld>
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.


Us systemd for that! You need .service and .timer units
script.service:
[Service]
Type=oneshot
ExecStart=/path/to/script
script.timer:
[Timer]
OnUnitActiveSec=10s
OnBootSec=10s
yes. systemd already works like a charm on RHEL.
Hey! That’s a clever T&T, thx!
RHEL? Oh poor you, I’m already thinking in moderm distros only
@Tomasz: ha! But thx for the tip anyway, I didn’t know about the systemD trick yet. In 5years it might come in handy, haha
1) Why not just turn the child process into a daemon and have it wake up every ten seconds (or better: when there is something for it to do)?
2) Your ‘runner’ script will send you email every ten seconds if anything goes wrong with the script you want to run. Are you sure you want this?
1) because that would be more work for me to do. I have been thinking about how I could do this, but at the moment it’s better not to do this, and wait till I pretty much rewrite the whole child process.
even if that means I have a 1000e-mails after a while
2) yes, that’s what I want. Actually, it’s pretty much impossible (I’m not saying it is) what there will be something going wrong every time due to how the child process behaves after a previous one has crashed. But since I couldn’t come up with a decent monitoring plan right out of the top of my head, I wanted the ‘runner’ to send me an e-mail on a crash. This way I get notified a bit faster
One tip for you: flock !
You will thank me once you have read the manual page