从cron作业中的脚本调用Git

时间:2019-02-10 19:14:34

标签: bash git cron

如果我从命令行运行这些命令,它将按预期运行。

git add * -v
git add . -v
git commit -m "This is the latest backup" -v
git tag -a v1.0.7 -m "my version v1.0.7" -v
git push origin v1.0.7 -v

但是,如果我在脚本名称script.sh中运行它:

cd working_dir && sudo -u user1 git add * -v >> logfile1 2> /home/user1/another.log
cd working_dir && sudo -u user1 git add . -v >> logfile1 2> /home/user1/another.log
cd working_dir && sudo -u user1 git commit -m "This is the latest backup" -v >> logfile1 2> /home/user1/another.log
cd working_dir && sudo -u user1 git tag -a v1.0.7 -m "my version v1.0.7" -v >> logfile1 2> /home/user1/another.log
cd working_dir && sudo -u user1 git push origin v1.0.7 -v >> logfile1 2> /home/user1/another.log

然后让cron运行script.sh:

57 13 * * * user1 /usr/local/bin/script.sh

然后问题是未创建标签,也未推送标签。

有什么想法我在这里做错了吗?

1 个答案:

答案 0 :(得分:1)

有很多事情可能出错了。检查您的cron日志以确保确定。

最可能的情况是/usr/local/bin/script.sh不起作用。这可能是因为它缺少#!/bin/sh,或者因为尚未将其设置为可执行文件(chmod +x /usr/local/bin/script.sh)。通过运行sudo -u user1 /usr/local/bin/script.sh可以很容易地进行检查。

cd working_dir也可能由于打字错误或权限问题而失败。


所有sudo都是不必要的,将用户的脚本放入/usr/local/bin也是如此。每个用户都有自己的cronfile,这可以完全由user1完成。

user1登录。将脚本放入~/bin/script.h中。确保它是可执行的。删除sudo。运行crontab -e以启动user1的cronfile。然后添加...

57 13 * * * ~/bin/script.sh

...没有用户。

相关问题