Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

4 Answers

up vote 3 down vote accepted

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.

share|improve this answer
    
I was doing it the same way. Was wondering if cron should have this feature integrated. –  NOLFXceptMe Jan 20 '12 at 9:25
    
can you explain what this does/how it solves the issue? –  PsychoData Oct 4 '13 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. –  RedGrittyBrick Oct 4 '13 at 11:08
add comment

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.

share|improve this answer
    
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. –  Keith Thompson Feb 13 '12 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. –  NOLFXceptMe Feb 13 '12 at 20:51
add comment

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

share|improve this answer
add comment

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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