简体   繁体   English

自动推送到远程 Github 存储库的脚本,不适用于 crontab?

[英]Script to automate pushing to remote Github repo, won't work with crontab?

I'm trying to make a simple shell script that pushes to Github my current workspace and automatically launch that script every 12 hours with cron .我正在尝试制作一个简单的 shell 脚本,该脚本将 Github 推送到我当前的工作区,并每 12 小时使用cron自动启动该脚本。

Here's what I got:这是我得到的:


shell script: shell脚本:

#!/bin/sh

git -C ~/backups/tools pull
git -C ~/backups/tools add .
git -C ~/backups/tools commit -m 'Automatic Backup of Latest Changes'
git -C ~/backups/tools push origin master

crontab file: crontab文件:

0 */12 * * * sh ~/scripts/backup-tools.sh

log file: (I also logged the output) log文件:(我还记录了输出)

  [master xxxxxx] Automatic Backup of Latest Changes
   1 file changed, 70 insertions(+), 43 deletions(-)

git log: git日志:

commit xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1 (HEAD -> master)
Author: user <address@mail.com>
Date:   Mon Jul 27 12:00:00 2020 -0700

    Automatic Backup of Latest Changes

commit xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx2
Author: user <address@mail.com>
Date:   Mon Jul 27 00:00:01 2020 -0700

    Automatic Backup of Latest Changes

commit xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx3 (origin/master, origin/HEAD)
Author: user <address@mail.com>
Date:   Sun Jul 26 10:02:53 2020 -0700

    Latest Change

Running the script manually works, and pushes everything remotely.手动运行脚本可以工作,并远程推送所有内容。 Now leaving the script to be run by crontabs also works, ie the script executes, the only problem being, which you can notice from the git log, that the origin master branch has not moved and nothing has been uploaded remotely since I last pushed it myself.现在让脚本由crontabs运行也可以,即脚本执行,唯一的问题是,您可以从git日志中注意到,自我上次推送它以来, origin master分支没有移动,也没有远程上传任何内容我。

Does anyone know what the problem is?有谁知道问题是什么? And what's the fix?解决方法是什么?

Thanks.谢谢。

The reason it didn't work is because the cron job was launching a script on a new terminal instance that didn't source my .zshrc and .bashrc which contained macros and important information for the script.它不起作用的原因是因为cron作业正在一个新终端实例上启动一个脚本,该脚本没有获取我的.zshrc.bashrc ,其中包含脚本的宏和重要信息。 So I added this to the script:所以我将它添加到脚本中:

if [ -z ${ZSH+x} ]; then        # <--- if .zshrc wasn't sourced yet
  source ~/.zshrc               # <--- source it
fi

...and it worked great. ...而且效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM