How to automatically flush cron on VPS running Ubuntu?

One of my clients has a Magento website hosted on a VPS running Ubuntu.

They were complaining that they weren't getting any copies of sales emails - and after investigating lots of other things, I found that the cron job on the server was stuck about two months ago.

I killed the cron job, which caused Magento to re-queue the latest commands, and hey presto - 7 sales emails came though in the next 30 seconds!

Is there a way that I can set the cron service to automatically restart at 3am each morning - or something like that? It seems this would stop this problem from ever being an issue again...

1 Answer

Restarting cron (systemctl restart cron.service) won't help: the hanging (or otherwise still running) processes will be re-parented and keep running (or keep hanging, for that matter). You need to stop the queueing process itself.

One simple way would be to start your job via the timeout command. Let's say you run a program called magento_que every 5 minutes and want to run it at most 3 minutes and 30 seconds:

*/5 * * * * /usr/bin/timeout 3.5m /path/to/magento_que

After 3.5 minutes the magento_que will be sent a SIGTERM (similar to CTRL-C). If it doesn't respond to that you can define another signal or combine with --kill-after=DURATION:

*/5 * * * * /usr/bin/timeout --kill-after=10s 3.5m /path/to/magento_que

Now magento_que will be sent a SIGTERM after 3.5 minutes and if it refuses to stop then a SIGKILL will be sent 10 seconds later (which it cannot intercept).

However, this is just a workaround and I should say I'd prefer to find the real reason why the program hangs.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like