107

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

jstricker
  • 107
Alex
  • 8,839

11 Answers11

207

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.

kubanczyk
  • 14,252
89

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
muffinista
  • 1,306
87

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

gregf
  • 1,078
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 (#).

andunix
  • 131
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

HBruijn
  • 84,206
  • 24
  • 145
  • 224
segaps
  • 81
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
ash
  • 31
2

I got the idea from the answer provided by @segaps

To disable:

crontab -l | awk '{print "# "$0}' | 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

In modern Linux that has systemd:

sudo systemctl stop crond.service

Which can then be re-enabled using

sudo systemctl start crond.service
isapir
  • 243
  • 1
  • 2
  • 9
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 :)
kubanczyk
  • 14,252
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"
}
HappyFace
  • 161
-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 #