Scheduled Tasks on MacOS
Automating repetitive tasks on macOS can save time and improve productivity. In this post, I’ll walk you through how I automated syncing my local notes folder with its remote repository on GitHub.
Automating Git Sync
To achieve this, I created a simple script that automates the process of committing and pushing changes from a local Git repository to a remote one. Here’s how it works:
The commit-and-push.sh Script
This script takes a single parameter: the path to your local Git repository. It stages all changes, commits them with a default message, and pushes them to the remote repository.
#!/bin/bash
# Automates committing and pushing changes to a remote Git repository
set -eo pipefail
LOCAL_REPOSITORY=$1
cd "${LOCAL_REPOSITORY}"
git add .
git commit -m "[Automated] Commit and push"
git push
Setup
- Add to Path: Place the script in a directory that’s already included in your PATH. For example:
mv commit-and-push.sh /usr/local/bin
- Set Permissions: Make the script executable by running:
chmod +x /usr/local/bin/commit-and-push.sh
With this setup, you can run the script from anywhere without needing to specify its full path.
Scheduling with crontab
To schedule the script, I used crontab
, a utility for setting up recurring tasks.
Getting Started with crontab
- View Jobs:
crontab -l
- Edit Jobs:
crontab -e
These commands apply only to the current user, which is ideal for tasks requiring SSH keys, like pushing to a remote Git repository.
Setting Up the Cron Job
Cron jobs are defined using a simple structure:
- Cron Expression: Specifies when the task should run.
- Command: Specifies the task to execute.
For building the cron expression, tools like crontab.guru can help. Alternatively, you can consult the man crontab
manual.
Here’s the cron job I added:
0 0,12 * * * /usr/local/bin/commit-and-push.sh /Users/lima/Vault/
Explanation:
0 0,12 * * *
: Runs the job at midnight (00:00) and noon (12:00) every day./usr/local/bin/commit-and-push.sh /Users/lima/Vault/
: Executes the script to sync the notes folder located in/Users/lima/Vault/
.
Wrapping Up
With this setup, my notes folder stays synchronized without manual intervention. I hope this post inspires you to automate some of your own repetitive tasks!
- ← Previous
Hello, world!