• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

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

0
David Aldridge
Asked:
David Aldridge
  • 18
  • 7
  • 5
  • +1
3 Solutions
 
David AldridgeAuthor Commented:
BTW, I didn't write this.. just trying to troubleshoot it for a guy.  :->
0
 
TintinCommented:
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.
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Gerwin Jansen, EE MVETopic Advisor Commented:
>>but it bombs
Can you post the error message you are getting?
0
 
David AldridgeAuthor Commented:
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.
0
 
David AldridgeAuthor Commented:
Well, that didn't work.  I even tried sourcing the .profile at the beginning with  . ~/.profile
0
 
TintinCommented:
Try adding the -v flag on mailx to get verbose output, ie:

cat /opt/VRTSsfmcs/Backup/VOM_Backup_$now/$now.log | mailx -vs "$MAILSTRING" $MAILLIST
0
 
omarfaridCommented:
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
0
 
David AldridgeAuthor Commented:
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.
0
 
omarfaridCommented:
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
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
Append to the logfile instead of overwriting it every time (>> iso >) and put the logfile where the script itself is (best practice):

16 17  * * * /opt/VRTSsfmcs/Backup/VOM_Backup.david.ksh >> /opt/VRTSsfmcs/Backup/VOM_Backup.david.log 2>&1
0
 
omarfaridCommented:
The aim is to get the errors if there is any error and for that it is temporary redirect to know the cause.
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
@omarfarid - just sharing some best practices here ;)
0
 
omarfaridCommented:
Thanks :)
0
 
David AldridgeAuthor Commented:
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
0
 
omarfaridCommented:
ok, change your crontab to:

4 6  * * * /bin/ksh -v /opt/VRTSsfmcs/Backup/VOM_Backup.david.ksh >> /tmp/mylog 2>&1

The above will force creating log while executing the script
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
>> 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.
0
 
David AldridgeAuthor Commented:
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)
0
 
David AldridgeAuthor Commented:
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
0
 
omarfaridCommented:
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.
0
 
David AldridgeAuthor Commented:
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
0
 
David AldridgeAuthor Commented:
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.
0
 
David AldridgeAuthor Commented:
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.
0
 
omarfaridCommented:
Are you sure that all necessary env variables are set?
Are you sourcing .profile file?
0
 
David AldridgeAuthor Commented:
I put the full path to all execuatables, set the path variable, AND sourced . /root/.profile.  Still doesn't work.
0
 
TintinCommented:
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.
0
 
David AldridgeAuthor Commented:
It's finishing.  All the backups are done.  The logs are there, and it runs just fine from the command line.
0
 
David AldridgeAuthor Commented:
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.
0
 
TintinCommented:
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.
0
 
David AldridgeAuthor Commented:
Ok, I'll give that a shot
0
 
David AldridgeAuthor Commented:
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
0
 
David AldridgeAuthor Commented:
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
0
 
David AldridgeAuthor Commented:
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.  :-/
0
 
David AldridgeAuthor Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 18
  • 7
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now