Alex Asked: 2009-12-15 14:34:57 +0800 CST2009-12-15 14:34:57 +0800 CST 2009-12-15 14:34:57 +0800 CST How to disable everything in crontab -l? 772 I just want to pause everything. Don't execute anything listed on crontab -l. linux 10 Answers Voted kubanczyk 2009-12-15T14:41:12+08:002009-12-15T14:41:12+08:00 First, back up the crontab: crontab -l > my_cron_backup.txt Then you can empty it: crontab -r To restore: crontab my_cron_backup.txt crontab -l Best Answer gregf 2009-12-15T14:39:21+08:002009-12-15T14:39:21+08:00 crontab -e then comment out each line you don't want to run with #. muffinista 2009-12-15T14:46:28+08:002009-12-15T14:46:28+08:00 Do you have root access? Just pause cron sudo /etc/init.d/crond stop Then restart it when you're ready sudo /etc/init.d/crond start andunix 2009-12-16T02:46:46+08:002009-12-16T02:46:46+08:00 If you are using vi as editor, then just enter :%s/^/#/ in command mode. In all lines (%), it substitutes (s///) the begin of line (^) with a hash (#). segaps 2014-09-13T02:31:24+08:002014-09-13T02:31:24+08:00 Wasn't happy with the options above since they weren't one liners. To disable crontab -l | perl -nle 's/^([^#])/# $1/;print' | crontab To enable crontab -l | perl -nle 's/^#\s*([0-9*])/$1/;print' | crontab usage example ( edited to show it doesn't disable comments) $ crontab -l # Comment 0 0 * * 0 /opt/something.sh $ crontab -l|perl -nle 's/^([^#])/# $1/;print'|crontab $ crontab -l # Comment # 0 0 * * 0 /opt/something.sh $ crontab -l|perl -nle 's/^#\s*([0-9*])/$1/;print'|crontab $ crontab -l # Comment 0 0 * * 0 /opt/something.sh Tested this on RHEL and AIX , and should work out of the box without anything needed to be installed ash 2014-10-14T13:23:36+08:002014-10-14T13:23:36+08:00 In my limited testing, setting the shell to /bin/false works. You will still see /opt/job.sh executing in your logs, but it will be a noop: SHELL=/bin/false */1 * * * * root /some/job.sh kubanczyk 2015-11-06T09:12:49+08:002015-11-06T09:12:49+08:00 In any flavor of Unix/Linux that I know of: mv /var/spool/cron /var/spool/cron_is_disabled This: disables crontabs of all users but not system /etc/crontab (/etc/cron.daily. etc.) persists across a reboot is a one-liner, duh :) Sarvsav Sharma 2016-06-30T05:49:50+08:002016-06-30T05:49:50+08:00 I got the idea from the answer provided by @segaps To disable: crontab -l | awk '{print "# "$1}' | crontab To enable: crontab -l | cut -c 3- | crontab The only problem with the solution provided by segaps, is that it will uncomment the jobs, that are already commented by the user. Jonesome Reinstate Monica 2017-12-14T13:32:39+08:002017-12-14T13:32:39+08:00 To do this, using nano as the editor: sudo env EDITOR=nano crontab -e then comment out each line you don't want to run with # HappyFace 2020-04-16T08:47:16+08:002020-04-16T08:47:16+08:00 You can use the following like so: crondisable cronenable crondisable some_other_user ... The zsh code (put in your .zshrc): ecerr () { print -r -- "$@" >&2 } crondisable() { local user="${1:-$(whoami)}" local cronpath="/tmp/$user.cron.tmp" test -e "$cronpath" && { ecerr "There is already a disabled crontab at $cronpath. Remove that manually if you want to proceed." return 1 } crontab -l -u $user > "$cronpath" crontab -r -u $user } cronenable() { local user="${1:-$(whoami)}" local cronpath="/tmp/$user.cron.tmp" test -e "$cronpath" || { ecerr "No disabled cron at $cronpath" return 1 } crontab -u $user "$cronpath" mv "$cronpath" "${cronpath}.bak" }
First, back up the crontab:
Then you can empty it:
To restore:
crontab -e
then comment out each line you don't want to run with#
.Do you have root access? Just pause cron
Then restart it when you're ready
If you are using vi as editor, then just enter
:%s/^/#/
in command mode. In all lines (%), it substitutes (s///) the begin of line (^) with a hash (#).Wasn't happy with the options above since they weren't one liners.
To disable
crontab -l | perl -nle 's/^([^#])/# $1/;print' | crontab
To enable
crontab -l | perl -nle 's/^#\s*([0-9*])/$1/;print' | crontab
usage example ( edited to show it doesn't disable comments)
Tested this on RHEL and AIX , and should work out of the box without anything needed to be installed
In my limited testing, setting the shell to /bin/false works. You will still see
/opt/job.sh
executing in your logs, but it will be a noop:In any flavor of Unix/Linux that I know of:
This:
I got the idea from the answer provided by @segaps
To disable:
To enable:
The only problem with the solution provided by segaps, is that it will uncomment the jobs, that are already commented by the user.
To do this, using nano as the editor:
then comment out each line you don't want to run with #
You can use the following like so:
The
zsh
code (put in your.zshrc
):