Link to home
Start Free TrialLog in
Avatar of UnixScott
UnixScott

asked on

Shutting off cron for a particular user - but only for a certain time period

Is there a way to turn off cron for a particular user for just a certain period of time?  I know that I could probably run a crontab -r to delete the current crontab, but then I would have to re-enter the commands.  I don't want cron itself stopped, just for one user, and only for a 4 hour time-period when we have to do some maintenance work.

Thanks
Scott
ASKER CERTIFIED SOLUTION
Avatar of tfewster
tfewster
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bira
bira

There´s a file named /var/adm/cron/cron.deny ( in AIX )
 or /var/spool/cron/deny  (in LINUX ) , i don´t know
 about other Unix where it is.
 You can can deny someone to use crontab by editing it and
 placing the user name.
Avatar of UnixScott

ASKER

I know that I can save the crontab (crontab -l ...) as tfewster suggests, but I was wondering if there was another way of doing it.  And cleaner way of simply turning off cron (just for one user) and then turn it back on.

As for the cron.deny, my understanding is that will only stop the user from running crontab, but it won't stop the current crontab from executing.  If I am mistaken, maybe someone will correct me.

Thanks for the answers so far,
Scott
First backup you crontab entries using
  crontab -l user > crontab_user.backup

Turn off all crontab jobs by editing the crontab entry using piped 'ex' commands to add '#' as 1st char on each line.
  EDITOR=ex; export EDITOR;
  echo "%s/^/#/g\nx\n" | crontab -e user

To restore your backup (turn on all the cron jobs)
  EDITOR=ex; export EDITOR;
  echo "%d\nr crontab_user.backup\nx\n" | crontab -e user

[the above 'ex' commands are 1) delete all lines, 2) read from backup file, 3) write then exit.]
Scott, which Unix do you use? Some variants may have the functionality you want (But this would be a function of `cron` rather than the `crontab` command).

man cron (HP-UX):
cron establishes a schedule for crontab files ... when it is notified by ... crontab that a (crontab) file has been added, deleted, or modified.

I.e. on HP-UX cron does not check cron.deny (as you had already figured out) - So I would have to do the save/delete/recreate method.

tfewster - I'm using HP-UX.

Yes, on HP-UX the cron.deny entry will only stop a user from executing the crontab function, but it will not stop cron from executing any of the commands in an already existing "crontab" file.

I had already tested the save/delete/restore method before I posted here, but it just doesn't seem very clean to me.  I was hoping that there would have been a flag or semaphore or something that could be set to do what I needed.  But it doesn't look like that's the case.

I will give it another day to see if anyone can come up with a different method.  If not, then it will be save/delete/restore method.

Rob-g : Thanks for the alternate method, I tried it and it works find.  It's basically a different way of doing the same thing.  Save the crontab, comment out the entries (instead of deleting the file), then restore the crontab from the backup.  If I can't find another method, then I will simply use the crontab -r to delete the crontab file, rather than changing the EDITOR variable, and piping in the substitute command through crontab.  But thanks anyway.

Scott