[英]Script to automate pushing to remote Github repo, won't work with crontab?
我正在嘗試制作一個簡單的 shell 腳本,該腳本將 Github 推送到我當前的工作區,並每 12 小時使用cron
自動啟動該腳本。
這是我得到的:
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
文件:
0 */12 * * * sh ~/scripts/backup-tools.sh
log
文件:(我還記錄了輸出)
[master xxxxxx] Automatic Backup of Latest Changes
1 file changed, 70 insertions(+), 43 deletions(-)
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
手動運行腳本可以工作,並遠程推送所有內容。 現在讓腳本由crontabs
運行也可以,即腳本執行,唯一的問題是,您可以從git
日志中注意到,自我上次推送它以來, origin master
分支沒有移動,也沒有遠程上傳任何內容我。
有誰知道問題是什么? 解決方法是什么?
謝謝。
它不起作用的原因是因為cron
作業正在一個新終端實例上啟動一個腳本,該腳本沒有獲取我的.zshrc
和.bashrc
,其中包含腳本的宏和重要信息。 所以我將它添加到腳本中:
if [ -z ${ZSH+x} ]; then # <--- if .zshrc wasn't sourced yet
source ~/.zshrc # <--- source it
fi
...而且效果很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.