Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help With Cron and Shell Script Issue

 
Robert Nurse
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic