LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Reply
 
Search this Thread
Old 01-03-2011, 06:14 AM   #1
drManhattan
Member
 
Registered: Aug 2010
Location: Warsaw, Poland
Distribution: Ubuntu 10.04 LTS on IBM Lenovo R61e, RHEL5-6,SLES10-11
Posts: 250

Rep: Reputation: 1
/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.
 
Old 01-03-2011, 06:18 AM   #2
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290
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
 
Old 01-03-2011, 06:32 AM   #3
drManhattan
Member
 
Registered: Aug 2010
Location: Warsaw, Poland
Distribution: Ubuntu 10.04 LTS on IBM Lenovo R61e, RHEL5-6,SLES10-11
Posts: 250

Original Poster
Rep: Reputation: 1
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.
 
Old 01-03-2011, 06:45 AM   #4
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290
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
 
Old 01-03-2011, 06:45 AM   #5
ashish_neekhra
Member
 
Registered: Nov 2007
Posts: 67

Rep: Reputation: 4
Hi you can find info about cron here.


Thanks
 
Old 01-03-2011, 07:28 AM   #6
drManhattan
Member
 
Registered: Aug 2010
Location: Warsaw, Poland
Distribution: Ubuntu 10.04 LTS on IBM Lenovo R61e, RHEL5-6,SLES10-11
Posts: 250

Original Poster
Rep: Reputation: 1
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 ?
 
Old 01-03-2011, 07:39 AM   #7
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290
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
 
Old 01-03-2011, 08:02 AM   #8
drManhattan
Member
 
Registered: Aug 2010
Location: Warsaw, Poland
Distribution: Ubuntu 10.04 LTS on IBM Lenovo R61e, RHEL5-6,SLES10-11
Posts: 250

Original Poster
Rep: Reputation: 1
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 ?
 
Old 01-03-2011, 03:40 PM   #9
silvyus_06
Member
 
Registered: Oct 2010
Distribution: Ubuntu 10.04 , Linux Mint Debian Edition , Microsoft Windows 7
Posts: 386

Rep: Reputation: 49
i thought /var/spool/ is for pending actions so ..
 
Old 01-04-2011, 01:12 AM   #10
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290
Quote:
Originally Posted by drManhattan View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM
cron not working from crontab nor form /etc/cron/cron.d. What did SuSE change? JZL240I-U Suse/Novell 11 01-04-2007 01:57 AM
Can any one plz explain why/what for cron.d, cron.daily, cron.weekly etc are there. mavinashbabu Linux - Newbie 4 09-21-2006 01:50 PM
nothing in /usr/spool/cron/crontabs Furlinastis Slackware 5 06-04-2005 10:16 PM
Failed to lock file /var/spool/cron/root after 5 minutes teeno Linux - Software 0 09-24-2003 02:49 AM


All times are GMT -5. The time now is 02:27 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration