# /etc/crontab or crontab -e as root



## wonslung (May 19, 2009)

another stupid questions i'm sure, but i just checked my mail on root and wow, i had like a million messages from crontab due to vnstat....i THOUGHT i had followed the instructions by posting 

```
*/5 *   * * *   root    if [ -x /usr/local/bin/vnstat ] && [ `ls -l /var/db/vnstat/ | wc -l` -ge 1 ]; then /usr/local/bin/vnstat -u; fi
```
into roots crontab but i did it using su
crontab -e

now i check the mail and see a TON of messages like this
root: not found
root: not found
root: not found


so i did a search and found out about /etc/crontab....
so...should i ONLY be using /etc/crontab..i'm totally confused now
is /etc/crontab a relic of days past or is the ability to do crontab -e as root messed up? or are they both correct and i'm getting the error due to something else?


----------



## DutchDaemon (May 19, 2009)

It's not cast in stone, but I always leave /etc/crontab (the system crontab) well alone, and only use a user's crontab (crontab -e as the user, e.g. root) to add other stuff/scripts/commands. 

The difference is that /etc/crontab  requires an extra field to designate the user to run as, whereas users' crontabs do not have that field (for obvious reasons).


----------



## phoenix (May 19, 2009)

/etc/crontab uses the following fields:

*minute, hour, day, month, weekday, user, command*


User crontabs only have the following fields:

*minute, hour, day, month, weekday, command*


See the difference?    By adding the "root" to your cronjob, you are telling it to run the command 
	
	



```
root    if [ -x /usr/local/bin/vnstat ] && [ `ls -l /var/db/vnstat/ | wc -l` -ge 1 ]; then /usr/local/bin/vnstat -u; fi
```
 which obviously doesn't exist (there's no such binary as "root").  Remove the word "root" from your command and it will work correctly.

My "rule of thumb" is that /etc/crontab is for the OS to look after itself and should not be touched by users.  User crontabs should be used to run user scripts, commands, etc.


----------



## DutchDaemon (May 19, 2009)

It's like I have a twin!

[/letterman]


----------



## wonslung (May 19, 2009)

thanks, i have got to say this forum has been a god send....without it i would be lost (i figured out that removing the root part, what threw me was that the file in the documents section had the format for the /etc/crontab while i'd only used the other)

i GUESS i should have READ the crontab setting instead of just pasting it in....but it wouldnt' ahve occurred to me that there was two crontabs.

i GUESS it makes pretty good sense when you think about it.

a lot of stuff in bsd is different but after someone explains it you get an "AHA" moment where you're like wow, of course that's why.....anything else would be silly.


----------



## SirDice (May 19, 2009)

If you use the same account on your machine it might be worthwhile to set an alias. Just edit /etc/aliases and point root's mail to your user account. If you make any changes to /etc/aliases don't forget to run *newaliases* afterward.


----------



## gnemmi (May 20, 2009)

Yup .. that's one of the first things I do on every install .. remap root's mail account ...

Regarding the /etc/crontab and `% crontab -e` issue, according to Dru Lavigne:



> So far, we've looked at the system crontab, which should be left as is. Now we want to look at making your own crontab file to put in the commands that you want cron to execute. By default on a FreeBSD system, any user can create his own crontab file. These crontabs will be stored in /var/cron/tabs. ...



You'll find this article really interesting: Getting Cron to Do Our Bidding.

Hope that helps 

Regards


----------



## wonslung (May 21, 2009)

yes, i did that


----------

