Link to home
Start Free TrialLog in
Avatar of David Aldridge
David AldridgeFlag for United States of America

asked on

script won't run in cron but runs on the command line

This script runs just fine when I run it from the command line, but it bombs on the mailx piece when I try to run it from a cron job.  Any ideas?

Thanks in advance,
David


#!/bin/ksh

now=current
set -x
LOGDIR=/opt/VRTSsfmcs/Backup
THIS_SCRIPT=`basename $0`
LOGFILE=$THIS_SCRIPT_$now.log
MAILLIST="david@itmemphis.com"
MAILSTRING="lnxutil VOM Backup Result $now"

function logger {

        mkdir /opt/VRTSsfmcs/Backup/VOM_Backup_$now
        echo "Creating Logs"

                echo "Creating Logfile"
                mkdir -p $LOGDIR
                exec 1> $LOGDIR/$LOGFILE 2>&1
                mv $LOGDIR/$LOGFILE /opt/VRTSsfmcs/Backup/VOM_Backup_$now
}

function clean {
        echo "Removing old file"
        rm -r  /opt/VRTSsfmcs/Backup/VOM_Backup_last
        echo "Changing file name"
        mv  /opt/VRTSsfmcs/Backup/VOM_Backup_$now  /opt/VRTSsfmcs/Backup/VOM_Backup_last

        echo "Clean Complete"
}

function Do_Backup {
        echo "Running the backup process...."

        /opt/VRTSsfmcs/config/adm/vom_bkup.pl --backup /opt/VRTSsfmcs/Backup/VOM_Backup_$now

        echo "Backup Complete"
}

function mailer {
        echo "Sending Email to david@itmemphis.com"
        cat /opt/VRTSsfmcs/Backup/VOM_Backup_$now/$now.log | mailx -s "$MAILSTRING" $MAILLIST
}

clean
logger
Do_Backup
mailer

Open in new window

Avatar of David Aldridge
David Aldridge
Flag of United States of America image

ASKER

BTW, I didn't write this.. just trying to troubleshoot it for a guy.  :->
Avatar of Tintin
Tintin

What user is the cronjob running as?

What user are you running it from the command line as?

What specific error are you getting?

Remember that cronjobs run in a limited environment (eg: PATH and other environment variables), although I can't see anything specifically that should affect it running as a cronjob.
Add full path to the mailx command.

Find where your mailx is like this: which mailx

So if your mailx path is /usr/bin/mailx then change the mailer line accordingly:

cat /opt/VRTSsfmcs/Backup/VOM_Backup_$now/$now.log | /usr/bin/mailx -s "$MAILSTRING" $MAILLIST
>>but it bombs
Can you post the error message you are getting?
It runs as root.  I'm not getting an error message, it just doesn't send the email.  It runs without a hitch from the command line though.  I'll give the idea of calling the full path to mailx a try.
Well, that didn't work.  I even tried sourcing the .profile at the beginning with  . ~/.profile
SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
How the crontab job is run? You may redirect the script std out & err to log file and see errors in the log file.

Also, check your email on the server or crontab error logs for errors
16 17  * * * /opt/VRTSsfmcs/Backup/VOM_Backup.david.ksh > /dev/null 2>&1

Of course I move the time to when I want it to run to test it.
Can you redirect to log file:

16 17  * * * /opt/VRTSsfmcs/Backup/VOM_Backup.david.ksh > /tmp/mylog 2>&1

and check the log file?

Also, what are the perms of /opt/VRTSsfmcs/Backup/VOM_Backup.david.ksh? Is the file executable (x perm)? If not then use

chmod +r /opt/VRTSsfmcs/Backup/VOM_Backup.david.ksh

to make it executable.

Also, make sure that:

- use full path names in your script for files and folders
- redirect stdout & stderr to files
- When using commands, either have PATH set or use full path names
- If some commands require input from stdin then redirect input as well
- script files you are calling have the execute perm
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The aim is to get the errors if there is any error and for that it is temporary redirect to know the cause.
@omarfarid - just sharing some best practices here ;)
Thanks :)
Ok, changed it to this and it didn't even create mylog.
4 6  * * * /opt/VRTSsfmcs/Backup/VOM_Backup.david.ksh >> /tmp/mylog 2>&1
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> It runs as root
Look in logging to see what is happening:

tail -100 /var/log/syslog | grep CRON

or

tail -100 /var/log/cron.log

You can post any errors you get.
No errors..
lnxutil:/var/log# tail -100 cron|grep VOM
Dec 10 06:04:01 lnxutil CROND[1880]: (root) CMD (/opt/VRTSsfmcs/Backup/VOM_Backup.david.ksh >> /tmp/mylog 2>&1)
Dec 10 06:54:01 lnxutil CROND[17529]: (root) CMD (/opt/VRTSsfmcs/Backup/VOM_Backup.david.ksh >> /tmp/mylog 2>&1)
I'm going to rewrite this script.  I don't like the variables and functions that aren't needed and see what happens.  I'll post the new code and results here.  No matter what, I'll reward you guys.  I appreciate the help.

David
The logs you posted show that the job runs, but did you try to call the script as I suggested with /bin/ksh -v

This will show the step by step execution of the script and you may catch the problem.
Ok, I rewrote this as easily as I could... here's what it looks like now.  It still does the same thing.  It works perfectly from the command line, but won't mail the results when it runs from cron.

#!/bin/ksh -v

set -x
MAILTO="david@itmemphis.com"
MAILSTRING="lnxutil VOM Backup Result current"

# Clean up
rm -r /opt/VRTSsfmcs/Backup/VOM_Backup_last
mv /opt/VRTSsfmcs/Backup/VOM_Backup_current /opt/VRTSsfmcs/Backup/VOM_Backup_last

#Logger
mv /opt/VRTSsmfcs/Backup/VOM_Backup_current.log /opt/VRTSsfmcs/Backup/VOM_Backup_last.log
mkdir -p /opt/VRTSsmfcs/Backup/VOM_Backup_current
exec 1> /opt/VRTSsmfcs/Backup/VOM_Backup_current.log

#Backup
/opt/VRTSsfmcs/config/adm/vom_bkup.pl --backup /opt/VRTSsfmcs/Backup/VOM_Backup_current

#Mail
echo "VOM Results"|/bin/mailx -v -s "$MAILSTRING" $MAILTO < /opt/VRTSsmfcs/Backup/VOM_Backup_current.log

Open in new window


Here's the crontab entry:
9 9  * * * /opt/VRTSsfmcs/Backup/VOM_Backup_test.ksh >> /tmp/mylog 2>&1

Here's the contents of mylog:
lnxutil:/tmp# cat mylog
#!/bin/ksh -v

set -x
MAILTO="david@itmemphis.com"
+ MAILTO=david@itmemphis.com
MAILSTRING="lnxutil VOM Backup Result current"

+ MAILSTRING='lnxutil VOM Backup Result current'
# Clean up
rm -r /opt/VRTSsfmcs/Backup/VOM_Backup_last
+ rm -r /opt/VRTSsfmcs/Backup/VOM_Backup_last
mv /opt/VRTSsfmcs/Backup/VOM_Backup_current /opt/VRTSsfmcs/Backup/VOM_Backup_last

+ mv /opt/VRTSsfmcs/Backup/VOM_Backup_current /opt/VRTSsfmcs/Backup/VOM_Backup_last
#Logger
mv /opt/VRTSsmfcs/Backup/VOM_Backup_current.log /opt/VRTSsfmcs/Backup/VOM_Backup_last.log
+ mv /opt/VRTSsmfcs/Backup/VOM_Backup_current.log /opt/VRTSsfmcs/Backup/VOM_Backup_last.log
mkdir -p /opt/VRTSsmfcs/Backup/VOM_Backup_current
+ mkdir -p /opt/VRTSsmfcs/Backup/VOM_Backup_current
exec 1> /opt/VRTSsmfcs/Backup/VOM_Backup_current.log

+ exec
+ 1> /opt/VRTSsmfcs/Backup/VOM_Backup_current.log
#Backup
/opt/VRTSsfmcs/config/adm/vom_bkup.pl --backup /opt/VRTSsfmcs/Backup/VOM_Backup_current

+ /opt/VRTSsfmcs/config/adm/vom_bkup.pl --backup /opt/VRTSsfmcs/Backup/VOM_Backup_current
Hrmm.. looking at the perl script, it ends with either exit 0 or exit 1.  I changed it to return 0 or return 1 and we'll see what that does.
Nope, that didn't work.
[Thu Dec 11 09:33:17 2014] vom_bkup.pl: Can't return outside a subroutine at /opt/VRTSsfmcs/config/adm/vom_bkup.pl line 3108.
Are you sure that all necessary env variables are set?
Are you sourcing .profile file?
I put the full path to all execuatables, set the path variable, AND sourced . /root/.profile.  Still doesn't work.
If you aren't seeing the mailx line in the log, that would indicate that the Perl script is either taking a very long time to finish, or never finishing.
It's finishing.  All the backups are done.  The logs are there, and it runs just fine from the command line.
Here's the contents of the log it's trying to email:

lnxutil:/opt/VRTSsfmcs/Backup# cat /opt/VRTSsmfcs/Backup/VOM_Backup_current.log
Stop SNMP Trap Service.
Stop xprtld Service.
Stop Web Service.
Stop AT Service.
Stop DB Service.
Backup files to /opt/VRTSsfmcs/Backup/VOM_Backup_current/vom_backup_linux_6.0.0.0.tar.gz.
Backup files completed.
Start DB Service.
Start AT Service.
Start Web Service.
Start xprtld Service.
Start SNMP Trap Service.
Veritas Operations Manager backups files successfully.
For detail information, please see vom_bkup.backup_log located in /opt/VRTSsfmcs/Backup/VOM_Backup_current directory.
Just to verify, the very last line in mylog is:

#Backup
/opt/VRTSsfmcs/config/adm/vom_bkup.pl --backup /opt/VRTSsfmcs/Backup/VOM_Backup_current

+ /opt/VRTSsfmcs/config/adm/vom_bkup.pl --backup /opt/VRTSsfmcs/Backup/VOM_Backup_current

You confirmed that the cronjob completed when you looked at the log?

Perhaps you could add a:

echo "Backup complete"

after where it calls the Perl script and see if you get that output in mylog.

If not, it indicates the script isn't completing.
Ok, I'll give that a shot
Ok, Tintin, we're making some progress.  The perl script calls it's own version of perl which is 5.8.8.  When I use the version of perl that's on this version of Linux, 5.10, the script bombs because it's still trying to use the 5.8.8 libs.  How can I make it use the libs for 5.10?  Here's the error:

#Backup
/usr/bin/perl /opt/VRTSsfmcs/config/adm/vom_bkup.pl --backup /opt/VRTSsfmcs/Backup/VOM_Backup_current
+ /usr/bin/perl /opt/VRTSsfmcs/config/adm/vom_bkup.pl --backup /opt/VRTSsfmcs/Backup/VOM_Backup_current
Perl lib version (v5.8.8) doesn't match executable version (v5.10.1) at /opt/VRTSsfmh/lib/modules/Config.pm line 37.
Compilation failed in require at /opt/VRTSsfmh/lib/modules/lib.pm line 3.
BEGIN failed--compilation aborted at /opt/VRTSsfmh/lib/modules/lib.pm line 3.
Compilation failed in require at /opt/VRTSsfmcs/config/adm/vom_bkup.pl line 24.
BEGIN failed--compilation aborted at /opt/VRTSsfmcs/config/adm/vom_bkup.pl line 24.
print "Backup complete" >> /tmp/mylog
Ok, tried this and didn't get an error, but the backup didn't run so I got a blank email.

#Backup
/usr/bin/perl -l /usr/share/perl5 /opt/VRTSsfmcs/config/adm/vom_bkup.pl --backup /opt/VRTSsfmcs/Backup/VOM_Backup_current
+ /usr/bin/perl -l /usr/share/perl5 /opt/VRTSsfmcs/config/adm/vom_bkup.pl --backup /opt/VRTSsfmcs/Backup/VOM_Backup_current
print "Backup complete" >> /tmp/mylog
Looking at the perl script, I'm going to have to use their version because they have a lot of their own modules they are calling.  I just have to figure out why it won't run in cron now.  :-/
Ok, so I found a way to make it work, although I STILL don't know why it won't mail from the original script.  I added these variables to the perl script:

my $MAILLIST = 'david@itmemphis.com';
my $MAILSTRING = 'lnxutil VOM Backup Result current';


Then added this line to the end of the perl script:
system ("/bin/cat /opt/VRTSsmfcs/Backup/VOM_Backup_current.log | /bin/mailx -s $MAILSTRING $MAILLIST");

Not polished, but it works.  I'll split the points between you guys.  Thanks for the effort!

David