I am wanting to learn cron and I searched for some quality tutorials. I found How do I set up a cron job.
My goal is to have a folder on a network that will have sub-folders. Each sub-folder is a particular script that will be executed by the cron. I don't want to overload our system so how can I create the cron the run a shell script after the assigned time and previous script is ran?
Example:
Main Folder
Sub1 - script1
Sub2 - script2
Sub3 - script3
Cron 10:01 - runs script1
Cron 10:02 - runs script2
Cron 10:03 - runs script3
Cron 10:04 - runs script1
Cron 10:05 - runs script2
Idea Outline
Your friend is an old fashioned
cron
companionat
.Combine this with
run-parts
.The idea is that you write a cron job which starts the scripts in the "sub folders" with the use of
run-parts
.If you want to parallelise the execution depending on the system load, you can wrap the execution of each single script with
batch
.Example
On my installations I use my own wrapper script
batchme
. This provides some enhancements for mailing reports and output.The cron.{daily,weekly,monthly} scripts are wrapped in my crontab this way:
Write yet a 4th script which calls the first 3 scritps in order.
Then call the 4th from cron. The most common problem with cron is that it runs with a minimal shell, so call all the scripts, including the 4th script, by the full path.
If you are having problems with that, post your cron entry and further details on what the problem you are having.
Instead of calling a new script every minute or so via cron I would call one script and put all your code in that one script. Then use the sleep command (assuming you are scripting in bash, if not then the equivalent for that scripting language), to wait a specific interval before executing the next part of the script. This will ensure time between the scripts and they will only run once the other script has completed successfully.
An example could be:
Cron (Run the script every 15min)
Script
Notice the sleep 120 which tells bash to wait 2 minutes or 120 seconds before executing the next line.