Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

CentOS 5.5 x86_64

Trying to e-mail a periodic system update with the output of a system temperatures command:

hpasmcli -s "show temp"

Outputs something like:

Sensor   Location              Temp       Threshold
------   --------              ----       ---------
#1        AMBIENT              21C/69F    42C/107F
...

So, I script this:

#!/bin/bash
hpasmcli -s "show temp" > /tmp/monitorTemp.log &
wait
mail -s "temperature" me@mydomain.com < /tmp/monitorTemp.log
rm -f /tmp/monitorTemp.log

Set to run every hour in cron

* */1 * * * /root/monitorTemp.sh

I receive the e-mail, but it's empty. However, if I simply run it from the command line ./monitorTemp.sh it sends me the e-mail with command output included!

What am I missing?

share|improve this question

migrated from superuser.com Nov 17 '10 at 7:03

This question came from our site for computer enthusiasts and power users.

    
You can eliminate the temporary file: /path/to/hpasmcli -s "show temp" | mail -s "temperature" me@mydomain.com - the whole script on one line (plus the shebang). –  Dennis Williamson Nov 17 '10 at 4:17
add comment

2 Answers

Try changing this:

hpasmcli -s "show temp" > /tmp/monitorTemp.log &
wait

to:

cd /path/to/hpasmcli
./hpasmcli -s "show temp" > /tmp/monitorTemp.log

When running from cron, you don't have the same PATH as when running logged in normally, that's something worth to keeping in mind. You can also change the PATH before

PATH=$PATH:/path/to/hpasmcli
hpasmcli -s "show temp" > /tmp/monitorTemp.log

or something similar. You can get the /path/to/hpasmcli by doing which hpasmcli from your regular command prompt.

EDIT: As Dennis notes in the comments, for simple scripts like this one, there's no need to cd or setup PATH, this would be just fine:

/path/to/hpasmcli -s "show temp" > /tmp/monitorTemp.log
share|improve this answer
    
Bingo, thank you! –  gr33d Nov 16 '10 at 22:03
    
Cool, glad that was the issue! –  icyrock.com Nov 16 '10 at 22:06
    
Yes, is there any $PATH at all for cron? It is another user? –  gr33d Nov 16 '10 at 22:08
    
AFAIK there is some basic path, but it's something like /bin:/usr/bin or something along these lines. –  icyrock.com Nov 16 '10 at 22:33
1  
There's no need to cd, just do /path/to/hpasmcli -s "show temp" .... For a simple script, I wouldn't change $PATH either. –  Dennis Williamson Nov 17 '10 at 4:14
show 1 more comment

You can also set a PATH explicitly in your crontab.

eg. PATH=/usr/bin:/usr/sbin:/path/to/executable:. * */1 * * * /root/monitorTemp.sh

But I agree that explicitly setting path in scripts is probably a better solution for one offs.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.