Insert a variable randomly generated value in Solaris Bash crontab

I have a job that needs to be on our hundreds of Solaris 10 x86 servers at nights
on Mon-Fri

As our cloud provisioning will read from the template to create/provision VMs
very often, I would like to put into crontab for the job to be run on Mon-Fri,
randomly being distributed.

So in the template which the provisioning process copies from, X could be 1-5
(ie Mon to Fri) : is there a way to insert a random value, ie X in the example
below  (1 to 5 inclusive) into the crontab of a Solaris 10?
       0 22 * * * X  /dir/Job
sunhuxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian UtterbackPrinciple Software EngineerCommented:
There are three ways to deal with this. One, run them all at the same time but have them each sleep for a randomly chosen amount. If you want to have them run at the same time on any given machine, pick a good, persistent file path and if there is no file there, write the sleep time to the file and if it is there, use the value there.

Another option is to have the job modify the crontab entry itself.  Read the crontab entry, if it is the default pick a random value and re-write the crontab entry.

Finally, build the randomization into the install procedure. Is there a script that sets the crontab? If so, change it there. If not, is it being installed via pkgadd? If so then you can make an install action that does what you want.

On Solaris 11 the correct way to do this is to use a transient install SMF service. I know that doesn't apply to you, but just thought I'd throw that in.
sunhuxAuthor Commented:
Ok, mind giving me a script (that uses 'random function' with truncation, round-down/up
& any other operations) that will randomly generate either 0, 1, 2, 3, 4 ?
That is I plan to adopt option 1 of sleeping for a random amount of time starting
from Monday (& based on the 5 random number given, I'll multiply them by x24x3600
for the script to sleep
ozoCommented:
sleep $((RANDOM % 5 * 24 * 3600))
sunhuxAuthor Commented:
Thanks very much chaps.


2 more questions:

a) which file in Solaris 10 stores root's crontab entries?

b) for a server which has generated the random value from
        sleep $((RANDOM % 5 * 24 * 3600))
    I'll need to save this random value to a file, say /opt/avscan/random.txt
    Purpose is so that we don't get a random value of 4 this week & then a random
    value of 0 next week, in case the AV scan overrun, we have up to 7 days' time
    before the next AV scan starts again ie to prevent sort of "race condition"
    where next week's AV scan starts before the current week's AV scan ends.
    So how do we use a script to read this value from /opt/avscan/random.txt ?
    I plan to :
      if exists /opt/avscan/random.txt
         then
            { sleep `value from random.txt`
               run AVscan}
         else          
            {sleep $((RANDOM % 5 * 24 * 3600))
               run AVscan}
      endif

     Can provide the actual Bash Shell script codes for the above pseudo codes?

Thanks very much
simon3270Commented:
(Is the avscan.sh script running OK now?)

I'd make a couple of changes to your script.  One is to check that it isn't already running, in case it takes longer than 7 days. The other is to calculate the correct sleep period first, then run the avscan (the "run AVscan" part is comon to both arms of your if/then/else, so it makes sense to move it to after the test). If we calculate the sleep time, store it in the text file for use next time.
#!/bin/bash

if pgrep avscan.sh >/dev/null; then
  echo AV Scan already running - aborting
  exit 1
fi

if [ -f /opt/avscan/random.txt ]; then
  sleeptime=$(</opt/avscan/random.txt)
else
  sleeptime=$((RANDOM % 5 * 24 * 3600))
  echo $sleeptime > /opt/avscan/random.txt
fi

sleep $sleeptime
/opt/avscan/avscan.sh
exit $?

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.