LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   /etc/crontab vs /etc/cron.d vs /var/spool/cron/crontabs/ (http://www.linuxquestions.org/questions/linux-newbie-8/etc-crontab-vs-etc-cron-d-vs-var-spool-cron-crontabs-853881/)

drManhattan 01-03-2011 07:14 AM

/etc/crontab vs /etc/cron.d vs /var/spool/cron/crontabs/
 
Hi

I am trying to understand the difference between 3 crontab locations :
- /etc/crontab
- /etc/cron.d
- /var/spool/cron/crontabs/

Which location is used by sys admins ?

thx for help.

EricTRA 01-03-2011 07:18 AM

Hello,

Have a look at the man page for cron:
Code:

man cron
I believe it explains all you need to know.

Kind regards,

Eric

drManhattan 01-03-2011 07:32 AM

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) ?


EricTRA 01-03-2011 07:45 AM

Hello,

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).

Kind regards,

Eric

ashish_neekhra 01-03-2011 07:45 AM

Hi you can find info about cron here.


Thanks

drManhattan 01-03-2011 08:28 AM

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 ?

EricTRA 01-03-2011 08:39 AM

Hi,

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.

Kind regards,

Eric

drManhattan 01-03-2011 09:02 AM

This is file from /etc/cron.d location :
Code:


# 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 ?

silvyus_06 01-03-2011 04:40 PM

i thought /var/spool/ is for pending actions so ..

EricTRA 01-04-2011 02:12 AM

Quote:

Originally Posted by drManhattan (Post 4211437)
This is file from /etc/cron.d location :
Code:


# 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.

Kind regards,

Eric


All times are GMT -5. The time now is 08:29 PM.