Problems with a .sh file in cron

Hi guys. This is my first post so bear with me.

I'm trying to get cron to run a shell script in my home directory (/home/server) that checks the temperature of my HDD. The script works fine, however I can't run it in cron. I've checked the syslog and I have only seen two errors:

  • Exited with error number 127
  • Exited with error number 1

I really need a simple guide showing me how to run a shell file from cron, as I've been searching for absolutely ages and I haven't found one tutorial on it.

Do I have to put .sh, or just the script name? I'm really confused... :wall:

And how do you launch your script in cron?
Most certainly if you execute your script in interactive mode runs fine, but not when executed by cron, you have to look at your environment!
Something set in your profile that cron ignores...

Well, I have heard a lot about the environments. I usually run the script using 'bash tempcheck.sh'. The script is:

#!/bin/bash

TEMP=$(/usr/sbin/hddtemp /dev/sda | cut -d: -f3 | sed 's/..$//' | sed 's/ //')
echo $(date '+DATE: %m/%d/%y  TIME:%H:%M:%S') - Temperature was: $TEMP >> templog.txt
if [ $TEMP -ge 50 ]; then
poweroff
fi

I also have the SHELL, MAILTO, HOME and PATH variables set in the crontab file:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/home/server

And I was trying to run the script like so:

*/20 * * * * root /home/server/tempcheck.sh

or...

*/20 * * * * root /home/server/tempcheck

The full error message should be in unix mail for the owner of the cron.

File extensions have no meaning in unix (beyond being documentary). You always specify the exact name of the script.

The script itself must be set executable.
Please post "ls -lad yourscriptname" so we can check the basics.

I can see some issues with the script where full pathnames will be required (e.g. poweroff and templog.txt).
Stongly advise you turn the "poweroff" line into an echo while you are testing the script.

I believe that $TEMP is a reserved variable. Maybe change it to ${TEMPERATURE} .

I do set all my files to 777 so my game server don't have problems with permissions etc. I have done ls on the script and it came back with the following:

-rwxrwxrwx 1 777 root 216 2011-11-22 17:14 tempcheck.sh

I will also change the $TEMP variable in the script. Where do I read/change the mail setting as I have no idea where it's going. Does it actually mean 'mail' (email) or is it just a log? Also, what full path would I specify with poweroff and hddtemp?

EDIT:
I've also amended the script and crontab file:

tempcheck.sh

#!/bin/bash

TEMPERATURE=$(/usr/sbin/hddtemp /dev/sda | cut -d: -f3 | sed 's/..$//' | sed 's/ //')
echo $(date '+DATE: %m/%d/%y  TIME:%H:%M:%S') - Temperature was: $TEMPERATURE >> templog.txt
if [ $TEMPERATURE -ge 50 ]; then
poweroff
fi

crontab file:

*/20 * * * * root /home/server/tempcheck >> /home/server/cronlog.txt

Do you get anything in templog.txt ? Did you find any errors in root mail?

I will check templog now, but, what is and how do I set the mail setting? I mean, what is root mail and where is it?

EDIT:
There's nothing in my cronlog.txt file or templog.txt, although in /var/log/syslog there are multiple errors like:

(server) CMD (root /home/server/tempcheck >> /home/server/cronlog.txt)
(CRON) error (grandchild #15590 failed with exit status 127)
(CRON) info (No MTA installed, discarding output)

Am I having eyesight trouble or what is that root before tempcheck ?

I would redirect STDOUT and STDERR in two separate files to see what is happening

1 Like

I think vbe has spotted it. Perhaps your Ubuntu has a normal unix crontab syntax which does not contain the username field (i.e. "root"). Is the crontab you are editing called "root". Are there any other entries in the file which could use to compare.

You can't beat actually posting the error messages verbatim. The 127 error means that cron can't find the file to be executed. The error suggests that it may be trying to execute a file called "root"? The MTA error means that cron can't find your Mail Transport Agent - maybe because you don't have email configured?

1 Like

I know that I don't have my mail configured because I've never had to. Where do I configure it? I know how to run servers and websites, not errors on linux :slight_smile: I'll remove the 'root' before the file and see what the syslog says. Also, the crontab I'm editing isn't called root, I just use 'crontab -e' to edit it. Oh, and should I put all these in the root's crontab (sudo crontab -e), so that it won't have a problem running the script, as I know that hddtemp and poweroff will need to be root.
By the way, what does verbatim mean?

---------- Post updated at 07:11 PM ---------- Previous update was at 07:01 PM ----------

Same thing. Here's the current syslog:

Nov 26 19:09:01 server CRON[25789]: (root) CMD (  [ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/lib/php5/ -depth -mindepth 1 -maxdepth 1 -type f -cmin +$(/usr/lib/php5/maxlifetime) ! -execdir fuser -s {} 2>/dev/null \; -delete)
Nov 26 19:09:01 server CRON[25790]: (server) CMD (/home/server/tempcheck >> /home/server/cronlog.txt)
Nov 26 19:09:01 server CRON[25788]: (CRON) error (grandchild #25790 failed with exit status 127)
Nov 26 19:09:01 server CRON[25788]: (CRON) info (No MTA installed, discarding output)
Nov 26 19:10:01 server CRON[25993]: (server) CMD (/home/server/tempcheck >> /home/server/cronlog.txt)
Nov 26 19:10:01 server CRON[25992]: (CRON) error (grandchild #25993 failed with exit status 127)
Nov 26 19:10:01 server CRON[25992]: (CRON) info (No MTA installed, discarding output)

---------- Post updated 27-11-11 at 03:21 PM ---------- Previous update was 26-11-11 at 07:11 PM ----------

I solved it!
Thanks for your help guys. I amended the script like so:


#!/bin/bash

TEMPERATURE=$(/usr/sbin/hddtemp /dev/sda | cut -d: -f3 | sed 's/..$//' | sed 's/ //')
echo $(date '+DATE: %m/%d/%y  TIME:%H:%M:%S') - Temperature was: $TEMPERATURE >> /home/server/templog.txt
if [ $TEMPERATURE -ge 50 ]; then
/sbin/poweroff
fi

and the crontab file like so:

*/5 * * * * /home/server/tempcheck.sh >> /home/server/templog.txt

Setting files owned by root to 777 is a disaster waiting to happen. NEVER do this.

If it is owned by root and 777, anybody can rewrite your file and you will run it, as root.

Imagine what this would do to your system:

dd if=/dev/zero of=/dev/sda

You really should not do this.

Verbatim is a word means "repeating the exact words that were used". When dealing with computer error messages you must report the whole message exactly as it appeared on the screen - complete with all punctuation, space characters, upper/lower case etc..

Glad it's working. The "man" pages for Ubuntu cite two different formats for a crontab file (without and with a username). I'm glad that yours is a normal "unix" format crontab.

Setting up basic unix mail should be straightforward. Source a "how to" manual for your Operating System. Internet mail is more difficult and will depend on your environment.