1. Introduction

The cron and anacron tools are one of the most common ways to schedule tasks on Linux. However, we may sometimes want to partially disable a scheduled task or task list.

In this tutorial, we’ll explore ways to disable and reenable all or specific cron and anacron entries. First, we briefly refresh our knowledge about task files by providing a working example. Next, we delve into ways to stop and prevent cron and anacron from running any tasks. After that, we move to user task files and methods to control them. Finally, we show the most granular way of dealing with a specific cron or anacron task or task group.

We tested the code in this tutorial on Debian 11 (Bullseye) with GNU Bash 5.1.4. It should work in most POSIX-compliant environments.

2. Task Table File

Of course, crontab holds the tasks for each user, with the crontab tool being the standard way to edit the file with the same name. Similarly, anacrontab modifies the general system tasks in anacrontab.

For example, we might have several tasks in our system-wide /etc/crontab:

$ cat /etc/crontab
# /etc/crontab: system-wide crontab

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
0  8    * * *   x       /bin/x

Indeed, the format is similar to other files like /etc/anacrontab, the contents of /etc/cron.d, and /var/spool/cron/crontabs/USER (the crontab of user USER).

With so many files and tasks, what would be the best way to disable some or all of them? Before exploring that, it’s good practice to backup our cron and anacron setup.

3. Control cron Daemon and anacron

While anacron runs on demand, cron is a daemon, which we can control. Depending on the system type, we can do that in several ways:

To that end, we can employ the option that our Linux distribution provides.

3.1. Pause cron and anacron

As usual, we can just stop cron:

$ /etc/init.d/crond stop
$ service cron stop
$ systemctl stop cron

After running the appropriate command, cron should stop but can restart after a reboot since we didn’t disable it.

On the other hand, any manual calls to anacron would still run, so at least a surface scan for such can be beneficial:

$ grep --recursive --ignore-case 'anacron' /etc

Here, we use grep with its –recursive (-r) and –ignore-case (-i) options to recursively search for anacron entries in all files below /etc. After that, we’d need to comment out or modify any calls to anacron.

3.2. Resume cron and anacron

To start cron, we again use the appropriate command:

$ /etc/init.d/crond start
$ service cron start
$ systemctl start cron

Yet, restoring each modification to anacron calls can be tedious.

4. Change Shell Variable

Indeed, an easier way to prevent cron and anacron jobs from running is to set the SHELL variable in their respective *tab files:

$ cat /etc/crontab
# /etc/crontab: system-wide crontab

SHELL=/bin/false
[...]

If we assign /bin/false to SHELL, tasks would still attempt to run, but nothing would actually execute.

Still, by disabling cron and anacron in any way, we halt the scheduling mechanism on a global level. Critically, the latter means we also stop and disable any system and superuser tasks.

Because of this, we move on to more granular solutions.

5. User crontab Disable

One option of crontab is -l, which lists the contents of the current user’s task file under /var/spool/cron/crontabs:

$ crontab -l
# Edit this file to introduce tasks to be run by cron.
[...]
$ cat /var/spool/crontabs/baeldung
# Edit this file to introduce tasks to be run by cron.
[...]

Thus, we can backup and restore a user’s crontab with two commands:

$ crontab -l > user1backup.cron
$ crontab user1backup.cron

By supplying a file to the crontab command, we replace the version in place. Further, by using the -r flag, we can remove our current crontab file entirely.

So, to disable all tasks of the current user temporarily, we can execute three commands:

$ crontab -l > user1backup.cron
$ crontab -r
$ [...]
$ crontab user1backup.cron

How long we wait after the first two depends on our needs.

Of course, we can always rename part of the path to the files or a particular file related to cron or anacron:

$ mv /etc/crontab /etc/crontab.stop
$ mv /var/spool/cron/crontabs/user1 /var/spool/cron/crontabs/user1.stop
$ mv /var/spool/cron /var/spool/cron_stop

Which file or path we choose depends on the configuration file function and our aim.

6. Comment Tasks

Like other configuration files in Linux, all of the aforementioned *tab files can contain comments. To add a comment, we start a line with the # octothorp or hashtag symbol.

Let’s comment out the last task in our original example file:

$ cat /etc/crontab
# /etc/crontab: system-wide crontab

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
#0  8    * * *   x       /bin/x

This is enough to prevent a task from running but preserve its definition. Let’s look at easier ways to do this.

6.1. vi

To halt all tasks, we can select the editor of crontab to be vi and run a global substitution command:

$ EDITOR=vi crontab -e
[...]
:%s/^/#/

Here, we use vi to place a # at each ^ (marker for) line start in the % current buffer, i.e., the contents of our crontab.

6.2. perl and awk

Two common interpreters in Linux are perl and the POSIX-compliant awk. Both can aid us with one-liners that can comment all lines of a cron or anacron tasks file.

To begin with, let’s check the awk solution:

$ crontab -l | awk '{print "#"$1}' | crontab
$ [...]
$ crontab -l | cut --characters=2- | crontab

The first command pipes the contents of crontab to awk, which prepends # to each line and further pipes the result to crontab, replacing the original. At this point, no tasks from the file would run.

When ready, we perform the reverse operation by substituting awk with a simple cut from the second character onwards.

Similarly, perl can do the same with its -e one-liner switch by looping over the lines via -n:

$ crontab -l | perl -ne 's/^/#/;print' | crontab
$ [...]
$ crontab -l | perl -ne 's/^#//;print' | crontab

In addition, we can use the same for /etc/crontab and append conditions for the replacement to specify particular (types of) tasks:

$ cat /etc/crontab | perl -ne 's/^/#/ if /^0/;print'
[...]
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
#0  8    * * *   x       /bin/x

Here, by including the if /^0/ statement, we only process the last task since it’s the only one with a 0 minutes specifier at the beginning of the line.

7. Summary

In this article, we looked at ways to disable cron and anacron tasks, as well as how to reenable them at a later stage.

In conclusion, we have multiple options to choose from when it comes to temporarily preventing some or all scheduled tasks from running on a Linux system.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.