Solved

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

Posted on 2014-12-09
34
194 Views
Last Modified: 2014-12-12
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
Comment
Question by:David Aldridge
  • 18
  • 7
  • 5
  • +1
34 Comments
 
LVL 1

Author Comment

by:David Aldridge
ID: 40489817
BTW, I didn't write this.. just trying to troubleshoot it for a guy.  :->
0
 
LVL 48

Expert Comment

by:Tintin
ID: 40490038
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
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 40490055
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
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 40490059
>>but it bombs
Can you post the error message you are getting?
0
 
LVL 1

Author Comment

by:David Aldridge
ID: 40490129
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
 
LVL 1

Author Comment

by:David Aldridge
ID: 40490184
Well, that didn't work.  I even tried sourcing the .profile at the beginning with  . ~/.profile
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 166 total points
ID: 40490346
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 40490405
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
 
LVL 1

Author Comment

by:David Aldridge
ID: 40490417
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 40490429
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
 
LVL 37

Assisted Solution

by:Gerwin Jansen
Gerwin Jansen earned 167 total points
ID: 40490635
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 40490655
The aim is to get the errors if there is any error and for that it is temporary redirect to know the cause.
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 40490678
@omarfarid - just sharing some best practices here ;)
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 40490711
Thanks :)
0
 
LVL 1

Author Comment

by:David Aldridge
ID: 40491103
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
 
LVL 40

Accepted Solution

by:
omarfarid earned 167 total points
ID: 40491139
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
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 40491485
>> 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:David Aldridge
ID: 40491590
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
 
LVL 1

Author Comment

by:David Aldridge
ID: 40491594
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 40493001
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
 
LVL 1

Author Comment

by:David Aldridge
ID: 40493983
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
 
LVL 1

Author Comment

by:David Aldridge
ID: 40494002
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
 
LVL 1

Author Comment

by:David Aldridge
ID: 40494019
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 40494882
Are you sure that all necessary env variables are set?
Are you sourcing .profile file?
0
 
LVL 1

Author Comment

by:David Aldridge
ID: 40494954
I put the full path to all execuatables, set the path variable, AND sourced . /root/.profile.  Still doesn't work.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 40495217
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
 
LVL 1

Author Comment

by:David Aldridge
ID: 40495220
It's finishing.  All the backups are done.  The logs are there, and it runs just fine from the command line.
0
 
LVL 1

Author Comment

by:David Aldridge
ID: 40495231
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
 
LVL 48

Expert Comment

by:Tintin
ID: 40495319
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
 
LVL 1

Author Comment

by:David Aldridge
ID: 40495994
Ok, I'll give that a shot
0
 
LVL 1

Author Comment

by:David Aldridge
ID: 40496255
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
 
LVL 1

Author Comment

by:David Aldridge
ID: 40496274
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
 
LVL 1

Author Comment

by:David Aldridge
ID: 40496296
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
 
LVL 1

Author Comment

by:David Aldridge
ID: 40497029
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
This article will show, step by step, how to integrate R code into a R Sweave document
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now