LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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, 01:00 AM   #1
aristosv
Member
 
Registered: Dec 2014
Posts: 125

Rep: Reputation: Disabled
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, 02:28 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

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

Original Poster
Rep: Reputation: Disabled
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, 02:49 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


All times are GMT -5. The time now is 06:34 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration