运行 bash 脚本如期

Run bash script on schedule

我在使用 bash 脚本时遇到一些问题。我需要向其中添加一些内容。我的脚本需要在特定时间 运行 但我不知道该怎么做。它应该像这样工作: 我有一个变量,然后我分配一个像 3200s 这样的时间。当我 运行 程序时,脚本将每 3200 秒创建一次备份,但前提是某些文件发生更改。我做错了什么?

!/bin/bash

SOURCE="/var/www/my_web/load/"
BACKUP="/home/your_user/load/"
LBACKUP="/home/your_user/load/latest-full/"

DATE=$(date +%Y-%m-%d-%T)

DESTINATION="$BACKUP"/"$DATE"-diff/

rsync -av --compare-dest="$LBACKUP" "$SOURCE" "$DESTINATION"

cd "$DESTINATION"
find . -depth -type d -empty -delete

我已将此功能添加到您的脚本中:

用法:

./yourscript.sh 3200

脚本:

#!/bin/bash

# make sure you gave a number of seconds:
[ 0 -gt 0 ] || exit

while true; do
    SOURCE="/var/www/my_web/load/"
    BACKUP="/home/your_user/load/"
    LBACKUP="/home/your_user/load/latest-full/"

    DATE=$(date +%Y-%m-%d-%T)

    DESTINATION="$BACKUP"/"$DATE"-diff/

    rsync -av --compare-dest="$LBACKUP" "$SOURCE" "$DESTINATION"

    cd "$DESTINATION"
    find . -depth -type d -empty -delete

    sleep 
done

如果您遇到类似 bash: ./yourscript.sh: Permission denied 的错误,那么您需要执行一次:chmod +x yourscript.sh 以使脚本可执行。

即使在您离开终端后也能在后台继续 运行ning window:

nohup ./yourscript.sh 3200 &

到 运行 即使在重新启动后也按计划在后台:

使用cron,例如Using crontab to execute script every minute and another every 24 hours