101

I just want to pause everything. Don't execute anything listed on crontab -l.

10 Answers 10

82

crontab -e then comment out each line you don't want to run with #.

2
  • 20
    Try this with 100 jobs ...
    – merlin
    May 19, 2020 at 4:40
  • 4
    @merlin Use column edit move in vi/vim
    – whirlwin
    Jun 8, 2020 at 13:33
198

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

This works only for the crontab of the user who runs these commands, but it does not empty/restore crontabs of other users. My other answer is about suspending launches from all the users.

2
  • 2
    This is particularly helpful when /tmp is corrupted (and if your crontab -e uses mktemp in /tmp.
    – Kevin Lee
    Mar 23, 2015 at 15:17
  • 1
    If you are running your cronjobs in sudo better make sure you sudo first when backing up so you get the right one. May 24, 2022 at 18:15
84

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
7
  • 10
    That is assuming you want to stop crontab for all users including root. The selected answer, and kubanskamac's answer would do it for just the current (desired?) user.
    – Kevin K
    Dec 15, 2009 at 0:20
  • 1
    our crond service had been stopped. This pointed us in right direction to check whether crond was running, and restart.
    – Paul
    May 24, 2016 at 12:51
  • 12
    If you're on Ubuntu you can use sudo service cron stop
    – Joel
    Oct 28, 2016 at 11:14
  • 3
    On Debian this is /etc/init.d/cron stop. Jul 19, 2017 at 5:52
  • 3
    @Faheem on Debian nowadays it’s also service cron stop. Jul 19, 2017 at 8:42
13

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 (#).

2
  • If you are not using vi as default editor, you can "force" usage of vi like EDITOR=vi; crontab -e, and than you can use the trick above.
    – Betlista
    Jan 25, 2013 at 11:53
  • 3
    To uncomment, command is :%s/^#//
    – NP01
    Aug 14, 2013 at 3:18
6

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

5
  • 1
    Also can look for specific details in the cronjob crontab -l | perl -nle 's/^#\s*([0-1*])/$1/;print if /.+mytexttofind.+/'
    – Jason
    Nov 6, 2016 at 6:10
  • Could you please explain why comments are not affected? What is the pattern you use to detect them? Dec 24, 2017 at 23:39
  • Hm, under Debian Jessie I get the following error: crontab: usage error: file name must be specified for replace. It seems you have to specify a file (?) when using crontab without any parameters (which is replace by default).
    – fritzmg
    Dec 4, 2018 at 14:06
  • @Sopalajo de Arrierez If the first character after #/space is a digit 0-9, then it should be a cron scheduling, and we have to drop the pound sign. This is because the comments mainly start with a word. Unfortunately, this will fail with a comment like this: "# 2 do the backups".
    – Fjor
    Oct 27, 2022 at 23:03
  • @fritzmg I suggest that the pseudo-filename - (dash as standard input) needs to be self-explanatory : [..];print' | crontab - Jan 13, 2023 at 16:07
3

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
1

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.

1
  • Disable command should be: crontab -l | awk '{print "# "$0}' | crontab Aug 25, 2020 at 8:49
1

In any flavor of Unix/Linux that I know of (except maybe OpenBSD):

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 :)
0

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"
}
-1

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 #

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .