Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why doesn't script work with CRONTAB?

Status
Not open for further replies.

Stinney

IS-IT--Management
Nov 29, 2004
2,031
US

If I manually call up the following script it runs fine:

DAYRUN=`date +%Y%m%d`
TIMERUN=`date +%H%M`
print $DAYRUN$TIMERUN > DAYRUN
who > wholog
cat wholog >> DAYRUN
cat DAYRUN >> /export/home/cms/logs/wholog$DAYRUN.txt

It creates a file wholog20071127.txt (if I run it today) with the first line being the date and time run, then a list of who is curently logged into the system. If I run it again, say 5 minutes later, it will append to the end of the file and will have a line showing the date and time (5 min later) and a list of who is logged in.

If I run this using crontab, it doesn't put the date and time stamp at the begining of the new list, it just appends the current who log to the end of the file.

Here is the crontab file:

0,30 * * * * /export/home/cms/loggedin.sh

Any ideas?

- Stinney

Favorite all too common vendor responses: "We've never seen this issue before." AND "No one's ever wanted to use it like that before.
 
Stinney - it's maybe a PATH issue - set the PATH which is current when you run it interactively as the first line of your script and hopefully it will work. It's also usually a good idea to include full paths to things like date if running from cron.

HTH.

I want to be good, is that not enough?
 

That was it! Thanks a million!

- Stinney

Favorite all too common vendor responses: "We've never seen this issue before." AND "No one's ever wanted to use it like that before.
 
the print command is an alias for "echo" ? change that to use the echo command and it should work.
(everything else seems to be in /bin so pathing might not be the issue.

eugene
 

I've been so busy, I haven't had time to get back to this post.

elgrandeperro is right. I went back to the logs and I still wasn't getting the date/time stamp in the output. I changed my script to use echo instead and it has been working since. I changed my script to be:

DAYRUN=`date +%Y%m%d`
TIMERUN=`date +%H%M`
echo $DAYRUN$TIMERUN > DAYRUN
who > wholog
cat wholog >> DAYRUN
cat DAYRUN >> /export/home/cms/logs/wholog$DAYRUN.txt
rm DAYRUN
rm wholog

- Stinney

Favorite all too common vendor responses: "We've never seen this issue before." AND "No one's ever wanted to use it like that before.
 
print is Korn shell's internal version of echo. An alternative solution to that would have been to add a shebang line to the beginning of the script to ensure it ran using the shell it was designed for, e.g.

[tt]#!/usr/bin/ksh

DAYRUN=`date +%Y%m%d`
TIMERUN=`date +%H%M`
...[/tt]

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top