Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git pull automatic via crontab

Tags:

git

cron

I would like to sync my git repository on my server to my local webserver. All the guides I read caused different errors and now I'm really desperate. I'm sorry if you already read this question a hundred times, but I still don't get it -.-

  • Working with Linux SuSE and conneting on it with PuTTY via SSH
  • the webprojekt where I need the sync is on the folder: /srv/www/htdocs/my_project
  • the git repository is here: /srv/git/repository/my_project
  • I have the users: "root" and "cron"

The cron should do this commands every minute or less with the user "cron":

cd /srv/www/htdocs/my_project
git pull origin
(git fetch) <-- not mandatory

These commands work fine when I input them manually. (except I have to enter the user password every time)

In this question I got some informations, but I always get an error when I try them myself.

My cron should look like this then:

* * * * * su -s /bin/sh cron -c 'cd /srv/www/htdocs/my_project && /usr/local/bin/git pull origin master' 

I executed this on my PuTTY, which answered me: -bash: bin: command not found

My next try was:

crontab * * * * * su -s /bin/sh cron -c 'cd /srv/www/htdocs/my_project && /usr/local/bin/git pull origin master'

Error: crontab: invalid option -- 's'

Error: crontab: usage error: unrecognized option

I tried a lot more combinations (removing the su -s or removing the -c etc.) but it never worked as I wanted -.- Shouldn't I do this command on PuTTY?

Good to know: I'm a linux and GIT beginner, started working with it yesterday and actually managed to do a lot til now. (^.^) So I would be happy to have an understandable explanation, in what I do wrong and what I have to do. It's confusing.

like image 512
lastria Avatar asked Dec 10 '25 01:12

lastria


1 Answers

The easiest way to achieve what you want (have the git repository automatically deployed on your server) is by putting the git repository inside you webserver and using a hook to do a check out on push. How to this is described in this answer: Deploy a project using Git push

like image 121
Chronial Avatar answered Dec 12 '25 14:12

Chronial



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!