Unix/Linux Go Back    


Ubuntu Ubuntu is a complete desktop Linux operating system, freely available with both community and professional support.

Problems with a .sh file in cron

Ubuntu


Tags
cron, execution, linux, ubuntu

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-26-2011   -   Original Discussion by Rautoner
Rautoner Rautoner is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 3 December 2011, 6:42 AM EST
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
Error 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... Linux
Sponsored Links
    #2  
Old Unix and Linux 11-26-2011   -   Original Discussion by Rautoner
vbe's Unix or Linux Image
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 18 November 2017, 9:50 AM EST
Location: Switzerland - GE
Posts: 6,479
Thanks: 289
Thanked 553 Times in 515 Posts
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...
Sponsored Links
    #3  
Old Unix and Linux 11-26-2011   -   Original Discussion by Rautoner
Rautoner Rautoner is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 3 December 2011, 6:42 AM EST
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
Well, I have heard a lot about the environments. I usually run the script using 'bash tempcheck.sh'. The script is:


Code:
#!/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:


Code:
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:

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

or...

Code:
*/20 * * * * root /home/server/tempcheck

    #4  
Old Unix and Linux 11-26-2011   -   Original Discussion by Rautoner
methyl methyl is offline
Registered User
 
Join Date: Mar 2008
Last Activity: 8 June 2016, 9:58 PM EDT
Posts: 6,402
Thanks: 288
Thanked 676 Times in 645 Posts
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} .

Last edited by methyl; 11-26-2011 at 08:31 AM..
Sponsored Links
    #5  
Old Unix and Linux 11-26-2011   -   Original Discussion by Rautoner
Rautoner Rautoner is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 3 December 2011, 6:42 AM EST
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
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:

Code:
-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

Code:
#!/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:

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

Sponsored Links
    #6  
Old Unix and Linux 11-26-2011   -   Original Discussion by Rautoner
methyl methyl is offline
Registered User
 
Join Date: Mar 2008
Last Activity: 8 June 2016, 9:58 PM EDT
Posts: 6,402
Thanks: 288
Thanked 676 Times in 645 Posts
Do you get anything in templog.txt ? Did you find any errors in root mail?
Sponsored Links
    #7  
Old Unix and Linux 11-26-2011   -   Original Discussion by Rautoner
Rautoner Rautoner is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 3 December 2011, 6:42 AM EST
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by methyl View Post
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:

Code:
(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)


Last edited by Rautoner; 11-26-2011 at 11:37 AM..
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Execution Problems with Cron chetancrsp18 Shell Programming and Scripting 1 08-08-2011 11:29 AM
Execution Problems with Cron dhivyasuresh UNIX and Linux Applications 1 03-22-2011 03:32 AM
execution problems with cron causalmodi777 Programming 1 11-25-2010 08:47 PM
cron problems scotty UNIX for Dummies Questions & Answers 3 02-08-2002 11:46 AM
Cron problems warrend UNIX for Advanced & Expert Users 6 12-07-2001 12:39 PM



All times are GMT -4. The time now is 05:12 PM.