Git auto-pull using cronjob

GitCron

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

Solution:

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

Solution 2 - Git

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.

Solution 3 - Git

*/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.

Solution 4 - Git

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'

Solution 5 - 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

Solution 6 - Git

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

Solution 7 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkayueView Question on Stackoverflow
Solution 1 - GitkayueView Answer on Stackoverflow
Solution 2 - GitDustinView Answer on Stackoverflow
Solution 3 - GithobsView Answer on Stackoverflow
Solution 4 - GitAndrew WeiView Answer on Stackoverflow
Solution 5 - GitSyarif AlamoudiView Answer on Stackoverflow
Solution 6 - GitxiazhanjianView Answer on Stackoverflow
Solution 7 - GitJohnView Answer on Stackoverflow