Using cron vs. system managed service for frequent tasks

0

If some task should run in an frequent but not neccessary precise manner, what would the benefits of using cron vs. just using a service (eg. managed by upstart) with a loop script like

#!/bin/sh
while true
do
    do_task
    sleep 3600
done

?

With today's upstart we can select the user with the setuid command, and upstart can be configured to restart the script if it dies, stop it if wanted etc.

Is there any real benefit of messing with cron either in this situation?

dronus

Posted 2014-02-19T23:41:59.000

Reputation: 1 482

Answers

1

Running from Cron is "neater". For a start it will take less resources - your script will consume extra resources (memory for the shell, timer and extra Processes and file-descriptors spring to mind). While practically machines are insanely powerful, its just less elegant.

Cron also has a neat way of handling stdout -> email or what-have-you, which could be useful for debugging.

If you use Cron you don't need to stuff around with different privs, just tell cron which user to use.

Of-course, using upstart does have some advantages - for example you might want a degree of randomness in the waits or a processing schedule that Cron does not like. There are times when a solution like this is better then Cron - but I think they are corner cases.

If you are going to want to start and stop the process and have user control, for example, Upstart may be a better way of going.

davidgo

Posted 2014-02-19T23:41:59.000

Reputation: 49 152

0

cron is used when you want a process to be executed at some precise absolute time (ie. 3:15 AM everyday or every hour at X:05 or so) while your script is relative to the time you start it. In your example will run every hour from the moment you launch it, which should correspond with the boot sequence. So if your PC boots at 1:05 you will have your job done at every X:05 while if it boots at 3:36 you will have your jobs done at X:36.

If you just want it to be done approximately once an hour then it doesn't really change, but of course it may run with anywhere between the first and the last minute of an hour depending on the boot. Actually if your do_task is something long that takes a lot of time it may even end up running less often than once per hour (since the sleep is calculated at the end of the task, so if do_task takes 30 minutes then do_task will start every 1:30 hours in your case)

On the other side using cron if your system is not always up you may easily lose some events (since they are done just when the condition is exact). In this case anacron is more suggested (since it just looks the time from last execution). Another thing is that cron will anyway execute the job even if the previous istance didn't finish yet which may in some cases be a good or bad thing while your script will not.

So shortly said: if you need a definite and know time cron is usually better, if you just need a periodical task without specific timings then you can go for upstart.

fede.evol

Posted 2014-02-19T23:41:59.000

Reputation: 1 718