http://www.perlmonks.org?node_id=969258

stahl has asked for the wisdom of the Perl Monks concerning the following question:

Oh Great Perl Monks, I come to you in the most dire of circumstances. I am but a lowely per lnewbie, and google has been a great friend to me. However, it has not been able to answer my greatest question. I created a 3 line perl script to call a shell script(I will list both Below) The current script is using system(), i have however tried both backticks and exec to accomplish my endgoal. None of these have worked. Also i have tried getting the stdout and in statements to work unsuccessfully. Im sure it is something to do with the otuput getting lost and not knowing where to go, but i wont know The real goal of this script is to automate the sophos updating process threw out my unix enviroment, by using this script to output to a file then i will make a nagios script to read the file and check for failed/successful updates. KSH/SH from what i have read and been told will not do inherently what i want it to do, which is why i come to you oh wise an great monks of perl. The OS running this is : AIX 5.3 Tech level 10

Below i shall insert very small code examples: The first script is call savupdate.sh it is apart of the sophos for unix Antivirus client.

#! /bin/sh # # Copyright (c) 1989-2009 Sophos Plc. All rights reserved. # All rights reserved. # CIDREPDIR=`dirname $0` INSTALLORCOMMONDIR=$CIDREPDIR/.. PythonInterpreter=$INSTALLORCOMMONDIR/engine/python [ -f $PythonInterpreter ] || PythonInterpreter=$INSTALLORCOMMONDIR/eng +ine/_/python ORIGINAL_PYTHONPATH="${ORIGINAL_PYTHONPATH-${PYTHONPATH}}" ORIGINAL_LD_LIBRARY_PATH="${ORIGINAL_LD_LIBRARY_PATH-${LD_LIBRARY_PATH +}}" ORIGINAL_PYTHONHOME="${ORIGINAL_PYTHONHOME-${PYTHONHOME}}" export ORIGINAL_PYTHONPATH export ORIGINAL_LD_LIBRARY_PATH export ORIGINAL_PYTHONHOME if [ -f $PythonInterpreter ] then #Settings for when SAV is installed INSTDIR=$INSTALLORCOMMONDIR PYTHONHOME=$INSTDIR PythonStdModules=$INSTDIR/engine/python25.zip PythonLibsDir=$INSTDIR/lib/python2.5/lib-dynload PythonModsDir=$INSTDIR/engine/_ LD_LIBRARY_PATH=$INSTDIR/lib PYTHONHOME=$INSTDIR MINIPKG= else PLATFORM=`uname -s | LC_ALL=C tr '[:upper:]' '[:lower:]' | LC_ALL= +C tr -d '-'` export PLATFORM if [ $PLATFORM = "linux" ] ; then PROCESSOR=`uname -m` else PROCESSOR=`uname -p 2>/dev/null` if [ $? -ne 0 ] || [ $PROCESSOR = "unknown" ] ; then PROCESSOR=`uname -m` else PROCESSOR=`uname -p` fi fi #Settings for when SAV is not installed COMMONDIR=$INSTALLORCOMMONDIR if [ -f /etc/redhat-release ] && grep -q "Red Hat Linux release 9 +(Shrike)" /etc/redhat-release ; then ARCHITECTURE=rh90 elif echo $PROCESSOR | grep "[ix].*86" > /dev/null 2>&1 ; then ARCHITECTURE=x86 else ARCHITECTURE=$PROCESSOR fi CoreDir=sav-${PLATFORM}/$ARCHITECTURE DISTDIR=$COMMONDIR/../.. PythonInterpreter=$DISTDIR/$CoreDir/engine/_/python PythonStdModules=$COMMONDIR/engine/python25.zip PythonLibsDir=$DISTDIR/$CoreDir/lib/python2.5/lib-dynload PythonModsDir=$COMMONDIR/engine/_:$DISTDIR/$CoreDir/engine/_ LD_LIBRARY_PATH=$DISTDIR/$CoreDir/lib PYTHONHOME=$DISTDIR MINIPKG=--minipkg fi APPPATH=$CIDREPDIR:$CIDREPDIR/cidrep.zip:$CIDREPDIR/installer.zip:$INS +TALLORCOMMONDIR/engine/util.zip PYTHONPATH=$PythonLibsDir:$PythonModsDir:$PythonStdModules:$APPPATH export PYTHONPATH export PYTHONHOME export LD_LIBRARY_PATH chmod 0755 $PythonInterpreter 2>/dev/null unset LIBPATH exec $PythonInterpreter -c "import savupdate.Updater,sys; sys.exit(sav +update.Updater.main(sys.argv))" "$CIDREPDIR" $MINIPKG "$@"

This is the scripts output:

root# ./savupdate.sh Downloading http://xxx.xxx.xxx.xxx:8000/CIDs/S000/EESAVUNIX/AIX_PPC/server.inf Downloading http://xxx.xxx.xxx.xxx:8000/CIDs/S000/EESAVUNIX/AIX_PPC/cidsync.upd 16763 bytes downloaded in 0.028047 secs (583.670512 KiB/s) Downloading http://xxx.xxx.xxx.xxx:8000/CIDs/S000/EESAVUNIX/AIX_PPC/config/index.spec Downloading http://xxx.xxx.xxx.xxx:8000/CIDs/S000/EESAVUNIX/AIX_PPC/talpa-custom/index.spec Downloading http://xxx.xxx.xxx.xxx:8000/CIDs/S000/EESAVUNIX/AIX_PPC/cac.pem 1131 bytes downloaded in 0.020245 secs (54.556092 KiB/s) Downloading http://xxx.xxx.xxx.xxx:8000/CIDs/S000/EESAVUNIX/AIX_PPC/mrinit.custom Downloading http://xxx.xxx.xxx.xxx:8000/CIDs/S000/EESAVUNIX/AIX_PPC/MRInit.custom Downloading http://xxx.xxx.xxx.xxx:8000/CIDs/S000/EESAVUNIX/AIX_PPC/mrinit.conf 461 bytes downloaded in 0.014299 secs (31.484052 KiB/s) Successfully updated Sophos Anti-Virus from http://xxx.xxx.xxx.xxx:8000/CIDs/S000/EESAVUNIX

The Next script is the smiple 1 lines i wrote, please dont laugh oh great monks for i am in training.

#!/usr/bin/perl ###################################### # #Sophos Update Script # # ###################################### system("/Antivirus/sophos-av/update/savupdate.sh 1>/tmp/updater 2>&1") +;

After running this script from command line it works every time. However when trying to run it from crontab it fails everytime..it has also been the case with every other KSH/SH/Perl script i have tried to write...they all work great from command line no problems, however when used with crontab they all fail. I have check to make sure the scripts themself are running by having an active ps -ef going just to make sure they are being executed. It seems that the output and how it is directed is what is illuding me.

Monks, if you could help solve this issue for me i would be eternally grateful! Thank you, your monk in training, Stahl

~

Replies are listed 'Best First'.
Re: Problem with AIX 5.3 Perl/SH Script and Crontab
by flexvault (Monsignor) on May 07, 2012 at 14:32 UTC

    Welcome stahl,

    Part of the problem is that when you execute the script from the command line, you have all the environmental variables set up for you. Unfortunately when you run the script(s) from 'crontab', you more than likely have a totally different set of environmental variables.

    Try running a script from crontab that captures the environmental variables ( 'env' command ) to a file and then run the same script to a different file. Use 'diff' to find the differences and then your script will have to initialize those variables for crontab. You also want to give the full path for all commands since the PATH s may be different. You'll know for sure when you see the diff results.

    Good Luck

    "Well done is better than well said." - Benjamin Franklin

Re: Problem with AIX 5.3 Perl/SH Script and Crontab
by ww (Archbishop) on May 07, 2012 at 22:59 UTC
    My esteemed colleagues, flexvault and zwon, highlight different elements of your problem. But don't go away mad... or disappointed. The issue with environment is critical to your script; asking questions that have rather more of a Perl nexus has much to do with the likelihood of getting future help here.

    A script which merely invokes the shell is a lot like the individual who asks permission to ask a question in the Monastery's CB. Neither is much more than a way to inconvenience a few electrons for no good reason... and neither gets to the root of the problem.

    And your entire shell script... or, rather, Sophos' shell script... could be rewritten in Perl ... or Python, in fact, in which case, your question might well be directed to those who specialize in that language.

    If you opt to try to do so in Perl, please do come again: we'll be pleased to help you with pointers to training, references and tips when you get stuck. Bit we really do expect you to have a Perl question... and to show that you've made an attempt to work out your issues, yourself.

Re: Problem with AIX 5.3 Perl/SH Script and Crontab
by zwon (Abbot) on May 07, 2012 at 14:57 UTC

    Replace your perl script with the following:

    #!/bin/sh /Antivirus/sophos-av/update/savupdate.sh 1>/tmp/updater 2>&1
    and seek help on some shell forum.
Re: Problem with AIX 5.3 Perl/SH Script and Crontab
by Anonymous Monk on May 08, 2012 at 00:13 UTC

    Hi,

    I have never used AIX, so this may not work. It did on various other *nix flavours.

    In your crontab line, before the perl command use the AIX equivalent of 'source' or '.' to create the same environment as you have in your normal login.

    Something along the lines of -

    0 0,12 1 */2 * /sbin/ping -c 192.168.0.1 becomes 0 0,12 1 */2 * source .env_file; /sbin/ping -c 192.168.0.1 or 0 0,12 1 */2 * . .env_file; /sbin/ping -c 192.168.0.1

    It has been some time since I've done this , so the actual commands have become hazy. Please forgive any errors.

    J.C.