# Cronjob problem



## bdj (Oct 15, 2010)

I've been trying for hours to get a cronjob to work but I still run into problems and i'm getting a bit frustated. Maybe someone can help me.

I have put 
	
	



```
* * * * * root /usr/scripts/load.cron.sh
```
 in /etc/crontab

Now I get emails every minute with the following: 
	
	



```
/usr/scripts/load.cron.sh: Permission denied
```
So I thought I should change permissions to get it working. I change permission ls -l gives

```
-rwxr--r--  1 root  wheel   518 Oct 15 08:06 load.cron.sh*
```

Now I get emails 
	
	



```
/usr/scripts/load.cron.sh: not found
```

I'm a newbee, i've been trying but this is just to much 

Thanks


----------



## jalla (Oct 15, 2010)

Make sure the first line of load.cron.sh is

```
#!/bin/sh
```
If that is not your problem, show us what load.cron.sh looks like


----------



## SirDice (Oct 15, 2010)

Don't use /etc/crontab for this. Only system related stuff is supposed to be in there.

If you want to add your own things use `# crontab -e`
(Note that you don't need to add the username then)


----------



## bdj (Oct 15, 2010)

@SirDice: I changed it back to what you advices

@jalla: I changed /bin/bash to /bin/sh and now it seems to be working. 

The cronjob output is still wrong but I think that's another problem so i'm going to try to figure that out for myself.


```
#!/bin/sh
max_loadavge=1.0

loadavg=$(uptime | awk '{gsub(",",""); print $11}')

#echo "Load Average:$loadavg on `date`" >> /var/log/load.log

SUBJECT="`hostname` LOAD AVERAGE ALERT $loadavg(>$max_loadavge)"

EMAIL1="email@me.me"

ETEXT1="Load Average:$loadavg on `date`\n\n"
ETEXT2="USER     PID %CPU %MEM   VSZ   RSS  TT  STAT STARTED      TIME COMMAND\n"

echo -e "$ETEXT1$ETEXT2"

if [ $(echo "$max_loadavge < $loadavg"|bc) -eq 1 ]
then
echo -e "$ETEXT1$ETEXT2" | mail -s "$SUBJECT" "$EMAIL1"

fi

exit
```


----------



## bdj (Oct 15, 2010)

Oke,

It script is working, I get a script email when load is over 1. But now the cronjob keeps sending me emails every minute.


----------



## SirDice (Oct 15, 2010)

If a cronjob produces output it will be emailed. So if you don't want to receive those emails make sure the script doesn't produce any output.


----------



## bdj (Oct 15, 2010)

Sirdice, 
I think I'm not really clear, excuse me.
I get emails every minute with the subject 
	
	



```
Cron <root@admin> /usr/scripts/load.cron.sh
```
when the load is higher > 1, I get: 
	
	



```
xxxx.yyyy.nl LOAD AVERAGE ALERT 2.01(>1.0)
```

I don't need the first messages.


----------



## SirDice (Oct 15, 2010)

I don't think you understood me.

If a cronjob produces output that output will get mailed. 
It has nothing to do with the mail functionality inside your script.


----------



## graudeejs (Oct 15, 2010)

redirect both stdout and stderr to /dev/null


----------



## bdj (Oct 15, 2010)

Sirdice, I think I get the point now, if I run it by hand the script produces an output even though the load is below 1. I think I have a script error.


----------



## bdj (Oct 16, 2010)

Thanks you killasmurf and SirDice, my problem is solved


----------

