# /etc/crontab and /usr/bin/mail



## TheGuyGuy (Jul 20, 2009)

Can someone tell me why this doesn't work please?

```
# /etc/crontab - root's crontab for FreeBSD
#
# $FreeBSD: src/etc/crontab,v 1.32 2002/11/22 16:13:39 tom Exp $
#
SHELL=/bin/sh
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin
HOME=/var/log
#
#minute hour    mday    month   wday    who     command
#
*/1     *       *       *       *       root    /usr/bin/mail -s 'hellooo' root < /home/curry/scripts/trash_file
#
```
(trash_file is just a dummy text file I made)

I've been banging my head up against the wall all day. I can run that exact commandline from the bash shell (as a normal user), from the csh (as root), and from the sh shell (as root), and it runs fine in all three cases. What's different about when crontab runs it?

Before you say something about the environment, I also copied the exact environment (line by line) from each of the three situations I described above to the crontab environment, and I also made a wrapper Perl script and changed the environment there as well to exactly match. I've even tried adding "@localhost" and "@localhost.localdomain" after the recipient, and changed recipient to just a normal user. Nothing has worked.

Any ideas? I really need to be able to mail things out as root. (This is because I need root privs to run some networking events, but I need to mail things out in case of trouble.)

Thanks,

TheGuyGuy


----------



## CmdLnKid (Jul 20, 2009)

Try this.


```
*/1 * * * * root /bin/cat /home/curry/scripts/trash_file | /usr/bin/mail -s 'hellooo' root
```


----------



## CmdLnKid (Jul 20, 2009)

On second thought skip that. The following will do.


```
*/1 * * * * root /bin/cat /home/curry/scripts/trash_file
```

It allready is mailed to root upon completion.


----------



## TheGuyGuy (Jul 20, 2009)

CmdLnKid said:
			
		

> On second thought skip that. The following will do.
> 
> */1 * * * * root /bin/cat /home/curry/scripts/trash_file
> 
> It allready is mailed to root upon completion.



So actually I want to run mail from within a perl script which does quite a lot of other stuffs (running networking commands, etc) and then have the script call /usr/bin/mail with the results, and only if they were bad. I just narrowed down the problem to the mail commandline itself, and the mail command doesn't work as /etc/crontab either from within the script or alone. That's my problem.

It doesn't give any errors at all, and the return level from the system() call in perl is zero (which means good). So it appears to have worked from the point of view of the shell and the script interpreter, but it doesn't. I'm wondering if it's confused about who I'm telling it to mail to under certain contexts.

TheGuyGuy


----------



## TheGuyGuy (Jul 20, 2009)

So I actually took your advice -- I just print everything to STDOUT instead of messaging it with mail now. It works fine. Thanks!

TheGuyGuy


----------



## DutchDaemon (Jul 20, 2009)

Leave the system crontab (/etc/crontab) alone, it may be replaced by an OS upgrade - use the user's own crontab (crontab -e as the user). See crontab(1).


```
*/1 * * * *
```
This is rather pointless, just use 
	
	



```
* * * * *
```


----------



## DutchDaemon (Jul 20, 2009)

TheGuyGuy said:
			
		

> .. and only if they were bad ..



Use [cmd=]command >/dev/null[/cmd] if you _only_ want error output. 
Use [cmd=]command 2>/dev/null[/cmd] if you _don't_ want error output. 
Use [cmd=]command > dev/null 2>&1[/cmd] if you don't want _any_ output.


----------

