How To Set Up A Git Auto-Pull Cron Job For The Nobody User?

Published September 1, 2024

Problem: Setting Up Automatic Git Updates

Keeping a Git repository up-to-date can take time when done manually. Automating this process with a cron job for the 'nobody' user is challenging, as it needs careful setup to allow proper permissions and execution.

Solution: Setting Up the Cron Job

Correct Crontab Entry

To set up the cron job, use this syntax:

*/1 * * * * su -s /bin/sh nobody -c 'cd ~dstrt/www && /usr/local/bin/git pull -q origin master'

This cron job runs every minute. It uses 'su' to switch to the 'nobody' user, sets '/bin/sh' as the shell, and runs the git pull command.

Tip: Verify Cron Job Setup

After setting up your cron job, use the command 'crontab -l' to list all cron jobs and verify that your new entry is correct and active.

Git Pull Command

The git pull command in the cron job has these parts:

  • Full path to git: '/usr/local/bin/git'
  • 'pull' command to fetch and merge changes
  • '-q' flag for quiet mode, reducing output
  • 'origin master' specifies the remote and branch

This setup keeps your production site in sync with the master branch automatically.

Example: Logging Git Pull Results

To log the results of each git pull operation, modify the cron job like this:

*/1 * * * * su -s /bin/sh nobody -c 'cd ~dstrt/www && /usr/local/bin/git pull -q origin master >> /path/to/gitpull.log 2>&1'

This will append the output of each git pull to a log file, helping you track updates and troubleshoot if needed.