[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1986
  • Last Modified:

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
0
eaglesprings
Asked:
eaglesprings
1 Solution
 
sunnycoderCommented:
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
0
 
tfewsterCommented:
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
0
 
tfewsterCommented:
Just checking back - A minor modification, probably more in keeping with the purpose of your script:
echo "System Check   `uptime|awk '{ print $1,$6,$7,$8,$9,$10, $11,$12 }'`" >> filetest
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
eaglespringsAuthor Commented:
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.


0
 
yuzhCommented:
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"
0
 
eaglespringsAuthor Commented:
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
0
 
eaglespringsAuthor Commented:
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
0
 
eaglespringsAuthor Commented:
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

0
 
eaglespringsAuthor Commented:
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
0
 
eaglespringsAuthor Commented:
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?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now