Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git auto-pull using cronjob

Tags:

git

cron

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.

like image 889
kayue Avatar asked Dec 10 '10 23:12

kayue


3 Answers

Solution:

*/1 * * * * su -s /bin/sh nobody -c 'cd ~dstrt/www && /usr/local/bin/git pull -q origin master' 
like image 186
kayue Avatar answered Oct 08 '22 01:10

kayue


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.

like image 42
Dustin Avatar answered Oct 08 '22 02:10

Dustin


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

like image 27
hobs Avatar answered Oct 08 '22 01:10

hobs