4 Replies Latest reply on Mar 3, 2005 6:50 AM by 807557

    How to set an environment variable in crontab in SOLARIS

    807557
      Hi,
      I have strange problem in using cron. i have written a shell script
      which is to be automated using cron. Inside the shell script i have to pick up a variable which can be set up either globally in an environment file or inside the crontab file. when i set it up inside the environment file and run the script , it works fine. but thesame script when scheduled doesnt work. I guess the problem is with picking the variable ($VAR_HOME) definition when being executed from cron.How do i set variable definitions inside the crontab file?

      Same scenario works fine in Linux environment.

      thanks in advance
      Vijay
        • 1. Re: How to set an environment variable in crontab in SOLARIS
          807557
          It depends on which shell the script is using.: sh, ksh, bash...
          You should find plenty of examples in /etc/init.d
          • 2. Re: How to set an environment variable in crontab in SOLARIS
            807557
            Hi,
            Actually i want to set the variable in crontab and use the same variable in the script that is located else where. I have to run 5-6 scripts which uses this variable. So i want to define it in the Crontab file and use the same in the shell script.
            Eg : I edited the crontab file of root as follows
            NEW_HOME=/export/home/general
            DB_SETUP=user/user@user
            59 17 * * * /export/home/general/executeScript1.sh


            executeScript1.sh is located in /export/home/general, which is as follows :
            #!/bin/bash
            echo "$NEW_HOME"
            if [ -r "$NEW_HOME"/cron.log ]; then
            echo "file is listed in the Directory"
            else
            echo " file is not listed"
            exit 2
            fi

            when i execute the script under /export/home/general it works fine : it echos "file is listed in the Directory" since NEW_HOME is set in the env.
            But when i excute the same in cron it doesnt. the var that is echo "$NEW_HOME" is blank. So how do i set the variable so that it can be picked when the script is being execute from cron

            thanks
            • 3. Re: How to set an environment variable in crontab in SOLARIS
              user4994457
              Hi,
              Actually i want to set the variable in crontab and
              use the same variable in the script that is located
              else where. I have to run 5-6 scripts which uses this
              variable. So i want to define it in the Crontab file
              and use the same in the shell script.
              The crontab format doesn't allow that.
              Eg : I edited the crontab file of root as follows
              NEW_HOME=/export/home/general
              DB_SETUP=user/user@user
              59 17 * * * /export/home/general/executeScript1.sh
              I would assume you got an error about invalid lines in the crontab.
              when i execute the script under /export/home/general
              it works fine : it echos "file is listed in the
              Directory" since NEW_HOME is set in the env.
              But when i excute the same in cron it doesnt. the var
              that is echo "$NEW_HOME" is blank. So how do i set
              the variable so that it can be picked when the script
              is being execute from cron
              Set the environment prior to each line. You mention 5-6 scripts. That's not too many to just cut&paste the environment in your favorite editor.

              59 17 * * * NEW_HOME=/export/home/general DB_SETUP=user/user@user /export/home/general/executeScript1.sh

              --
              Darren
              • 4. Re: How to set an environment variable in crontab in SOLARIS
                807557
                Thanks Darren, that works. Can you plz help me on one more issue.
                In the scripts i was mentioning, i am actaully picking a few .xls files converting them into .txt files husning JXCEL. After the successfull conversion we call a java program which connects to the DB ( thats what theDB_SETUP variable is for) which executes a set of procedures fro storing the content from the .txt file into the DB. On successfull execution of the procedure i call a mailer program which send mail to the respective uses. All the above process i automate it using Cron. As i already mentioned the above set up works fine in linux where we set the vaibales intially in itself unlike in solaris where you mentioned its has to be set along with the scheduled script. Now For the above process of calling java program, connecting to DB, then excuting a mail i need to set a few jars in the class path as follows :
                CLASSPATH=/DataConversion/DataConversionMail/ogi_ims/lib/activation.jar:/DataConversion/DataConversionMail/ogi_ims/lib/classes12.jar:/DataConversion/DataConversionMail/ogi_ims/lib/log4j-1.2.8.jar:/DataConversion/DataConversionMail/ogi_ims/lib/mail.jar:/DataConversion/DataConversionMail:.
                PATH=/sbin:/bin:/usr/sbin:/usr/bin:/opt/j2sdk_nb/j2sdk1.4.2/bin:$ORACLE_HOME/bin

                The above 2 lines i set in the beginning in the crontab file in Linux. But I dont think i can do the same in Solaris crontab file. But my java programs embedded within the shell script will be pointing to these jars, so how i do put these jars in class path of the cron env when the script executes from the crontab file.
                Please let me know if you need any other info.

                Thanks in advance
                rgds
                Vijay Kumar Yenne