按计划运行bash脚本 [英] Run bash script on schedule

查看:66
本文介绍了按计划运行bash脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some problem with bash script. I need to add some content to it. My script need to run at a certain time but I don't know how to do that. It should work like this: I have a variable then I assign a time like 3200s. When I run the program, then the script will create backups every 3200s but only if some files changed. What am I doing wrong?

!/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

推荐答案

在这里我已将该功能添加到您的脚本中:

here i have added that feature to your script:

用法:

./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:权限被拒绝之类的错误,则需要执行一次: chmod + x yourscript.sh 脚本可执行文件.

if you get an error like bash: ./yourscript.sh: Permission denied, then you need to do this once: chmod +x yourscript.sh to make the script executable.

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

to continue running in background even after you leave the terminal window:

nohup ./yourscript.sh 3200 &

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

to run in background on schedule even after restart:

使用 cron ,例如 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆