使用 crontab 自动更新 git 存储库

Automatically updating git repository using crontab

我正在尝试使我每天手工完成的工作自动化。我最终编写了一个 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