Wednesday, August 3, 2016

How to keep cron from stampeding?



Say I have several cron scripts that need to run every 15 minutes. I could set them to run: */15 * * * * , but then they all run at the same time. It seems silly for the server to sit idle for several minutes and then suddenly try to execute a dozen scripts all at the same time.



Is there a way I can have one script run at minute 1, 16, 31, 46 and another at 2, 17, 32, 47?




In other words, I want each script to run every 15 minutes, but I don't care that they run specifically on the quarter hour marks.


Answer



You're making this harder than it needs to be. Put them all on the same line, separated by semicolons:



*/15 * * * * command1 ; command 2 ; command 3


It will run command1, wait for it to finish, then run command2, wait for it to finish, and so on.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...