2

I am looking at run-parts script on CentOS 7. Very similar to this:

https://github.com/ikysil/run-parts/blob/master/run-parts

It appears to run everything in the cron.hourly etc. directories in sequence with a random delay between 1 and 300 seconds.

I have a lot of scripts in cron.hourly I must run for my unique setup. What I would like is all of them run at same time but with random delay before each.

Something like:

directory_script_list = contents(/etc/cron.hourly)
for script in directory_script_list:
  if script_is_executeable(script):
    execute(sleep(random(1, 300)) && script) &)

I do not want the server swamped every hour when they hit and I also do not want one of these scripts blocking the others.

The scripts do not use much CPU load what they mostly do is query slow replying SNMP devices, lots of them and in lots of ways. Some take a long time to complete and some are very quick. Not a lot of network traffic either. There are currently about 30 scripts but I frequently add, update and remove them.

Does anyone know of something that exists like this?

3

Since run-parts is very close to what you want, you could make a copy of run-parts and modify it to make it exactly what you want.

For instance:

# cp -a /usr/bin/run-parts /usr/local/bin/run-parts-parallel

Then add an '&' after the last 'fi' in /usr/local/bin/run-parts-parallel, as you can see in this diff:

# diff -u /usr/bin/run-parts /usr/local/bin/run-parts-parallel 
--- /usr/bin/run-parts  2014-06-09 18:14:31.000000000 -0400
+++ /usr/local/bin/run-parts-parallel   2017-02-03 14:55:11.327000000 -0500
@@ -95,7 +95,7 @@
               { print; }'
        logger -i -p cron.notice -t "run-parts($1)" "finished $(basename $i)"
    fi
-   fi
+   fi &
 done

 exit 0

You can then change the line in /etc/cron.d/0hourly from:

01 * * * * root run-parts /etc/cron.hourly

to

01 * * * * root RANDOMIZE=1 RANDOMTIME=300 /usr/local/run-parts-parallel /etc/cron.hourly

The script could also be modified to use /etc/sysconfig/run-parts-parallel to set the RANDOM* variables and remove them from the crontab file. Or, if you're not using run-parts elsewhere, you can put the RANDOM* variables in /etc/sysconfig/run-parts to make the random delay the global default.

  • I want the random sleep and the script in the background so the sleep delays the execution of the script it's associated with but has no affect on next script. – Matt Feb 5 '17 at 15:14
  • Hi @Matt - If you add the ampersand after the last 'fi', you will get this behavior. The ampersand spawns handling of each file into a new subprocess and continues on with the next file without delay. The random sleep and execution for each file is executed in the subprocesses. – Rich Alloway - RogueWave Feb 6 '17 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.