14

How do I temporarily disable one or more users' cron jobs? In particular, I do not want to suspend the user's cron rights - merely not fire any of their jobs.

I am on SLES 11 SP2 and SP3 systems

  • Can't you just use the -u user switch of the crontab command and put a # comment character in front of the jobs to disable for the given user? – Janis Mar 6 '15 at 0:41
  • @Janis then I need to keep track of what the user (there are many) had already commented out, plus the user can go ahead and add another thing in (I am not adding them to /etc/cron.deny). Too much manual work :( – Dinesh Mar 6 '15 at 0:49
  • Dinesh, I (mis-)understood you want a fine-granular control about what you disable. – Janis Mar 6 '15 at 1:00
10

touch /var/spool/cron/crontabs/$username; chmod 0 /var/spool/cron/crontabs/$username should do the trick. Restore with chmod 600 and touch (you need to change the file's mtime to make cron (attempt to) reload it).

On at least Debian and probably with Vixie cron in general, chmod 400 /var/spool/cron/crontabs/$username also does the trick, because that implementation insists on permissions being exactly 600. However this only lasts until the user runs the crontab command.

If you want a robust way, I don't think there's anything better than temporarily moving their crontab out of the way or changing the permissions, and temporarily adding them to /etc/cron.deny.

  • Thanks. On my system it is /var/spool/cron/tabs/$username. But it still executes, even after the chmod. – Dinesh Mar 6 '15 at 3:19
  • After changing the file perms, I also needed to stop/restart cron. I thought its probably because cron might be holding the stuff in memory. So I added touch $username after the chmod. But that didn't help. (btw got your point on cron.deny) – Dinesh Mar 6 '15 at 3:28
  • @Dinesh Indeed you need to update the file's mtime or else cron thinks it hasn't changed and keeps cached information. I noticed this while testing yesterday but forgot to mention it in my answer, sorry about that. – Gilles Mar 7 '15 at 0:17
  • If you want to disable for all standard users just: chgrp root /var/spool/cron/crontabs – shrimpwagon Aug 25 '15 at 22:11
9

How about something like this to disable a user crontab:

crontab -l -u [username] >/tmp/[username].cron.tmp
crontab -r -u [username]

and to re-enable:

crontab -u [username] /tmp/[username].cron.tmp

This has the added advantage that you can run it as that user without needing root (just take the -u parameter away).

0

If you're using Debian, this only applies to Debian AFAIK:

You can do so by adding a dot to the name of the cronjob.

Behavior is referred to on the Debian official docs:

https://www.debian.org/doc/debian-policy/#cron-job-file-names

A cron job file name must not include any period or plus characters (. or +) characters as this will cause cron to ignore the file. Underscores (_) should be used instead of . and + characters.

0

If you just want to stop all cron jobs entirely for a while -- for example, while doing system maintenance which they might interact badly with -- the commands are normally

systemctl stop crond.service

and, to resume

systemctl start crond.service

I presume you need root or wheel authority to execute those; if necessary, run them via sudo or (last resort) log in as root.

Overkill for the specific question posed, but provides "one-stop shopping" and doesn't require playing with the filesystem or temporary files.

  • 2
    Since this disables cron for all users, it doesn’t really answer the question. – Scott Nov 30 '17 at 6:34
-1

I agree, the path via systemctl is the one to take. On raspberry, toe comands would be

sudo systemctl stop cron.service
sudo systemctl start cron.service
  • 1
    This will stop the entire cron service. The OP wanted to be able to choose which users' crontabs to disable. – roaima Oct 4 '18 at 10:25
  • 1
    It's also identical to another answer. – roaima Oct 4 '18 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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