ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Cron job not executing script properly

    IT Discussion
    3
    9
    1.3k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • AdamFA
      AdamF
      last edited by

      I have 3 scripts that I want to run at different times. Logged in as root, I setup a cronjob with:

      crontab -e
      

      The contents of the crontab file is:

      0 7 * * 6 /opt/scripts/script_1.sh >/dev/null 2>&1
      0 0 * * * /opt/scripts/script_2.sh >/dev/null 2>&1
      0 6 * * * /opt/scripts/script_3.sh >/dev/null 2>&1
      

      Permissions on all script files are:

      -rwxr-xr-x  1 root root
      

      Each script runs when running it manually.

      The scripts are not running. On another system, I run only script 1 via cron, and it works perfectly. However on this server, the only difference (at least with the crontab file) is that I have multiple entries.

      Is there anything obvious that anyone sees that could be an issue?

      1 Reply Last reply Reply Quote 0
      • triple9T
        triple9
        last edited by

        Do you have #!/bin/bash in the scripts? Other thing that could cause problem is missing PATH variable. Put something like

        #!/bin/bash
        export PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin
        

        In script itself.

        AdamFA 2 Replies Last reply Reply Quote 2
        • AdamFA
          AdamF @triple9
          last edited by AdamF

          @triple9 said in Cron job not executing script properly:

          Do you have #!/bin/bash in the scripts? Other thing that could cause problem is missing PATH variable. Put something like

          #!/bin/bash
          export PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin
          

          In script itself.

          Yes, I have #!/bin/bash in each script. I will put in:

          export PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin 
          

          and see what happens. Thanks

          1 Reply Last reply Reply Quote 0
          • AdamFA
            AdamF @triple9
            last edited by

            @triple9 said in Cron job not executing script properly:

            Do you have #!/bin/bash in the scripts? Other thing that could cause problem is missing PATH variable. Put something like

            #!/bin/bash
            export PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin
            

            In script itself.

            Well that did the trick. Thanks!

            1 Reply Last reply Reply Quote 0
            • triple9T
              triple9
              last edited by

              You're welcome.

              1 Reply Last reply Reply Quote 0
              • travisdh1T
                travisdh1
                last edited by

                @triple9 Good job getting @fuznutz04 sorted.

                Cron runs in it's own user-space. Which has very little already defined.... like PATH.

                If a script runs fine when run manually but fails in cron, it's almost always a missing environment variable (PATH being the most common.)

                AdamFA 1 Reply Last reply Reply Quote 1
                • AdamFA
                  AdamF @travisdh1
                  last edited by

                  @travisdh1 said in Cron job not executing script properly:

                  @triple9 Good job getting @fuznutz04 sorted.

                  Cron runs in it's own user-space. Which has very little already defined.... like PATH.

                  If a script runs fine when run manually but fails in cron, it's almost always a missing environment variable (PATH being the most common.)

                  Well that's great to know. So is it safe to assume for every script that I want to run with cron, should have this defined in each script?

                  #!/bin/bash
                  export PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin
                  
                  travisdh1T 1 Reply Last reply Reply Quote 0
                  • travisdh1T
                    travisdh1 @AdamF
                    last edited by

                    @fuznutz04 said in Cron job not executing script properly:

                    @travisdh1 said in Cron job not executing script properly:

                    @triple9 Good job getting @fuznutz04 sorted.

                    Cron runs in it's own user-space. Which has very little already defined.... like PATH.

                    If a script runs fine when run manually but fails in cron, it's almost always a missing environment variable (PATH being the most common.)

                    Well that's great to know. So is it safe to assume for every script that I want to run with cron, should have this defined in each script?

                    #!/bin/bash
                    export PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin
                    

                    It's very likely, yes.

                    AdamFA 1 Reply Last reply Reply Quote 0
                    • AdamFA
                      AdamF @travisdh1
                      last edited by

                      @travisdh1 Ok, thanks for the explanation.

                      1 Reply Last reply Reply Quote 1
                      • 1 / 1
                      • First post
                        Last post