Skip to content

Instantly share code, notes, and snippets.

@Walcriz
Created February 27, 2023 11:47
Show Gist options
  • Save Walcriz/fb4b8a51ab7d308f638be8482258bd88 to your computer and use it in GitHub Desktop.
Save Walcriz/fb4b8a51ab7d308f638be8482258bd88 to your computer and use it in GitHub Desktop.
Simple utility for cronjobs. Install via: `chmod +x cronhelper.sh && sudo ln cronhelper.sh /usr/bin/cronhelper`
#!/bin/sh
# See: https://unix.stackexchange.com/questions/363376/how-do-i-add-remove-cron-jobs-by-script
usage () {
cat <<USAGE_END
Usage:
$0 add "job-spec"
$0 list
$0 remove "job-spec-lineno"
USAGE_END
}
if [ -z "$1" ]; then
usage >&2
exit 1
fi
case "$1" in
add)
if [ -z "$2" ]; then
usage >&2
exit 1
fi
tmpfile=$(mktemp)
crontab -l >"$tmpfile"
printf '%s\n' "$2" >>"$tmpfile"
crontab "$tmpfile" && rm -f "$tmpfile"
;;
list)
crontab -l | cat -n
;;
remove)
if [ -z "$2" ]; then
usage >&2
exit 1
fi
tmpfile=$(mktemp)
crontab -l | sed -e "$2d" >"$tmpfile"
crontab "$tmpfile" && rm -f "$tmpfile"
;;
*)
usage >&2
exit 1
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment