LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-03-2008, 11:25 AM   #1
0.o
Member
 
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 208

Rep: Reputation: 35
Stagger cron jobs


I maintain a large MRTG system (~30k graphs) and would like a way to stagger cron jobs that run every five minutes. I am guessing there is some way of doing this using a range such as 2-59/5 in the minutes field, but i have been unsuccessful. The thinking behind this is that i can stagger the cron jobs...one start exactly every 5 minutes and have the next ones started on the sixth minute and run for five minutes, etc.

If anyone has any guidance, it would be greatly appreciated.

--Thanks
 
Old 07-03-2008, 04:00 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
The ugly but simple way is:
Code:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /your/command1
1,6,11,16,21,26,31,36,41,46,51,56 * * * * /your/command2
2,7,12,17,22,27,32,37,42,47,52,57 * * * * /your/command3
3,8,13,18,23,28,33,38,43,48,53,58 * * * * /your/comman4
4,9,14,19,24,29,34,39,44,49,54,59 * * * * /your/command5
I'm assuming you have some different command to run...
If not you might as well just have a script running once per minute.

An alternative is to just run a launcher script once a minute, and have it decide what to do based on the current time. i.e.
Code:
$ crcontab -l
* * * * * /path/to/launcher.sh

$ cat /path/to/launcher.sh
#!/bin/bash
case $(( $(date +%M) % 5 )) in
0) /some/command ;;
1) /some/other/command ;;
2) /yet/another/command ;;
3) /maybe/still/another/command ;;
4) /i/am/out/of/ideas ;;
esac
With a launcher script you can do all sorts of pre-running checks For example you could check for the existence of previous instances of a program before running, and only launch if there are not already multiple instances on the go (pgrep is your friend). Or you could run only if the load average is lower than some level.

Last edited by matthewg42; 07-03-2008 at 04:08 PM.
 
Old 07-03-2008, 04:04 PM   #3
Samotnik
Member
 
Registered: Jun 2006
Location: Belarus
Distribution: Debian GNU/Linux testing/unstable
Posts: 471

Rep: Reputation: 40
try */5. should work.
 
Old 07-03-2008, 05:08 PM   #4
kenoshi
Member
 
Registered: Sep 2007
Location: SF Bay Area, CA
Distribution: CentOS, SLES 10+, RHEL 3+, Debian Sarge
Posts: 159

Rep: Reputation: 32
Quote:
Originally Posted by Samotnik View Post
try */5. should work.
He's not talking about running cronjobs every 5 minutes.

He is talking about say if he has 10 cronjobs, to:

- start the first one at 0, then again every 5 minutes
- start the second one at 1, then again every 5 minutes
- start the third one at 2, then again every 5 minutes

And so forth.

matthewg24 has it nailed pretty much...just kick off a script that spawns the correct job each minute...great use of the modulo operator
 
Old 07-03-2008, 06:59 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
A much better way is to introduce a "batch scheduler." There are a great many of these for Linux that are open-source.

The essential idea is that now you are able to define jobs, in such a way that the batch scheduler can decide how to run them on the various computers in your network. You no longer have to specify exactly which computer(s) will run the job (except to the degree that you need to...), and you no longer have to try to laboriously specify exactly when. Instead, you give the batch scheduler enough information to allow it to make those decisions dynamically, in response to actual workload.

Generally speaking, a batch scheduler will include at least three major subsystems:
  1. The scheduler(s) observe the "to-do list" and decide what a particular computer should do next.
  2. Each computer has a job monitor that observes and reports exactly what a particular job is doing. The job monitor has the power to kill the job, to suspend it and so-on.
  3. Output disposition and file staging mechanisms get the output to a common place, and arrange for the files needed by a job to be within reach of that job when it runs.
They can also have neat features like these:
  1. Suspend/Resume: a job can be suspended and can then resume where it left off.
  2. Checkpoint/Restart: a job that fails does not have to start over.
  3. Multi-process/Multi-worker jobs: a job that can run across multiple processes and/or processors to the extent that the job scheduler determines that such resources are available.
When you "set up complicated crontabs," you're basically having to guess. If you guess wrong and set the times too short, jobs run into one another. Too long, and you're wasting precious milliseconds. A batch scheduler is a much smarter solution.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cron hourly, daily, cron.d jobs don't execute eggsmartha Linux - General 3 09-17-2007 06:37 PM
Cron Jobs christo512 Linux - Software 1 02-09-2006 09:19 AM
Cron Jobs XaViaR Linux - General 1 06-23-2005 06:57 PM
cron jobs working2hard Linux - Software 0 07-28-2004 05:31 PM
cron jobs Zyanid Linux - Software 0 08-27-2002 05:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration