Meaningless Drivel is fun!*
The moose likes Linux / UNIX and the fly likes Help With Cron and Shell Script Issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Help With Cron and Shell Script Issue" Watch "Help With Cron and Shell Script Issue" New topic
Author

Help With Cron and Shell Script Issue

Robert Nurse
Greenhorn

Joined: Oct 25, 2006
Posts: 5
Hi All,

I have a (Bourne, SunOS ver. 5.10) shell script that performs a job and writes to a log file. I've created two functions in the shell that mainly write opening and closing statements to the same log file. When this script is run manually, it works just fine. But, when it's run through the cron, only the two functions get run. Non of the other commands are executed. The script uses full paths to all its resources.

Joe Hepp
Greenhorn

Joined: Feb 07, 2005
Posts: 12
You may need to make sure your shell script includes the user profile it is running under.

Add



before any other commands in your shell script.

This will setup your script's environment to be the same as the user it is running under.

You don't provide much information to work on however so this is just a guess.
Robert Nurse
Greenhorn

Joined: Oct 25, 2006
Posts: 5
Hi Joe,

Thanks for responding. I'm not to versed in Unix. Let me know what other information you need and I'll post it. So, you're saying at the top of my script, I should reference my profile? Would something like $HOME/.profile work?


Joe Hepp wrote:You may need to make sure your shell script includes the user profile it is running under.

Add



before any other commands in your shell script.

This will setup your script's environment to be the same as the user it is running under.

You don't provide much information to work on however so this is just a guess.


Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Thanks for responding. I'm not to versed in Unix. Let me know what other information you need and I'll post it. So, you're saying at the top of my script, I should reference my profile? Would something like $HOME/.profile work?

You should post your script (using code tags) and the cron information. You cannot use $HOME/.profile because the .profile sets $HOME (its a chicken and egg thing :-)
Robert Nurse
Greenhorn

Joined: Oct 25, 2006
Posts: 5
Tom Reilly wrote:
Thanks for responding. I'm not to versed in Unix. Let me know what other information you need and I'll post it. So, you're saying at the top of my script, I should reference my profile? Would something like $HOME/.profile work?

You should post your script (using code tags) and the cron information. You cannot use $HOME/.profile because the .profile sets $HOME (its a chicken and egg thing :-)


Thanks Tom. Here's (basically) my script. I left out specific names and addresses. I tried putting ~/.profile at the very top. But, it still doesn't run. All that runs are the calls to startLog and endLog:


function startLog
{
echo "****************" >> logfile.log

echo "Started: `date`" >> logfile.log

echo "****************" >> logfile.log

}

function endLog
{
echo "****************" >> logfile.log

echo "Ended: `date`" >> logfile.log

echo "****************" >> logfile.log

}


startLog;
if [ -f dat.dat ]; then
echo "Found data file... >> logfile.log
while read -a line
do
if [ ! -d /tempdir ]; then
echo "Temp folder missing" >> logfile.log
mkdir /tempdir
fi

echo "Creating remote archive for " ${line[0]} >> logfile.log
zip -rq /tempdir/${line[1]} ${line[2]}
if [ -f /tempdir/${line[1]}; then
sftp -b thebatch 192.192.1.1
fi
done < dat.dat
else
echo "No data file" >> logfile.log
fi

endLog;

Joe Hepp
Greenhorn

Joined: Feb 07, 2005
Posts: 12
Based on your script I would expect to see the line "No data file" in your logfile.

When using cron you need to remember that it "runs" in the home directory of the user whose cron it is running under.

As part of this you really should not use relative filepaths in your scripts.

The line


will be looking for the file dat.dat in the home directory of the user that has your script in it's crontab. The logfile will also be put into that directory.

We only put absolute filepaths in our scripts that run in cron so that they aren't tied to any specific user.

If your dat.dat file is in the user's home directory and you still aren't see anything other than "No data file" in your log file there may be a permission issue with your dat.dat file where the user running the script can't read it.
 
 
subject: Help With Cron and Shell Script Issue