Home > Net >  How can I `git push` from a script initiated by a cronjob?
How can I `git push` from a script initiated by a cronjob?

Time:12-18

I have the following script:

cd ~/path/to/repo ;
git pull ;
git add . ;
git commit -m "autocommit for repo" ;
git push ;
cd ~ ;

I have it run in a cronjob every minute. It works up until the git push command and then it does nothing. If I run the script manually it works fine. I updated the cronjob to write the output of running the script to a temp text file to see if git push was failing. It is not failing, it just isn't being ran at all it seems. The only output present is related to the git commit command. What might be going wrong?

I'm on a Mac running the latest macOS but I have tried it on my Linux machine as well with the same result.

CodePudding user response:

I updated the cronjob to write the output of running the script to a temp text file to see if git push was failing

Make sure to redirect both stdout and stderr, since most Git command produce outputs to stderr

*/1 * * * * /your/script.sh >> /your/script.log 2>&1

But a post-commit client-side local hook is a good alternative approach.

(As noted here, that means creating a push script named post-commit, without extension, as executable in your .git/hooks folder)

  • Related