Script fails to run properly when run from CRONTAB

Hello all,

I'm trying to write a script to gather and send data and it works just fine at the bash command line, but when executing from CRON, it does not run properly.

My scripting skills are pretty limited and there's probably a better way, but as I said it works at the command line, but not from cron. Through troubleshooting I have found that this line

#Execute the 'report' portion of nbdeployutil and send output to a file.
/bin/echo `$mail_report` > /tmp/nbdeployutil_temp_rpt.out

isn't actually executing the $mail_report variable (which is just a string of text) and updating a temp file, so the rest of the script doesn't work. Any thoughts on why this isn't working through cron? I've checked the environment and it seems ok. I even tried to have cron run it with bash -c "scriptname.sh" and behavior did not change.

This is on a solaris 10 system, but I am also running the same script on SuSe 11 and it is behaving in exactly the same way.

Thanks for the help.

CRON runs scripts in a minimal environment, where most variables etc. are not set. Either set them in your script, or source your and/or the system's .profile s.

Here is cron env:

HOME=/
LOGNAME=root
PATH=/usr/sbin:/usr/bin
SHELL=/usr/bin/sh
TZ=UTC

Here is my env when I run the script successfully:

SHELL=/bin/sh
TERM=vt100
LD_LIBRARY_PATH=/etc/emc/rsa/cst/lib
PATH=/sbin:/bin:/usr/sbin:/usr/bin::/opt/sfw:/opt/sfw/bin:/usr/openv/netbackup/bin:/usr/openv/netbackup/bin/goodies:/usr/openv/netbackup/bin/admincmd:/usr/openv/volmgr/bin:/opt/EMCpower/bin:/etc/emc/bin:/etc
PWD=/
EDITOR=vi
HOME=/
SHLVL=2
LOGNAME=root
_=/bin/env

Look at the PATH variables. What be the contents of mail_report, and where is it set?

Path doesn't matter as I am using full path anyway. This is a script for NetBackup (enterprise backup application) to run a command to gather data. The command (nbdeployutil) is a two step command. You run it first for it to gather the data and then again to generate a report. I wish I could just run it once, but it doesn't have that feature. Here is my whole script.

#!/bin/bash

#This script runs the NBU NBDEPLOYUTIL command
#to generate capacity based output in the
#form of Front End (or Protected Data) Terrabytes per site.
#This script works with NBU version 6.5.x


usermail=(users email address goes here)
hostname=`hostname`
date=`date +%m%d%Y`

#Execute the NBU nbdeployutil command and send output to a file as this command
#is a two-part command. 
/usr/openv/netbackup/bin/admincmd/nbdeployutil --gather | /bin/grep nbdeployutil > /tmp/nbdeployutil_temp.out

#Set the mail_report variable by grepping out the command to run from the temp file
mail_report=`/bin/grep report /tmp/nbdeployutil_temp.out | /bin/cut -d':' -f2`

#Execute the 'report' portion of nbdeployutil and send output to a file.
/bin/echo `$mail_report` > /tmp/nbdeployutil_temp_rpt.out

#Pull the proper information out of the temp file.
mail_report=$(/bin/cat /tmp/nbdeployutil_temp_rpt.out | /bin/awk '{print $15}')

#Mail the file to the specified users.
(/bin/uuencode "$mail_report" "$mail_report") | /bin/mailx -s "$hostname NBDEPLOY $date" $usermail
exit 0

Again, it seems to fail to execute this line properly when run from cron.

/bin/echo `$mail_report` > /tmp/nbdeployutil_temp_rpt.out

The only thing special about this is that it is executing a variable. I'm open to better ways to do this if it works. If i knew perl that would probably be that better way ;(.

The evaluated string in backticks is run as a command.
Instead put it in quotes

echo "$mail_report"
1 Like

Are you sure the script posted does run correctly when invoked interactively? The backticks will try to execute the contents of mail_report, whatever it contains, and will probably fail, printing an error msg. Again: what is the contents of mail_report?

Yes, I do want the command executed - that is intended.
the mail_report variable will have output that looks like this:

nbdeployutil --report /usr/openv/var/global/reports/20140104_044802_servername

The command is a two part command. Step one is just to run
nbdeployutil --gather
That will run for a length of time and generate several different files in a specified directory. It also spits out some output that looks like this:

NetBackup Deployment Utility, version 6.5.6_EEB1_PET2269969_SET2269958
Gathering license deployment information...
  Discovered master server servername
  Output for servername at: /usr/openv/var/global/reports/20140105_002630_servername
Gather DONE
Execution time: 15 secs
To create a report for this master server, run the following:
  nbdeployutil --report /usr/openv/var/global/reports/20140105_002630_servername

I cut out the necessary part (the part that stays "run the following") and assign it to the mail_report variable and then execute it since it is the second step of the command to run to give me the final report.

This works interactively using the script, but not through cron.

You said that PATH doesn't matter. You said that when using cron, PATH is defined by:

PATH=/usr/sbin:/usr/bin

Is nbdeployutil in /usr/sbin or in /usr/bin ? Or is it in /usr/openv/netbackup/bin or one of its subdirectories that is in your PATH when you run it in an environment where it works successfully?

You said your script uses absolute pathnames for everything it executes. But, nbdeployutil is not an absolute pathname.

Hi Don, in this case I am using full paths in the script so the PATH environment variable doesn't come into play here. Further, the script works up to this line. I am able to tell this because the second temp file is never updated and the output file is never created. I think cron doesn't like this line

/bin/echo `$mail_report` > /tmp/nbdeployutil_temp_rpt.out

which works fine under the interactive shell.

No, no, no! The PATH environment variable absolutely DOES COME INTO PLAY here. Using back quotes (shown in red above) asks the shell being run by cron to execute the command found between those quotes. The shell has to find that command in one of the directories specified by $PATH to be able to execute it unless you use an absolute pathname. The nbdeployutil in the command you are asking the shell to execute:

nbdeployutil --report /usr/openv/var/global/reports/20140104_044802_servername

is not an absolute pathname and is not found in the directories listed in the PATH setting that is being used when you run this script under cron !

If you want this script to work under cron , you either need to change the way you set mail_report so it includes an absolute pathname for nbdeployutil or you need to set PATH so the shell being run by cron will be able to find nbdeployutil in one of the directories named in $PATH as it is set in cron 's environment.

Holy moly, Don! I hadn't even given that any thought (obviously)! I will give that a try and let you know, but I'm feeling pretty good that this is the issue!

Hi,

Could you please help me out in scheduling a mail through Unix command.
I am using "Corntab" and "at" but not able to schedule.

I use putty for Unix.

Thanks
Vinit Raj

---------- Post updated at 06:04 AM ---------- Previous update was at 06:02 AM ----------

Hi,

Could you please help me out in scheduling a mail through Unix command.
I am using "Corntab" and "at" but not able to schedule.

I use putty for Unix.

presently i am using this command

echo "Test Mail through scheduling for 0553" | mailx -s "Check 123" xyz@abc.com | at 0553 Jan 6

Thanks
Vinit Raj