Solved

Crontab error but Sh Script runs from command line

Posted on 2004-09-02
10
1,934 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
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 20

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 20

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
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now