/etc/crontab vs /etc/cron.d vs /var/spool/cron/crontabs/
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Ubuntu 10.04 LTS on IBM Lenovo R61e, RHEL5-6,SLES10-11
Posts: 262
Original Poster
Rep:
I already did but need sth kind of 'good practice'.
- /etc/cron.d ---> used by packages
- /etc/crontab ---> system crontab (with additional user directive)
- /var/spool/cron/crontabs ---> user crontab
Code:
REQUIREMENT :
--------------
I have to make a scripts that will restart 5 application servers at some time. Those servers run under user 'server_user'.
Code:
QUESTIONS :
------------
I don't know whether I should create one user file in /var/spool/cron/crontabs/server_user ?
OR
Add it into /etc/crontab (with user directive inside) ?
OR
Add it into /etc/cron.d/server_user (with user directive inside) ?
Last edited by drManhattan; 01-03-2011 at 06:34 AM.
Don't manually create crontab files inside /var/spool/cron/crontabs. They get created using the crontab command, either when logged in as the user (server_user) or as root using the -u option. It's not advised according to the man page.
Also for the other two options, read this quote from the man page.
Quote:
cron also reads /etc/crontab, which is in a slightly different format (see crontab(5)). Additionally, cron
reads the files in /etc/cron.d: it treats the files in /etc/cron.d as in the same way as the /etc/crontab
file (they follow the special format of that file, i.e. they include the user field). However, they are
independent of /etc/crontab: they do not, for example, inherit environment variable settings from it. The
intended purpose of this feature is to allow packages that require finer control of their scheduling than
the /etc/cron.{daily,weekly,monthly} directories to add a crontab file to /etc/cron.d.
So, if you need environment variables to be loaded, use /etc/crontab, if you don't, then you can go with a file in /etc/cron.d. Also a reason for putting a file in /etc/cron.d can be, as pointed out in the man page, a finer scheduling control, which you also have in /etc/crontab (but that includes the environment variables).
Distribution: Ubuntu 10.04 LTS on IBM Lenovo R61e, RHEL5-6,SLES10-11
Posts: 262
Original Poster
Rep:
I have placed my script in /etc/crontab and everything worked fine but when I placed the same script in /etc/cron.d, script stopped working.
What might be wrong ?
At first hand I'd say that if it works from /etc/crontab and not when placing the file in /etc/cron.d/ that it either has to do with permissions not set correctly or, which is most likely, that your script needs environment variables (which as documented in the quote from the man page) are not loaded when placing it in /etc/cron.d.
# m h dom mon dow command
* * * * * user1 /bin/echo "sample cmd1" >> /tmp/etc_crontab_d
The same file works in /etc/crontab but doesn't in /etc/cron.d/
Why ?
Hi,
As mentioned before, scripts that are in /etc/cron.d/ don't load environment variables. I'd look into that part. I'm assuming you added your command as root in the /etc/crontab file. If that's the fact then executing the crontab line will load the user's environment variables which don't get loaded when you put the script in /etc/cron.d. Log in as the user and run:
Code:
env
to check what's set as environment variables. See what you need to execute your script and add those variables to the script you run.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.