tags:

views:

101

answers:

3

I have a load of bash scripts that backup different directories to different locations. I want each one to run every day. However, I want to make they don't run simultaneously.

I've wrote a script that basically just calls each script in succession and sits in cron.daily, but I want a way for this script to work even if I add and remove backup scripts without having to manually edit it.

So what I need to go is generate a list of the scripts (e.g. "dir -1 /usr/bin/backup*.sh") and then run each script it finds in turn.

Thanks.

+5  A: 
#!/bin/sh    
for script in /usr/bin/backup*.sh
do
$script
done
PeterMmm
Thanks so much, I will try it out.
Darren
This is not scalable, since the glob will fail if it expands to too many files.
William Pursell
And it will fail if there are no files.
camh
+4  A: 
#!/bin/bash
for SCRIPT in /usr/bin/backup*.sh
do
   [ -x "$SCRIPT" ] && [ -f "$SCRIPT" ] && $SCRIPT   
done
ghostdog74
That `x` bit is a very nice idea, as makes it very easy to temporarily disable one of those backup scripts just by running `chmod -x backup-foo.sh`.
ndim
Thanks very much.
Darren
@ghostdog: If you are going to call `/bin/bash`, you really aught to use [[ instead of [
SiegeX
@Siegex, thanks point taken. I will use it where its due, but in this case, using [ is perfectly fine.
ghostdog74
+2  A: 

If your system has run-parts then that will take care of it for you. You can name your scripts like "10script", "20anotherscript" and they will be run in order in a manner similar to the rc*.d hierarchy (which is run via init or Upstart, however). On some systems it's a script. On mine it's a binary executable.

It is likely that your system is using it to run hourly, daily, etc., cron jobs just by dropping scripts into directories such as /etc/cron.hourly/

Pay particular attention, though, to how you name your scripts. (Don't use dots, for example.) Check the man page specific to your system, since file naming restrictions may vary.

Dennis Williamson