[Git + cron]: Schedule your git commands with cron job

git

bash

11/21/2019


Note: cron has been deprecated in favor of launchd on macOS

launchd:

In computing, launchd, a unified operating system service management framework, starts, stops and manages daemons, applications, processes, and scripts in macOS. It was introduced with Mac OS X Tiger and is licensed under the Apache License — Wikipedia

More information about launchd is handled in this post

Using git commands with cron scheduler

Create a repo

1

If you haven't set up SSH key

Please follow this post to link your SSH key on your machine with your GitHub account before proceeding

Create a file that'll be pushed

BASH
vim READ.md
MD
## This file will be pushed to github repo with cron

Then save

Create .sh script

newrepo.sh (Example)

BASH
git init
git add README.md
git commit -m "[INITIAL]: initial commit 🔥"
git remote add origin https://github.com/EllisMin/auto-pushed-contents.git
git push -u origin master

Save it then grant executable permission with chmod +x newrepo.sh

Check your computer's time

BASH
date

Schedule with cron

BASH
sudo crontab -e

sudo allows you to run script as root

BASH
10 9 21 03 * cd ~/Desktop && ./newrepo.sh
# Minute | Hour | Date | DayOfWeek | Script

The shell script will be run at 03/21 at 9:10AM

Outputting result into file

BASH
10 9 21 03 * cd ~/Desktop && ./newrepo.sh > /tmp/result 2>&1

Output result in /tmp/result

2&>1 indicates that standard error is redirected to /tmp/result

View list of active crontab jobs

BASH
sudo crontab -l

Ran into trouble? (macOS)

If somehow doesn't work, try giving Full Disk Access to /usr/sbin/cron and /usr/bin/crontab on System Preference > Security & Privacy > Privacy > Full Disk Access. Press command + shift + G to open go to folder to type in manual directory.

More Examples

BASH
11 13 * * 1-5 /some/dir/script.sh

Execute every business day at 1:11PM

BASH
*/10 * * * * /some/dir/script.sh

Execute every 10 minutes

Schedule your mac to wake up

cron doesn't execute while your computer is asleep, but you can schedule your computer to awake a minute before scheduled cron job.

For macOS: Preference >> Energy Saver >> Schedule

Check logs

On linux, cron job gets logged at

TEXT
/var/log/system.log

Use the following to filter cron jobs

TEXT
grep CRON /var/log/system.log

WRITTEN BY

Keeping a record