David Aldridge
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
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
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.
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_$no w/$now.log | /usr/bin/mailx -s "$MAILSTRING" $MAILLIST
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_
>>but it bombs
Can you post the error message you are getting?
Can you post the error message you are getting?
ASKER
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.
ASKER
Well, that didn't work. I even tried sourcing the .profile at the beginning with . ~/.profile
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Also, check your email on the server or crontab error logs for errors
ASKER
16 17 * * * /opt/VRTSsfmcs/Backup/VOM_ Backup.dav id.ksh > /dev/null 2>&1
Of course I move the time to when I want it to run to test it.
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.dav id.ksh > /tmp/mylog 2>&1
and check the log file?
Also, what are the perms of /opt/VRTSsfmcs/Backup/VOM_ Backup.dav id.ksh? Is the file executable (x perm)? If not then use
chmod +r /opt/VRTSsfmcs/Backup/VOM_ Backup.dav id.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
16 17 * * * /opt/VRTSsfmcs/Backup/VOM_
and check the log file?
Also, what are the perms of /opt/VRTSsfmcs/Backup/VOM_
chmod +r /opt/VRTSsfmcs/Backup/VOM_
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 :)
ASKER
Ok, changed it to this and it didn't even create mylog.
4 6 * * * /opt/VRTSsfmcs/Backup/VOM_ Backup.dav id.ksh >> /tmp/mylog 2>&1
4 6 * * * /opt/VRTSsfmcs/Backup/VOM_
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> 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.
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.
ASKER
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.da vid.ksh >> /tmp/mylog 2>&1)
Dec 10 06:54:01 lnxutil CROND[17529]: (root) CMD (/opt/VRTSsfmcs/Backup/VOM _Backup.da vid.ksh >> /tmp/mylog 2>&1)
lnxutil:/var/log# tail -100 cron|grep VOM
Dec 10 06:04:01 lnxutil CROND[1880]: (root) CMD (/opt/VRTSsfmcs/Backup/VOM
Dec 10 06:54:01 lnxutil CROND[17529]: (root) CMD (/opt/VRTSsfmcs/Backup/VOM
ASKER
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
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.
This will show the step by step execution of the script and you may catch the problem.
ASKER
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.
Here's the crontab entry:
9 9 * * * /opt/VRTSsfmcs/Backup/VOM_ Backup_tes t.ksh >> /tmp/mylog 2>&1
Here's the contents of mylog:
lnxutil:/tmp# cat mylog
#!/bin/ksh -v
set -x
MAILTO="david@itmemphis.co m"
+ 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_las t
+ rm -r /opt/VRTSsfmcs/Backup/VOM_ Backup_las t
mv /opt/VRTSsfmcs/Backup/VOM_ Backup_cur rent /opt/VRTSsfmcs/Backup/VOM_ Backup_las t
+ mv /opt/VRTSsfmcs/Backup/VOM_ Backup_cur rent /opt/VRTSsfmcs/Backup/VOM_ Backup_las t
#Logger
mv /opt/VRTSsmfcs/Backup/VOM_ Backup_cur rent.log /opt/VRTSsfmcs/Backup/VOM_ Backup_las t.log
+ mv /opt/VRTSsmfcs/Backup/VOM_ Backup_cur rent.log /opt/VRTSsfmcs/Backup/VOM_ Backup_las t.log
mkdir -p /opt/VRTSsmfcs/Backup/VOM_ Backup_cur rent
+ mkdir -p /opt/VRTSsmfcs/Backup/VOM_ Backup_cur rent
exec 1> /opt/VRTSsmfcs/Backup/VOM_ Backup_cur rent.log
+ exec
+ 1> /opt/VRTSsmfcs/Backup/VOM_ Backup_cur rent.log
#Backup
/opt/VRTSsfmcs/config/adm/ vom_bkup.p l --backup /opt/VRTSsfmcs/Backup/VOM_ Backup_cur rent
+ /opt/VRTSsfmcs/config/adm/ vom_bkup.p l --backup /opt/VRTSsfmcs/Backup/VOM_ Backup_cur rent
#!/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
Here's the crontab entry:
9 9 * * * /opt/VRTSsfmcs/Backup/VOM_
Here's the contents of mylog:
lnxutil:/tmp# cat mylog
#!/bin/ksh -v
set -x
MAILTO="david@itmemphis.co
+ MAILTO=david@itmemphis.com
MAILSTRING="lnxutil VOM Backup Result current"
+ MAILSTRING='lnxutil VOM Backup Result current'
# Clean up
rm -r /opt/VRTSsfmcs/Backup/VOM_
+ rm -r /opt/VRTSsfmcs/Backup/VOM_
mv /opt/VRTSsfmcs/Backup/VOM_
+ mv /opt/VRTSsfmcs/Backup/VOM_
#Logger
mv /opt/VRTSsmfcs/Backup/VOM_
+ mv /opt/VRTSsmfcs/Backup/VOM_
mkdir -p /opt/VRTSsmfcs/Backup/VOM_
+ mkdir -p /opt/VRTSsmfcs/Backup/VOM_
exec 1> /opt/VRTSsmfcs/Backup/VOM_
+ exec
+ 1> /opt/VRTSsmfcs/Backup/VOM_
#Backup
/opt/VRTSsfmcs/config/adm/
+ /opt/VRTSsfmcs/config/adm/
ASKER
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.
ASKER
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.p l line 3108.
[Thu Dec 11 09:33:17 2014] vom_bkup.pl: Can't return outside a subroutine at /opt/VRTSsfmcs/config/adm/
Are you sure that all necessary env variables are set?
Are you sourcing .profile file?
Are you sourcing .profile file?
ASKER
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.
ASKER
It's finishing. All the backups are done. The logs are there, and it runs just fine from the command line.
ASKER
Here's the contents of the log it's trying to email:
lnxutil:/opt/VRTSsfmcs/Bac kup# cat /opt/VRTSsmfcs/Backup/VOM_ Backup_cur rent.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_cur rent/vom_b ackup_linu x_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_cur rent directory.
lnxutil:/opt/VRTSsfmcs/Bac
Stop SNMP Trap Service.
Stop xprtld Service.
Stop Web Service.
Stop AT Service.
Stop DB Service.
Backup files to /opt/VRTSsfmcs/Backup/VOM_
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_
Just to verify, the very last line in mylog is:
#Backup
/opt/VRTSsfmcs/config/adm/ vom_bkup.p l --backup /opt/VRTSsfmcs/Backup/VOM_ Backup_cur rent
+ /opt/VRTSsfmcs/config/adm/ vom_bkup.p l --backup /opt/VRTSsfmcs/Backup/VOM_ Backup_cur rent
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.
#Backup
/opt/VRTSsfmcs/config/adm/
+ /opt/VRTSsfmcs/config/adm/
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.
ASKER
Ok, I'll give that a shot
ASKER
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.p l --backup /opt/VRTSsfmcs/Backup/VOM_ Backup_cur rent
+ /usr/bin/perl /opt/VRTSsfmcs/config/adm/ vom_bkup.p l --backup /opt/VRTSsfmcs/Backup/VOM_ Backup_cur rent
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.p l line 24.
BEGIN failed--compilation aborted at /opt/VRTSsfmcs/config/adm/ vom_bkup.p l line 24.
print "Backup complete" >> /tmp/mylog
#Backup
/usr/bin/perl /opt/VRTSsfmcs/config/adm/
+ /usr/bin/perl /opt/VRTSsfmcs/config/adm/
Perl lib version (v5.8.8) doesn't match executable version (v5.10.1) at /opt/VRTSsfmh/lib/modules/
Compilation failed in require at /opt/VRTSsfmh/lib/modules/
BEGIN failed--compilation aborted at /opt/VRTSsfmh/lib/modules/
Compilation failed in require at /opt/VRTSsfmcs/config/adm/
BEGIN failed--compilation aborted at /opt/VRTSsfmcs/config/adm/
print "Backup complete" >> /tmp/mylog
ASKER
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.p l --backup /opt/VRTSsfmcs/Backup/VOM_ Backup_cur rent
+ /usr/bin/perl -l /usr/share/perl5 /opt/VRTSsfmcs/config/adm/ vom_bkup.p l --backup /opt/VRTSsfmcs/Backup/VOM_ Backup_cur rent
print "Backup complete" >> /tmp/mylog
#Backup
/usr/bin/perl -l /usr/share/perl5 /opt/VRTSsfmcs/config/adm/
+ /usr/bin/perl -l /usr/share/perl5 /opt/VRTSsfmcs/config/adm/
print "Backup complete" >> /tmp/mylog
ASKER
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. :-/
ASKER
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_cur rent.log | /bin/mailx -s $MAILSTRING $MAILLIST");
Not polished, but it works. I'll split the points between you guys. Thanks for the effort!
David
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_
Not polished, but it works. I'll split the points between you guys. Thanks for the effort!
David
ASKER