1

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.

TomLisankie
  • 3,785
  • 7
  • 28
  • 32
  • What exactly are you trying to achieve by this? – Mureinik Dec 17 '22 at 19:57
  • @Mureinik I have a folder of notes that I back up to GitHub. Preferably I wouldn't have to do this process manually every time I make changes. – TomLisankie Dec 17 '22 at 19:59
  • No need for cron job: write a `post-commit` hook; push after every commit. Advantages: doesn't run where thre're no commits. – phd Dec 17 '22 at 20:23
  • 1
    Checklist for cron jobs: Does it run under the same user? Does it run with the same shell environment? Does it properly authenticate (PAT for HTTPS, keypair for SSH)? – phd Dec 17 '22 at 20:25
  • Thanks! And that makes sense, it probably is not properly authenticating. – TomLisankie Dec 18 '22 at 03:12
  • 1
    @phd Looks like the issue was git-lfs (I have some media I store in this repo) not being found in the path of the user cron is operating under. – TomLisankie Dec 18 '22 at 03:16
  • Note that Git, which attempts to be a good *version control* system (VCS), makes a *terrible* backup system to whatever extent it *is* a good VCS. macOS has Time Machine as a backup system, and Time Machine isn't bad as a backup system (and is therefore terrible as a VCS). VCS and backup are related but what makes something good at one of those jobs makes it bad at the other, and vice versa. – torek Dec 18 '22 at 04:53
  • For an analogy (if that helps), think about these two sports: basketball and gymnastics. What makes someone a good basketball player? Well, in general, being *tall* helps enormously. What makes someone a good gymnast? Well, in general, being *short* helps enormously. So someone who's a great gymnast is probably not a good basketball player, and vice versa, however good shape they're in otherwise. – torek Dec 18 '22 at 04:55
  • @torek Yeah I understand, I am just using `git` because I want to be able to keep track of the history of my edits. I also maintain a separate backup that doesn't keep track of edit history. I realize Time Machine can also keep track of edit history, but I switch back and forth between my Mac and a Linux machine and Time Machine doesn't work with Linux. – TomLisankie Dec 26 '22 at 04:47
  • You might want to use ZFS on Linux here; ZFS has snapshots and you can stream a snapshot backup to, e.g., S3 (encrypted even!). – torek Dec 26 '22 at 13:54

1 Answers1

1

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)

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250