如何使用Flock作为CRON作业运行1个Rsync / Rclone脚本的实例?
我正在尝试将备份脚本的一个实例作为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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论