Solved

Crontab error but Sh Script runs from command line

Posted on 2004-09-02
10
1,950 Views
Last Modified: 2013-12-26
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
Comment
Question by:eaglesprings
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11961854
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
 
LVL 21

Expert Comment

by:tfewster
ID: 11964755
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
 
LVL 21

Accepted Solution

by:
tfewster earned 500 total points
ID: 11965148
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
Technology Partners: 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!

 

Author Comment

by:eaglesprings
ID: 11969204
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
 
LVL 38

Expert Comment

by:yuzh
ID: 11969667
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
 

Author Comment

by:eaglesprings
ID: 11970011
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
 

Author Comment

by:eaglesprings
ID: 11970204
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
 

Author Comment

by:eaglesprings
ID: 11970261
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
 

Author Comment

by:eaglesprings
ID: 11970393
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
 

Author Comment

by:eaglesprings
ID: 11970509
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

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Show shut-down message as Windows 8 shuts down. 9 109
Define unique primary key 9 99
Prevent this page from creating additional dialogs. 3 777
countEvens challenge 2 117
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question