LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-16-2015, 12:00 AM   #1
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Rep: Reputation: 3
crontab random delay not working


I am trying to delay the daily reboot of multiple Linux machines by a random time, within one hour.

This is to avoid a simultaneous reboot of all the Linux machines at the same time.

I also want to avoid specifying a time in cron. I want it to be completely random between 00:00 and 01:00.

So far I tried the commands below, but no luck. The machines still reboot at midnight.

Code:
@daily /bin/sleep $((RANDOM\%3600)) && /sbin/reboot
@daily /bin/sleep $(/usr/bin/expr $RANDOM \% 3600); /bin/reboot
 
Old 02-16-2015, 01:28 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
see man crontab, you will see crontab uses /bin/sh, not /bin/bash, therefore $RANDOM is an empty string.
 
Old 02-16-2015, 01:45 AM   #3
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Original Poster
Rep: Reputation: 3
So what should I do? If I add a /bin/sh at the front of the line, will it fix the problem? For example:

Quote:
@daily /bin/sh /bin/sleep $((RANDOM\%3600)) && /sbin/reboot
@daily /bin/sh /bin/sleep $(/usr/bin/expr $RANDOM \% 3600); /bin/reboot
 
Old 02-16-2015, 01:49 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
no, I suggest you to write a small shell script and crontab will invoke that only
Code:
script:
#!/bin/bash
/bin/sleep $((RANDOM\%3600))
/sbin/reboot
or whatever you want


crontab:
@daily /full/path/to/the/script
 
  


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
[SOLVED] delay and not working keyboard's backspace rpmacaspac Linux - Newbie 4 09-05-2013 08:40 AM
Java: Random Delay between repaint() rm_-rf_windows Programming 10 11-15-2012 08:34 PM
crontab not working mrgreaper Linux - Newbie 24 10-13-2011 12:20 PM
Crontab is not working, the script is working arfal SUSE / openSUSE 6 02-08-2010 08:48 PM
Crontab is not working Ejdaha Linux - Newbie 15 12-01-2009 05:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:55 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