【问题标题】:Run bash script on schedule按计划运行 bash 脚本
【发布时间】:2017-01-31 17:38:39
【问题描述】:

我对 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

【问题讨论】:

  • 检查cronat 命令。

标签: bash time backup


【解决方案1】:

我已在您的脚本中添加了该功能:

用法:

./yourscript.sh 3200

脚本:

#!/bin/bash

# make sure you gave a number of seconds:
[ 0$1 -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 $1
done

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

即使在您离开终端窗口后仍继续在后台运行:

nohup ./yourscript.sh 3200 &

即使在重启后也能按计划在后台运行:

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

【讨论】:

    猜你喜欢
    • 2010-10-20
    • 2019-06-12
    • 2019-09-19
    • 1970-01-01
    • 2019-05-02
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多