Link to home
Start Free TrialLog in
Avatar of eaglesprings
eaglesprings

asked on

Crontab error but Sh Script runs from command line

I need some suggestions regarding a script I am trying to write.  There are two problems .
Currently, I am writing a script in sh on HP- Unix box. I am able to run the program from the
command line but when I try to run the crontab file I get a exception error.  I ran chmod 777 on my
sh script file  and tried re-running it; no luck. The purpose of this script is to pull uptime information  &  I am needing this script to also pull  the Average of all the CPUS System average loads and output to a log file in this script and need some help on how to go about doing this.  I need all the suggestions I can get; This is what I have so far  

For Cron File
2 * * * 1-5  \home\mon\filetest.sh

My Script file reads

#!/bin/sh
echo "System Check `"  `uptime|awk ' >> filetest { print $1,$6,$7,$8,$9,$10, $11,$12 }'`"
print /n/n
Avatar of sunnycoder
sunnycoder
Flag of India image

Hi eaglesprings,

> echo "System Check `"  `uptime|awk ' >> filetest { print $1,$6,$7,$8,$9,$10, $11,$12 }'`"

Your quotes look suspect ....

echo "System Check "  `uptime|awk ' >> filetest { print $1,$6,$7,$8,$9,$10, $11,$12 } '  `

Sunnycoder
For Cron File
2 * * * 1-5  \home\mon\filetest.sh

Of course, the \'s should be  /'s - Was this just a typo when posting here?
Check your Unix mailfile - Any errors will be mailed yo the user who's crontab it was.


The script itself will generate errors under HPUX; Building on sunnycoders suggestion of sorting out "punctuation", the ">> filetest" should normally be at the end of the statement, e.g.
 echo "System Check "  `uptime|awk '{ print $1,$6,$7,$8,$9,$10, $11,$12 }'` >> filetest
ASKER CERTIFIED SOLUTION
Avatar of tfewster
tfewster
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of eaglesprings
eaglesprings

ASKER

I made some adjust with my script it still will only run at the command line and not via cron

I have aded some more stuff to my script


#!/usr/bin/sh
SRVNM=`uname -n`

echo "\nStatus Taken at: "`date`
print "\n\n"
echo  "$SRVNM System Check `uptime|awk '{ print $1,$6,$7,$8,$9,$10, $11,$12 }'`" >> filetest.sh
uptime |sed -e 's/.*: //' -e 's/ //g' | awk -F , ' { print "CPU 1= "$1 "\nCPU 2= " $2 "\nCPU 3= " $3 } '

echo "---------------------------------------------------------" >>filetest.sh

print "\n\n"


redid the cron file to read
2 * * * * /home/mon/filetest


And I keep getting these errors


SRVNM=`uname -n`
crontab: error on previous line; unexpected character found in line.
echo "\nStatus Taken at: "`date`
crontab: error on previous line; unexpected character found in line.
print "\n"
crontab: error on previous line; unexpected character found in line.
echo  "$SRVNM System Check `uptime|awk '{ print $1,$6,$7,$8,$9,$10, $11,$12 }'`" >> filetest
crontab: error on previous line; unexpected character found in line.
uptime |sed -e 's/.*: //' -e 's/ //g' | awk -F , ' { print "CPU 1= "$1 "\nCPU 2= " $2 "\nCPU 3= " $3 } '
crontab: error on previous line; unexpected character found in line.
echo "---------------------------------------------------------" >> filetest
crontab: error on previous line; unexpected character found in line.
print "\n\n"
crontab: error on previous line; unexpected character found in line.


when you use cron to run a script, cron doesn't have all the ENV vars setting as you
do in command line, so it is the ENV setting problem.

You said the script works in command line (eg for user fred), you can use fred's
login ENV to run the cron job:

eg:
su - fred -c /path-to script.

eg:
2 * * * * /usr/bin/su - fred -c "/home/mon/filetest"

or if the script works for root in commandline

2 * * * * /usr/bin/su - root -c "/home/mon/filetest"
can I still run this command if I do not have root access.
You maybe able to help me , I was browsing through and did see a prior post

I ran the command

who am i
mon

echo $SHELL

/usr/bin/ksh

ls -al .[a-z]*

-rw-rw-rw-   1 mon      pd          0 Aug  9 13:08 .addressbook
-rw-------   1 mon         pd       2285 Aug  9 13:08 .addressbook.lu
-r--r--r--   1 mon         pd       814 Aug  4 10:40 .cshrc
-r--r--r--   1 mon         pd        347 Aug  4 10:40 .exrc
-r--r--r--   1 mon         pd        341 Aug  4 10:40 .login
-rw-------   1 mon         pd       7261 Aug  9 21:23 .pine-debug1
-rw-------   1 mon         pd         7261 Aug  9 17:52 .pine-debug2
-rw-------   1 mon         pd         7209 Aug  9 15:53 .pine-debug3
-rw-------   1 mon         pd         7261 Aug  9 13:46 .pine-debug4
-rw-rw-rw-   1 mon         pd        11484 Aug  9 13:08 .pinerc
-r--r--r--   1 mon         pd            3297 Aug  4 15:57 .profile
-rw-------   1 mon         pd            35098 Sep  2 21:46 .sh_history

I tried adding this to my sh script

/home/mon/.profile
/home/mon/.sh_history
/home/mon/.exrc


Not sure if the last 2 lines are needed
but I received a error at the top but it still continues to run the script

/filetest[2]: /home/mon/.profile: Execute permission denied.
./filetest [3]: /home/mon/.sh_history: Execute permission denied.
./filetest[4]: /home/mon/.exrc: Execute permission denied.

Status Taken at: Thu Sep 2 21:51:50 CDT 2004


CPU 1= 0.32
CPU 2= 0.32
CPU 3= 0.37
i ran chmod 777 on my file and reran the crontab file and now I am getting a different error

crontab: can't open your crontab file.  


My script still runs at the command line but permissions error still show at the beginning of the script after it is run

filetest[2]: /home/mon/.profile: Execute permission denied.
./filetest [3]: /home/mon/.sh_history: Execute permission denied.
./filetest[4]: /home/mon/.exrc: Execute permission denied.

Status Taken at: Thu Sep 2 21:51:50 CDT 2004


CPU 1= 0.32
CPU 2= 0.32
CPU 3= 0.37
Ok, now I am back to the error I had before.

SRVNM=`uname -n`
crontab: error on previous line; unexpected character found in line.
echo "\nStatus Taken at: "`date`
crontab: error on previous line; unexpected character found in line.
print "\n"
crontab: error on previous line; unexpected character found in line.
echo  "$SRVNM System Check `uptime|awk '{ print $1,$6,$7,$8,$9,$10, $11,$12 }'`" >> filetest
crontab: error on previous line; unexpected character found in line.
uptime |sed -e 's/.*: //' -e 's/ //g' | awk -F , ' { print "CPU 1= "$1 "\nCPU 2= " $2 "\nCPU 3= " $3 } '
crontab: error on previous line; unexpected character found in line.
echo "---------------------------------------------------------" >> filetest
crontab: error on previous line; unexpected character found in line.
print "\n\n"
crontab: error on previous line; unexpected character found in line.


Can someone tell me if I am setting up my ENV path correctly

This is what I am putting in my sh script file

/home/mon/.profile
/home/mon/.sh_history
/home/mon/.exrc

ok I removed the other 2 below from script

/home/mon/.sh_history
/home/mon/.exrc
 

But I left below in my sh script.  I ran chmod 777 .profile & reran my script with this one only listed and also I added a Path and Export Path option, My script still runs but the permission error is gone now and I am still getting the same error with cron.  I think somethings missing from my script.
 

#!/usr/bin/sh
/home/mon/.profile

PATH=/sbin/sh:/usr/bin:/usr/newconfig/sbin:/usr/old/bin
export PATH


SRVNM=`uname -n`


echo "\nStatus Taken at: "`date`
print "\n"
echo  "$SRVNM System Check `uptime|awk '{ print $1,$6,$7,$8,$9,$10, $11,$12 }'`" >> filetest
uptime |sed -e 's/.*: //' -e 's/ //g'  | awk -F , ' { print "CPU 1= "$1 "\nCPU 2= " $2 "\nCPU 3= " $3 } '


echo "---------------------------------------------------------" >> filetest
Also I have a question about this since I added /home/mon/.profile
and reviewed my .profile file  through vi it shows I have no paths setup in my environment variables.
Would I need to specifiy a path one for cron is pulling info from my .profile file?