Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Will renaming a cron file in /var/spool/cron/ mean that the contents of the cron file will not be executed anymore? Or will all files in /var/spool/cron/ be executed, no matter the name of the file.

share|improve this question
    
You might want to specify what OS this is on, and if Linux, which distro. –  Some Guy Oct 6 '09 at 15:47
    
@Some Guy: The tag says "redhat" –  Dennis Williamson Oct 6 '09 at 16:20
add comment

4 Answers

Files in /var/spool/cron (or /var/spool/cron/crontabs or /var/spool/cron/tabs on some systems) will be run with the permissions of the user for whom the file is named. For instance, /var/spool/cron/root will run as user "root" and /var/spool/cron/tom_13 will run as user "tom_13".

If a crontab file is renamed to another valid user, it should run as that user. However, there are two caveats:

  1. If you rename a crontab to a filename that does not correspond to a valid user, I don't know what the expected behavior is. It might run as root, it might run as the user owning the file, or might not run at all.
  2. Some cron daemons do not check /var/spool/cron for changes unless you make those changes with the crontab command. If you are going to make changes manually, you may need to restart your cron daemon for them to take effect.

Check the manpage for crontab (man 1 crontab) to see how it works. If you're using Vixie cron (which most Linux distributions seem to favor these days), you could do the following to make one user's crontab run as a different user (same effect as renaming the file, but safer):

  1. Save the old user's crontab to a file: crontab -u olduser -l > olduser.cron
  2. Import it into the new user's crontab: crontab -u newuser olduser.cron
  3. Delete the old user's crontab: crontab -u olduser -r
share|improve this answer
add comment

Cron is tightened to /etc/crontab, where all the magic happens. Actually, by default crontab has only one record similar to this:

-*/15 * * * *   root  test -x /usr/lib/cron/run-crons && /usr/lib/cron/run-crons >/dev/null 2>&1

As you see, this will execute /usr/lib/cron/run-crons every 15 minutes and run-crons in fact is a script.

Taking a brief look at the script you will see what directories should hold cron scripts:

for CRONDIR in /etc/cron.{hourly,daily,weekly,monthly} ; do

Digging deeper and checking what this script is about you can see that yes, it will execute all the scripts in appropriate directories:

for SCRIPT in $CRONDIR/* ; do
    test -d $SCRIPT && continue
    case "$SCRIPT" in
           .svn)           continue ;;
           *.rpm*)         continue ;;
           *.swap)         continue ;;
           *.bak)          continue ;;
           *.orig)         continue ;;
           \#*)            continue ;;
           *~)             continue ;;
    esac

... except mentioned file extensions. So you can simply add a ".bak" add the end of the file so cron will not execute it.

NOTE: This post is written using OpenSUSE, and things can vary for other distros

share|improve this answer
1  
Entries in /etc/crontab and /etc/cron.* are installed and run independently of the per-user crontabs in /var/spool/cron that Tom_13 is asking about. –  James Sneeringer Oct 6 '09 at 16:07
    
-1 for not answering the question –  asdmin Oct 6 '09 at 16:36
add comment

If you want to prevent it from running, one way would be to edit the crontab for that user and comment out all the lines:

sudo crontab -u username -e

and put a # at the beginning of each line that doesn't have one.

share|improve this answer
add comment

The man cron says the following:

   Cron searches /var/spool/cron for crontab files which are named  after
   accounts  in /etc/passwd; crontabs found are loaded into memory. Cron
   also searches for /etc/crontab and the files in the /etc/cron.d direc-
   tory,  which  are  in  a different format (see crontab(5)).  Cron then
   wakes up every minute, examining all stored  crontabs,  checking  each
   command  to  see if it should be run in the current minute.  When exe-
   cuting commands, any output is mailed to the owner of the crontab  (or
   to  the  user named in the MAILTO environment variable in the crontab,
   if such exists).

   Additionally, cron checks each minute to see if its spool  directoryâs
   modtime  (or  the modtime on /etc/crontab) has changed, and if it has,
   cron will then examine the modtime on all crontabs  and  reload  those
   which  have  changed.   Thus  cron  need  not  be restarted whenever a
   crontab file is modified.  Note that the  Crontab(1)  command  updates
   the modtime of the spool directory whenever it changes a crontab.

So if I interpret this correctly, a rename to an unexisting user will do the trick..

share|improve this answer
    
My man cron says "Note that crontabs in this directory should not be accessed directly - the crontab command should be used to access and update them." –  Dennis Williamson Oct 7 '09 at 18:08
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.