Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
share|improve this question
add comment

3 Answers 3

up vote 2 down vote accepted

Idea Outline

Your friend is an old fashioned cron companion at.

at and batch read commands from standard input or a specified file which are to be executed at a later time, using /bin/sh

Combine this with run-parts.

run-parts - run scripts or programs in a directory

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:

@midnight   root  test -x /usr/sbin/anacron ||  for script in $( run-parts --list  /etc/cron.daily)  ; do batchme --quiet --info "cron-daily ${script}" ${script} ; done

@weekly   root  test -x /usr/sbin/anacron ||  for script in $( run-parts --list  /etc/cron.weekly) ; do batchme --quiet --info "cron-weekly ${script}" ${script} ; done

@monthly        root  test -x /usr/sbin/anacron ||  for script in $( run-parts --list  /etc/cron.monthly); do batchme --quiet --info "cron-monthly ${script}" ${script} ; done
share|improve this answer
add comment

Write yet a 4th script which calls the first 3 scritps in order.

 #! /bin/sh
/path/to/script1
/path/to/script2
/path/to/script3

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.

share|improve this answer
    
My apologies the three scripts were an example of what I am trying to do. Currently there are 20 scripts so this method would be hard to manage but the idea is nice for a few scripts. –  Matt Mar 5 '13 at 23:29
add comment

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)

*/15 * * * * /path/to/script

Script

 #!/bin/bash
 if command
       then
                   command executed successfully
                   sleep 120

       else
                   command failed so
                   execute all commands up to fi
       fi

Notice the sleep 120 which tells bash to wait 2 minutes or 120 seconds before executing the next line.

share|improve this answer
    
I generally debated this route but the scripts get modified regularly so this would prove timely. –  Matt Mar 5 '13 at 23:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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