如何使用Flock作为CRON作业运行1个Rsync / Rclone脚本的实例?

发布于 2025-01-26 22:46:02 字数 732 浏览 3 评论 0原文

我正在尝试将备份脚本的一个实例作为cron工作。
我知道我可以通过检查该过程是否正在运行的函数来完成:

if pgrep -x rclone >/dev/null
then
    # rclone is still running, backup is not done yet, exit.
    exit
else
    # rclone is not running.
    # start backup.
    /path/to/rclone/script.sh  
fi

但是经过一些研究,我发现flock应该使用而不是寻找过程ID,在crontab -e中在这种情况下,每30分钟运行一次脚本:
*/30 * * * */usr/bin/flock -n/var/lock/myjob.lock/path/to/rclone/script.sh

在上方运行命令需要sudo < /strong>。因此,脚本要求提供sudo密码,并且永远不会运行。 (我手动运行上面的命令,这就是我发现的)。

我如何确切使用羊群?当FLOCK要求它时,我是否在脚本中键入一个变量? (我知道这不是安全的,因此必须有不同的方法来执行此操作)。

我试图研究这个主题,但找不到任何很好的答案来解释如何正确使用它。

谢谢。

I'm trying to run only one instance of my back up script as a cron job.
I know I can do it with a function that checks if the process is running:

if pgrep -x rclone >/dev/null
then
    # rclone is still running, backup is not done yet, exit.
    exit
else
    # rclone is not running.
    # start backup.
    /path/to/rclone/script.sh  
fi

But after some research, I found out flock should be used instead of looking for process ID, in crontab -e, in this case, run the script every 30 minutes:
*/30 * * * * /usr/bin/flock -n /var/lock/myjob.lock /path/to/rclone/script.sh

Running the command above requires sudo. Therefore, the script asks for sudo password, and never runs. (I ran the command above manually, that's how I found out).

How exactly do I use flock? Do I type a variable in my script that injects the sudo password when flock asks for it? (I know it's not secure, so there must be a different way to do this).

I tried to research this subject but couldn't find any good answers that explain how to use it properly.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文