8

I have a list of jobs in the crontabs for user1, user2. I want to prevent the jobs of user2 from running.

I tried listing user2 in cron.deny, but that only prevents him from accessing his crontab. The jobs listed in user2's crontab still get executed.

How do I disable cron jobs for a specific user?

5 Answers 5

5

Brute force!

crontab -u fred -e
%s/^/#/
:wq

There's probably a more elegant way to do it, but unless you are doing this all the time, use the hammer you have.

3
  • I was doing it the same way. Was wondering if cron should have this feature integrated. Jan 20, 2012 at 9:25
  • 1
    can you explain what this does/how it solves the issue?
    – PsychoData
    Oct 4, 2013 at 5:51
  • 1
    @PsychoData: It comments-out all scheduled tasks for the specified user. See man crontab and man vi: %= every line, s/a/b/=substitute b for first a, ^=start of line. Lines starting with # are ignored by cron. Oct 4, 2013 at 11:08
5

The easiest way to disable the crontab for a specific user is to locate her crontab and rename it. The Debian man page for cron says:

cron searches its spool  area  (/var/spool/cron/crontabs)  for  crontab
files  (which  are named after accounts in /etc/passwd); crontabs found
are loaded into memory.

So just rename the file to something that is not in the passwd, usually by giving it a suffix like disabled, offline, dead or similar.

mv -vi /var/spool/cron/crontabs/user2 /var/spool/cron/crontabs/user2.disabled

On Debian (and related systems like Ubuntu) this produces the following entry in /var/log/syslog:

(user2.disabled) ORPHAN (no passwd entry)

Make sure to also add user2 to the file /etc/cron.deny, otherwise the user will be able to create a new crontab for herself.

Re-enable the user's crontab by deleting the entry from /etc/cron.deny and renaming her crontab:

mv -vi /var/spool/cron/crontabs/user2.disabled /var/spool/cron/crontabs/user2
1

Delete /var/spool/cron/user1 or user2 ? and you also want to add these user names to /etc/cron.deny otherwise they can add those cron jobs back

0

crontab -r username works on Solaris to remove the crontab for a given username.

If -r doesn't work for you, then try:

su username 'sh -c "crontab /dev/null"'

Quicker to type and easier to script than crontab -e.

2
  • The problem with this is that it destroys the user's information, which you probably shouldn't do unless you're dealing with a hostile user (and perhaps not even then). The other answer just comments out the user's crontab, which strikes me as a better idea. Feb 13, 2012 at 5:32
  • As @KeithThompson says, I do not want to erase the crontab for that user. I just want to disable it. The use case is such that I'd like to enable it later. Feb 13, 2012 at 20:51
0

If you want to block the crontab job for a particular user make sure that your logged in with root user,root user only have access for crontab files. 1.Then go to the dir /etc/cron.d in this directory you can see that cron.deny file,make an enter of particular user name which user want to block by using vi editor or orther.

1
  • This duplicates another answer and adds no new content. Please don't post an answer unless you actually have something new to contribute.
    – DavidPostill
    Apr 17, 2016 at 12:38

You must log in to answer this question.

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