Created
February 27, 2023 11:47
-
-
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`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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