Git auto-pull using cronjob

I tried to create a cronjob with the task of doing git pull every minute, so that my production site synchronized with my main branch.

The git output should be executed by the nobody system user due to a permission problem. However, it seems that the nobody account is not allowed to run commands. So I have to create tasks as root .

In crontab, I tried:

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

This does not work, and I do not know how to debug it.

Can anyone help?

UPDATE1: The git pull command is correct. I can run it without errors.

+50
git cron
Dec 10 '10 at 23:14
source share
6 answers

Decision:

 */1 * * * * su -s /bin/sh nobody -c 'cd ~dstrt/www && /usr/local/bin/git pull -q origin master' 
+27
Dec 11 2018-10-12T00:
source share

While you need to figure out how to get the update to work in the first place, you will be much better off using the upstream hook so that it can go. You can do this simply using curl from the post-commit hook, or if you use github, just use the hook after receiving on their side.

+10
Dec 11 '10 at 3:30
source share
 */1 * * * * su -s /bin/sh nobody -c 'cd /home/heilee/src/project && /usr/bin/git pull origin master' 

This fixes a couple of errors that prevented the accepted answer from working on my system (Ubuntu server> 10.04). Changing the key seems to be -q after pull , not before. You will not notice that your click does not work until you close the /var/log/syslog file or try to run non-updated production code.

+5
Jul 11 '12 at 18:40
source share
 #!/bin/bash cd /home/your_folder/your_folder && /usr/bin/git pull git@bitbucket.org:your_user/your_file.git 

which was used by me and worked

+3
Oct 13 '14 at 5:19
source share

I am creating a small script to handle it. Then you can use the crontab command

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

Here is 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 must set the working directory

+1
Jun 07 '18 at 0:23
source share

@xiazhanjian this script asks for a username and password. Because of what cronjob fails. Did I do something wrong? Or any possible solution?

0
Jun 06 '19 at 11:05
source share



All Articles