Git 使用 cronjob 自动拉取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4414140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:38:26  来源:igfitidea点击:

Git auto-pull using cronjob

gitcron

提问by kayue

I was trying to create a cronjob with a task to do a git pullevery minute to keep my production site in sync with my master branch.

我试图创建一个 cronjob,任务是git pull每分钟执行一次,以保持我的生产站点与我的主分支同步。

The git pull needs to be done by the system user nobody, due to the permissions problem. However it seems that the nobodyaccount is not allowed run commands. So I have to create tasks as the rootuser.

nobody由于权限问题,git pull 需要由系统用户完成。但是,该nobody帐户似乎不允许运行命令。所以我必须以root用户身份创建任务。

The crontab entry I tried:

我试过的 crontab 条目:

*/1 * * * * su -s /bin/sh nobody -c 'cd ~heilee/www && git pull -q origin master' >> ~/git.log

It doesn't work, and I don't know how to debug it.

它不起作用,我不知道如何调试它。

Could anyone help?

有人可以帮忙吗?

UPDATE1: the git pullcommand itself is correct. I can run it without errors.

UPDATE1:git pull命令本身是正确的。我可以毫无错误地运行它。

采纳答案by kayue

Solution:

解决方案:

*/1 * * * * su -s /bin/sh nobody -c 'cd ~dstrt/www && /usr/local/bin/git pull -q origin master' 

回答by Dustin

While you do need to figure out how to get the update to work in the first place, you'd be far better off using a hook from the upstream to make it go. You can do this simply with curl from a post-commithook or if you're using github, just use a post-receive hook on their side.

虽然您首先需要弄清楚如何让更新工作,但最好使用上游的钩子来让它运行。您可以简单地使用post-commit钩子中的curl 来完成此操作,或者如果您使用的是 github,只需在他们身边使用 post-receive 钩子即可。

回答by hobs

*/1 * * * * su -s /bin/sh nobody -c 'cd /home/heilee/src/project && /usr/bin/git pull origin master'

This corrects a couple errors that prevented the accepted answer from working on my system (Ubuntu >10.04 server). The key change seems to be the -qafter the pullrather than before. You won't notice that your pull isn't working until you tail the /var/log/syslogfile or try to run your non-updated production code.

这纠正了一些错误,这些错误阻止了接受的答案在我的系统(Ubuntu > 10.04 服务器)上工作。关键的变化似乎是-q之后pull而不是之前。在您拖尾/var/log/syslog文件或尝试运行未更新的生产代码之前,您不会注意到您的 pull 不起作用。

回答by Syarif Alamoudi

#!/bin/bash
cd /home/your_folder/your_folder && /usr/bin/git pull [email protected]:your_user/your_file.git

that is has been using by me and worked

这是我一直在使用和工作

回答by xiazhanjian

I create a small script to deal with it.Then can use the by command crontab

我创建了一个小脚本来处理它。然后可以使用 by 命令 crontab

crontab -e
0 2 * * * cd /root && ./gitpull.sh > /root/log/cron.log  2>&1 &

Here are the gitpull.sh:

以下是gitpull.sh

#!/bin/bash

source /etc/profile
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
export TERM=${TERM:-dumb}

#----------------------------------------
# Please set the following variable section
# Please set up working directories, use','split
# eg:path="/root/test/path1,/root/test/path2"
path=""
#----------------------------------------

# Do not edit the following section

# Check if user is root
[ $(id -u) != "0" ] && { echo "${CFAILURE}Error: You must run this script as root.${CEND}"; exit 1; } 2>&1

# Check if directory path exists
if [[ "${path}" = "" ]]; then 
    echo "${CFAILURE}Error: You must set the correct directory path.Exit.${CEND}" 2>&1
    exit 1
fi

# Check if command git exists
if ! [ -x "$(command -v git)" ]; then
    echo "${CFAILURE}Error: You may not install the git.Exit.${CEND}" 2>&1
    exit 1
fi

# Check where is command git
git_path=`which git`

# Start to deal the set dir
OLD_IFS="$IFS" 
IFS="," 
dir=($path) 
IFS="$OLD_IFS" 

echo "Start to execute this script." 2>&1

for every_dir in ${dir[@]} 
do 
    cd ${every_dir}
    work_dir=`pwd`
    echo "---------------------------------" 2>&1
    echo "Start to deal" ${work_dir} 2>&1
    ${git_path} pull
    echo "---------------------------------" 2>&1
done

echo "All done,thanks for your use." 2>&1

We have to set the work directory

我们必须设置工作目录