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.

Whenever I need to enable crons, I will copy the cron file to the spool folder.

cp /home/shantanu/shantanu /var/spool/cron/
chown shantanu:root /var/spool/cron/shantanu 

Will this work? Any issues I may face?

update: using CentOS

share|improve this question

4 Answers 4

up vote 1 down vote accepted

The official way to disable some cron jobs for specific user is type crontab -e -u <user> and put a # at the begin to comment out.


UPDATE

Because cron file is going to be a part of shell script.

Try this:

crontab -l -u shantanuo > shantanuo.cron
sed -i 's/^/#/' shantanuo.cron
crontab shantanuo.cron
rm -f shantanuo.cron
share|improve this answer
    
Isn't copy easier than this? This is going to be one time job. I will keep the cron file ready as txt file and copy it to spool just before taking my application in production. I still do not see any risk in doing so. –  shantanuo Nov 25 '11 at 9:44

It may work. But..

As the previous posters said: Don't do this. Use crontab -e. Directly editing the files may work, but isn't robust practice. Your stuff could break any time when updating the involved components. Also, it's not very portable, other systems may store the files in a different place. It's like implementing "printf" by yourself instead of using the standard iolib.

So, don't do it this way.

edit: And if you need to generate the crontab with a shell script, then just pipe your stuff into crontab (It even knows the pseudo-file '-' ! Read the man page.).

share|improve this answer

Why not just use command crontab -e, or if you are logged in as root, crontab -e -u shantanu?

share|improve this answer
    
Because cron file is going to be a part of shell script. –  shantanuo Nov 25 '11 at 9:22
    
Err.... what? Please elaborate a bit more. :-) –  Janne Pikkarainen Nov 25 '11 at 9:24
    
He meant the crontab won't be created interactively. I edited my answer below to take this into account. –  Roman Nov 25 '11 at 9:30

This may or may not work depending on which operating system (and which version of cron) you are using. Checking some Linux boxes, their cron man page says this

Additionally, cron checks each minute to see if its spool directory's mod‐ time (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, providing the /var/spool/cron directory's mtime is updated then cron will check the individual crontabs to see if they have been updated a reread them.

If though we look at a Solaris 10 box it's cron man page says this

cron only examines crontab or at command files during it's own process initialization phase and when the crontab or at command is run. This reduces the overhead of checking for new or changed files at regularly scheduled intervals.

So on a Solaris 10 system (and probably others) you would have to use the crontab command to change the files and inform cron that it should reread it's crontab files.

share|improve this answer

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.