Cron Job Not Working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sainiamit25
    New Member
    • Jul 2007
    • 40

    Cron Job Not Working

    Hi,

    I am trying to run a simple shell script which executes asl in return. I am able to execute the shell scrip manually but i am not able to run the script through cron job. Can anybody help me out? Is it something to do with environment variables?My script is

    #!/bin/sh
    cd /opt/InCh/IP/smarts/bin
    sm_adapter -s APM1 /opt/InCh/IP/smarts/local/rules/Cards.asl >> /opt/InCharge65/IP/smarts/bin/xvz.log

    exit 0

    Cheers,
    Amit
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by sainiamit25
    Hi,

    I am trying to run a simple shell script which executes asl in return. I am able to execute the shell scrip manually but i am not able to run the script through cron job. Can anybody help me out? Is it something to do with environment variables?My script is

    #!/bin/sh
    cd /opt/InCh/IP/smarts/bin
    sm_adapter -s APM1 /opt/InCh/IP/smarts/local/rules/Cards.asl >> /opt/InCharge65/IP/smarts/bin/xvz.log

    exit 0

    Cheers,
    Amit
    I can see that you did a "cd" to the directory, but instead, run the sm_adapter command with the full path before it and see if it works.

    Regards,

    Jeff

    Comment

    • sainiamit25
      New Member
      • Jul 2007
      • 40

      #3
      Originally posted by numberwhun
      I can see that you did a "cd" to the directory, but instead, run the sm_adapter command with the full path before it and see if it works.

      Regards,

      Jeff
      Hi Jeff,

      Thanks for the suggestions. Yes, i tried that way also but it is not working. In frustation, i serached through the internet and found that many people face this problem. This is something to do with environment variable -- shell takes different set of environment variables and crontab takes different. But i could not understand how i can modify my script or change the environment variables. If you could through some light from that side, it would be great!!

      Cheers,
      Amit

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        Originally posted by sainiamit25
        Hi Jeff,

        Thanks for the suggestions. Yes, i tried that way also but it is not working. In frustation, i serached through the internet and found that many people face this problem. This is something to do with environment variable -- shell takes different set of environment variables and crontab takes different. But i could not understand how i can modify my script or change the environment variables. If you could through some light from that side, it would be great!!

        Cheers,
        Amit
        If I remember right, when the cron job runs, it is actually root that runs the job. That is why the need for absolute paths. As for the environment variables, if one of them is not getting set correctly, maybe you can put a line in the script to change it to what you need it to be.

        Regards,

        Jeff

        Comment

        • prn
          Recognized Expert Contributor
          • Apr 2007
          • 254

          #5
          A cron job is not necessarily run as root. Any user may have a crontab and the jobs in that crontab are run as that user. However, the crontab job does not necessarily exec the user's .profile (.bash_profile, .cshrc, whatever) so the user's environment is not necessarily the same in the context of the cron job as it is for an interactive job. Jeff's suggestion of adding the full path to the sm_adapter command is the most common solution to the problem, but adding "./" to the beginning of that line should be equivalent since you already did a cd to its directory. Since that did not work, the next thing to try is to actually find out what is going wrong. :-/

          You need to catch the output from your cron job (not just the output from the sm_adapter command) so make sure that your crontab entry redirects both STDOUT and STDERR to a file. How you do that will depend on your shell, but most likely, the entry in your crontab file should look something like:
          Code:
          30 23 * * *  /home/whatever/foo >/home/whatever/mylog 2>&1
          and now the file "/home/whatever/mylog" should contain relevant information.

          Since the most likely cause for your problem is your environment settings, I would suggest adding a line in your cron job to catch that too, e.g.:
          Code:
          #!/bin/sh
          env
          cd /opt/InCh/IP/smarts/bin
          ./sm_adapter -s APM1 /opt/InCh/IP/smarts/local/rules/Cards.asl >> /opt/InCharge65/IP/smarts/bin/xvz.log
          
          exit 0
          Addition of the "env" line should add a listing of your environmental settings to the /home/whatever/mylog file. Comparing the environment listing with the equivalent from an interactive session may also give you a clue about where your problem lies.

          If it comes down to it, you may be able to execute your .profile inside the cron job to get comparable environment variables, but that's not the first thing I would suggest, and I certainly would not suggest it if you are not clear on what works in interactive and non-interactive contexts. Too many people do unsafe things in their .profiles for me to feel comfortable recommending this without knowing a lot more than I do about your situation. More likely there are relatively specific things you should add to your cron job to set the environment. I'll refrain from suggesting any of those until I know more about your situation.

          HTH,
          Paul

          Comment

          • arne
            Recognized Expert Contributor
            • Oct 2006
            • 315

            #6
            Originally posted by sainiamit25
            Hi,

            I am trying to run a simple shell script which executes asl in return. I am able to execute the shell scrip manually but i am not able to run the script through cron job. Can anybody help me out? Is it something to do with environment variables?My script is

            #!/bin/sh
            cd /opt/InCh/IP/smarts/bin
            sm_adapter -s APM1 /opt/InCh/IP/smarts/local/rules/Cards.asl >> /opt/InCharge65/IP/smarts/bin/xvz.log

            exit 0

            Cheers,
            Amit
            Are you sure your your cron job is invoked at all? I once had a problem that the cron job was not invoked after I copied the crontab from one server to another. The reason was a missing newline at the the very end of the crontab ...

            arne

            Comment

            • sainiamit25
              New Member
              • Jul 2007
              • 40

              #7
              Originally posted by prn
              A cron job is not necessarily run as root. Any user may have a crontab and the jobs in that crontab are run as that user. However, the crontab job does not necessarily exec the user's .profile (.bash_profile, .cshrc, whatever) so the user's environment is not necessarily the same in the context of the cron job as it is for an interactive job. Jeff's suggestion of adding the full path to the sm_adapter command is the most common solution to the problem, but adding "./" to the beginning of that line should be equivalent since you already did a cd to its directory. Since that did not work, the next thing to try is to actually find out what is going wrong. :-/

              You need to catch the output from your cron job (not just the output from the sm_adapter command) so make sure that your crontab entry redirects both STDOUT and STDERR to a file. How you do that will depend on your shell, but most likely, the entry in your crontab file should look something like:
              Code:
              30 23 * * *  /home/whatever/foo >/home/whatever/mylog 2>&1
              and now the file "/home/whatever/mylog" should contain relevant information.

              Since the most likely cause for your problem is your environment settings, I would suggest adding a line in your cron job to catch that too, e.g.:
              Code:
              #!/bin/sh
              env
              cd /opt/InCh/IP/smarts/bin
              ./sm_adapter -s APM1 /opt/InCh/IP/smarts/local/rules/Cards.asl >> /opt/InCharge65/IP/smarts/bin/xvz.log
              
              exit 0
              Addition of the "env" line should add a listing of your environmental settings to the /home/whatever/mylog file. Comparing the environment listing with the equivalent from an interactive session may also give you a clue about where your problem lies.

              If it comes down to it, you may be able to execute your .profile inside the cron job to get comparable environment variables, but that's not the first thing I would suggest, and I certainly would not suggest it if you are not clear on what works in interactive and non-interactive contexts. Too many people do unsafe things in their .profiles for me to feel comfortable recommending this without knowing a lot more than I do about your situation. More likely there are relatively specific things you should add to your cron job to set the environment. I'll refrain from suggesting any of those until I know more about your situation.

              HTH,
              Paul
              Thanks Paul for such a nice expalnation. I will try it it.

              Cheers,
              Amit

              Comment

              • sainiamit25
                New Member
                • Jul 2007
                • 40

                #8
                Originally posted by arne
                Are you sure your your cron job is invoked at all? I once had a problem that the cron job was not invoked after I copied the crontab from one server to another. The reason was a missing newline at the the very end of the crontab ...

                arne
                Hi Arne,

                There are some other utilities which are running through that crontab. So i think that the crontab is running. I will try to debug the problem as suggested by Paul earlier.

                Thanks a lot for your inputs

                Cheers,
                Amit

                Comment

                • arne
                  Recognized Expert Contributor
                  • Oct 2006
                  • 315

                  #9
                  Originally posted by sainiamit25
                  Hi Arne,

                  There are some other utilities which are running through that crontab. So i think that the crontab is running. I will try to debug the problem as suggested by Paul earlier.

                  Thanks a lot for your inputs

                  Cheers,
                  Amit
                  Still: even if other entries work, it doesn't mean that cron is able to parse the last entry ... this was the problem I had. All other entries worked just fine and there was no message about a corrupted entry or something similar. cron just silently didn't execute the last line as it was not ending in a newline.

                  arne

                  Comment

                  • mully31
                    New Member
                    • Oct 2007
                    • 1

                    #10
                    Originally posted by arne
                    Still: even if other entries work, it doesn't mean that cron is able to parse the last entry ... this was the problem I had. All other entries worked just fine and there was no message about a corrupted entry or something similar. cron just silently didn't execute the last line as it was not ending in a newline.

                    arne
                    Is this behaviour just on linux?? Reason i ask is that i am moving some db's from Solaris to Linux RHE4. there is no blank line on the solaris crontab, and the last line works....

                    cheers
                    mark

                    Comment

                    • arne
                      Recognized Expert Contributor
                      • Oct 2006
                      • 315

                      #11
                      Originally posted by mully31
                      Is this behaviour just on linux?? Reason i ask is that i am moving some db's from Solaris to Linux RHE4. there is no blank line on the solaris crontab, and the last line works....

                      cheers
                      mark
                      I didn't check with Solaris. The copy was between two different versions of Linux. On one machine it worked, on the other it didn't.

                      arne

                      Comment

                      Working...