我正在尝试将每天手工完成的工作自动化。我最终编写了一个 sh 脚本来推送 git 存储库。
脚本位于/home/autogit-project.sh
看起来像这样:
#!/bin/sh
cd /var/www/project
git add *
timestamp(){
date +"%d.%m.%Y um %H:%M"
}
git commit -am "Auto Server Commit $(timestamp)"
git push --repo https://[user]:[password]@github.com/[organisation]/[repo].git
通过 $ sh autogit-project.sh
运行脚本时它可以工作。
出于测试目的,我想每分钟运行一次脚本。我的 Crontab 看起来像这样:
@daily /home/anotherscriptthatworks.sh
* * * * * /home/autogit-project.sh
anotherscriptthatworks.sh
位于同一目录中,具有相同的用户权限。该脚本写入一个日志文件,所以我知道它有效。然而,我的 autogit-project.sh 没有,我正在努力找出原因。非常欢迎任何想法或帮助。
最佳答案
首先,您必须确保autogit-project.sh
具有执行权限
chmod a+x autogit-project.sh
其次,您可以按照以下形式编写 cron 任务(运行 crontab -e 并粘贴以下 cronjob)
* * * * * /bin/bash -l -c 'cd /var/www/project && ./autogit-project.sh >> /home/cron_log.log 2>&1'
您可以通过预期来检查应该运行该任务的用户的 cron 作业
crontab -l
or
crontab -l -u $user_name
关于git - 使用 crontab 自动更新 git 存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62191331/