Visit The New, Modern Unix Linux Community


[Tip] How to add individual delays to a cron job?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users [Tip] How to add individual delays to a cron job?
# 1  
[Tip] How to add individual delays to a cron job?

ofIn a big Unix environment you likely install cron jobs like this on a thousand systems:
Code:
39 15 * * * { /usr/local/monitoring/sendstats ; } >/dev/null 2>&1

If all the system clocks are synchronized (usually via NTP), these jobs run *exactly* at the same time.
If the cron job accesses a shared resource (network file system, database, send mail, ...) there is a load peak!
The solution is to add individual delays.
Say the delay should be within 60 seconds.
bash and ksh have $RANDOM and arithmetics; the following looks promising
Code:
39 15 * * * sleep $((RANDOM % 60)) && { /usr/local/monitoring/sendstats ; } >/dev/null 2>&1

Unfortunately % has a special meaning to "cron". It must be escaped \%, and the shell then sees \%.
This is really an obstacle!

I have found the following solutions:
Code:
39 15 * * * perl -e 'sleep rand(60)' && { /usr/local/monitoring/sendstats ; } >/dev/null 2>&1

Code:
39 15 * * * sleep `cksum /etc/hosts | awk '{print $1'\%'60}'` && { /usr/local/monitoring/sendstats ; } >/dev/null 2>&1

The latter has a static delay - but still individual.

Do you have other solutions?

Last edited by MadeInGermany; 01-08-2014 at 04:45 PM.. Reason: rephrased: often distribute to -> likely install on
This User Gave Thanks to MadeInGermany For This Post:
# 2  
I am not sure what problem this solves and a number of issues are raised:
  • Big Unix Environment
  • Submitting jobs to thousands of systems
  • load peaks for shared resources
For example, depending on what the task is and the targets, this could just be a matter of updating cron entries on the end systems as opposed to scheduling tasks from the cron of a single system or set of systems. Doing this for a thousand or so systems seems problematic.



I would not do this for a thousand systems, but rather I would either distribute crontabs to remote systems or look for an enterprise sheduling solution (open software; an example of paid-for solutions are products like Control-M).


Enterprise schedulers provide such features as distribution, redundancy and central reporting.


Just a thought.
# 3  
Maybe you misunderstood my post?
I did not mean the distribution to several systems.
Instead I mean: running a cron job on several systems at the same time.
I have rephrased my post.

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #993
Difficulty: Medium
Illumination is a free and open-source Unix operating system based on OpenSolaris.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Tip: inotify cron

Dear members, moderators and others. While working on <insert project>, a need has surfaced to watch a directory, and when a file comes, to do appropriate action. So, i started writing some shell code, aware of linux inotify-tools package with inotifywait. Also, i'm seeing a lot of similar... (1 Reply)
Discussion started by: Peasant
1 Replies

2. UNIX for Beginners Questions & Answers

Add TimeStamp to cron job and write file name sent

Hello, I have written a cron job to automate the sftp of files using key authentication. I wanted to add a timeStamp and name of file sent to a log file and append each these details to the same file each time files are sent and if possible include whether the files were sent successfully or not.... (3 Replies)
Discussion started by: KidKoder
3 Replies

3. Shell Programming and Scripting

Cron job - Need to run Cron every quarter at particular time

Hi, 1) If some job supposed to run on 1st of every month at 7 AM In cron job when we have a blackout on the 1st ( i.e when 1st falls on a sunday ) how can we make the job run the next business day? 2) How can we run a job on 25th of every quarter 7 AM(jan,apr,jul,oct) And if 25th... (5 Replies)
Discussion started by: System Admin 77
5 Replies

4. UNIX for Advanced & Expert Users

[Tip] How to add an application cron job?

A well established form of application cron jobs look like this: 39 15 * * * && /usr/local/monitoring/oracle/check_dbs.sh >/dev/null 2>&1The repetition makes it a long line, hard to read, hard to maintain. I suggest the following instead: 39 15 * * * { /usr/local/monitoring/oracle/check_dbs.sh... (1 Reply)
Discussion started by: MadeInGermany
1 Replies

5. Shell Programming and Scripting

Commented cron job -- cron monitoring

Hi I have a requirement to write a shell script,that will check the all commented job in cron job.Please help !! (2 Replies)
Discussion started by: netdbaind
2 Replies

6. Solaris

Cron job running even after cron is removed

Hi , I have removed a cron for particular user , but cron job seems to be running even after the cron entry is removed. The purpose of the cron was to sendmail to user ( it uses mailx utility ) I have restarted cron and sendmail service still user is getting mail alerts from the cron job. And... (4 Replies)
Discussion started by: chidori
4 Replies

7. Shell Programming and Scripting

How to add cron job in /etc/crontab

Hi, How to add a cron job in /etc/crontab using a shell script.??:confused: Actually the requirement is we need to run a script say, XXX.sh every 10 min through “cron”. That can be achieved by adding the below code line in the /etc/crontab , (i.e., “crontab -e ” command to add this to the... (4 Replies)
Discussion started by: Dedeepthi
4 Replies

8. Shell Programming and Scripting

Cron Job help

I need to write the cron job for the following scenario, Please help me out The CRON job runs sometime at night on Saturday and checks if there are more than eight files in the /PROCESSED folder. Any files over and above eight are deleted based on ascending order of date (eight most recent... (10 Replies)
Discussion started by: sandeep.dwivedi
10 Replies

9. Solaris

cron job starts new cron proccess

I run cron in solaris 10 zone. One cron job which syncing files to nfs mounted on container, creates after finishing another cron proccess(/usr/sbin/cron), and after 100 existing cron proccesses next cron job will not start. It's too weird for me, I'm not able to solve this problem. Theoretically... (3 Replies)
Discussion started by: ron76
3 Replies

10. UNIX for Dummies Questions & Answers

CRON usage for CRON job

can anybody explain the usage of CRON for adding a cron job. please provide an example also for better understanding !!! Thanks (1 Reply)
Discussion started by: skyineyes
1 Replies

Featured Tech Videos