62

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

58

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

121

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
  • 2
    This is particularly helpful when /tmp is corrupted (and if your crontab -e uses mktemp in /tmp. – Kevin Lee Mar 23 '15 at 15:17
  • 1
    +1 Finally, this is what worked for me. Thank you – Pavan Jun 4 '15 at 23:25
  • @alex This should be the accepted answer – Luis Ferrao Sep 7 '18 at 8:52
  • crontab -r is what I was looking for. Thanks ! – forzagreen Feb 26 at 13:48
62

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
  • 5
    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 '09 at 0:20
  • our crond service had been stopped. This pointed us in right direction to check whether crond was running, and restart. – Paul May 24 '16 at 12:51
  • 4
    If you're on Ubuntu you can use sudo service cron stop – Joel Oct 28 '16 at 11:14
  • 1
    On Debian this is /etc/init.d/cron stop. – Faheem Mitha Jul 19 '17 at 5:52
  • 2
    @Faheem on Debian nowadays it’s also service cron stop. – Stephen Kitt Jul 19 '17 at 8:42
10

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

  • 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 '13 at 11:53
  • 2
    To uncomment, command is :%s/^#// – NP01 Aug 14 '13 at 3:18
3

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

  • 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 '16 at 6:10
  • Could you please explain why comments are not affected? What is the pattern you use to detect them? – Sopalajo de Arrierez Dec 24 '17 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 '18 at 14:06
2

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

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

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.

0

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 #

protected by Sven Aug 30 '17 at 8:20

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.