简体   繁体   English

连续运行bash脚本列表

[英]Run a list of bash scripts consecutively

I have a load of bash scripts that backup different directories to different locations. 我有大量的bash脚本,可以将不同的目录备份到不同的位置。 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. 我已经编写了一个脚本,该脚本基本上只连续调用每个脚本,并且位于cron.daily中,但是我希望该脚本可以工作,即使我添加和删除备份脚本而不必手动对其进行编辑。

So what I need to go is generate a list of the scripts (eg "dir -1 /usr/bin/backup*.sh") and then run each script it finds in turn. 因此,我需要做的是生成脚本列表(例如“ dir -1 /usr/bin/backup*.sh”),然后依次运行它找到的每个脚本。

Thanks. 谢谢。

#!/bin/sh    
for script in /usr/bin/backup*.sh
do
$script
done
#!/bin/bash
for SCRIPT in /usr/bin/backup*.sh
do
   [ -x "$SCRIPT" ] && [ -f "$SCRIPT" ] && $SCRIPT   
done

If your system has run-parts then that will take care of it for you. 如果您的系统有run-parts那么它将为您解决。 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). 您可以将脚本命名为“ 10script”,“ 20anotherscript”,它们将以类似于rc*.d层次结构的方式顺​​序运行(不过,该层次结构是通过init或Upstart运行的)。 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/ 您的系统可能仅通过将脚本放入/etc/cron.hourly/之类的目录就可以每小时,每天等运行cron作业。

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. (例如,不要使用点。)检查系统特定的手册页,因为文件命名限制可能会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM