Git 使用 cronjob 自动拉取

发布于 2024-10-07 06:10:38 字数 437 浏览 3 评论 0原文

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

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

我尝试过的 crontab 条目:

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

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

有人可以帮忙吗?

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

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

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

The crontab entry I tried:

*/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 pull command itself is correct. I can run it without errors.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

方圜几里 2024-10-14 06:10:39

遇到同样的事情,我强烈建议像这样拉分支:

git fetch origin master:master

这样无论你在哪个分支,它都会拉原始主机并合并到本地主机中。不会签出你,不会中断你的工作(除非你可能会遇到 git lock,无论如何,考虑到你每半小时拉一次,这不会很长)。

另外,整个字符串(每个工作日,每半小时+每小时):

0,30 * * * 1-5 cd ~/work/project && git fetch origin master:master
0 * * * * 1-5 cd ~/work/project && git prune

如果您需要使 monorepo 更快,这确实是一件好事。

Encountered the same thing, and I highly recommend pulling the branch like this:

git fetch origin master:master

So that it pulls origin master and merges into local master regardless on what branch you're in. Doesn't checkout you, doesn't interrupt your work (except you may encounter a git lock, which isn't gonna be long anyway, considering you pull it every half an hour).

Also, the whole string (every working day, every half an hour + every hour):

0,30 * * * 1-5 cd ~/work/project && git fetch origin master:master
0 * * * * 1-5 cd ~/work/project && git prune

If you need to make a monorepo faster, it's a really good thing.

放手` 2024-10-14 06:10:38

解决方案:

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

Solution:

*/1 * * * * su -s /bin/sh nobody -c 'cd ~dstrt/www && /usr/local/bin/git pull -q origin master' 
恬淡成诗 2024-10-14 06:10:38

虽然您确实需要首先弄清楚如何使更新正常工作,但最好使用上游的钩子来使其运行。您可以简单地使用 post-commit 钩子中的curl 来完成此操作,或者如果您使用的是 github,则只需在其一侧使用 post-receive 钩子即可。

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-commit hook or if you're using github, just use a post-receive hook on their side.

闻呓 2024-10-14 06:10:38
*/1 * * * * su -s /bin/sh nobody -c 'cd /home/heilee/src/project && /usr/bin/git pull origin master'

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

*/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 -q after the pull rather than before. You won't notice that your pull isn't working until you tail the /var/log/syslog file or try to run your non-updated production code.

苦妄 2024-10-14 06:10:38

从 Git 版本开始 1.8.5 (Q4 2013) 您可以使用 -C 参数轻松完成此操作。然后,您不需要 cd 进入 Git 项目目录,而是可以将其指定为 Git 命令的一部分,如下所示:

*/15 * * * * git -C /home/me/gitprojectdir pull

Starting from Git version 1.8.5 (Q4 2013) you can use the -C argument to easily accomplish this. Then you don't need to cd into the Git project directory but can instead specify it as part of the Git command like this:

*/15 * * * * git -C /home/me/gitprojectdir pull
錯遇了你 2024-10-14 06:10:38

在撰写此答案时,所有建议的解决方案都以 cd 进入工作目录开始。就我个人而言,我更喜欢像这样使用 git-dir 参数。

git --git-dir=/path/to/project/.git pull

出于我自己的目的,我使用这个 cron 来使我的本地开发盒与其他开发人员保持同步。因此,我使用 fetch 而不是 pull

*/15 * * * * git --git-dir=/home/andey/path/.git fetch -a

为了简化当前接受的解决方案,使用 git-dir 参数

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

At the time of writing this answer, all the suggested solutions start with cd'ing into the working directory. Personally I would prefer to use the git-dir argument as so.

git --git-dir=/path/to/project/.git pull

For my own purposes, I'm using this cron to keep my local dev box up to date with what the other developers. Hence I'm using fetch instead of pull

*/15 * * * * git --git-dir=/home/andey/path/.git fetch -a

To simplify the current accepted solution, using the git-dir argument

*/1 * * * * su -s /bin/sh nobody -c '/usr/local/bin/git --git-dir=~dstrt/www pull origin master'
尘世孤行 2024-10-14 06:10:38
#!/bin/bash
cd /home/your_folder/your_folder && /usr/bin/git pull [email protected]:your_user/your_file.git

那是我一直在使用并且有效的

#!/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

飘然心甜 2024-10-14 06:10:38

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

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

这里是 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

我们必须设置工作目录

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

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

Here are the 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文