The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

CRON: Run .sh Script as Root

Discussion in 'General Discussion' started by wswd, Aug 28, 2014.

  1. wswd

    wswd Well-Known Member

    Joined:
    Aug 9, 2005
    Messages:
    112
    Likes Received:
    0
    Trophy Points:
    16
    cPanel Access Level:
    Root Administrator
    Hello all,

    Curious if there is a special secret to call .sh scripts using crons as root.

    Tried

    * * * * * /home/script.sh
    * * * * * /home/script.sh > /home2/script.log 2>&1


    Nothing. As far as I can tell, the cron isn't even executing. Doesn't matter if I reboot the server or restart crond.

    Anything?
     
  2. cPJacob

    cPJacob cPanel Product Owner
    Staff Member

    Joined:
    May 2, 2014
    Messages:
    510
    Likes Received:
    66
    Trophy Points:
    28
    cPanel Access Level:
    DataCenter Provider
    Twitter:
    Is '/home/script.sh' executable with an interpreter at the top? If not, you need to call the interpreter in the cron (which is wise to do anyway).

    * * * * * /bin/bash /home/script.sh
     
  3. wswd

    wswd Well-Known Member

    Joined:
    Aug 9, 2005
    Messages:
    112
    Likes Received:
    0
    Trophy Points:
    16
    cPanel Access Level:
    Root Administrator
    Thanks Jacob.

    Yes, it's executable (755) with the #!/bin/bash at the top of the script.

    From what I can tell (cron log and such), it doesn't even appear that the cron is being run at all, which is odd. But then again, can't really tell.
     
  4. kdean

    kdean Well-Known Member

    Joined:
    Oct 19, 2012
    Messages:
    262
    Likes Received:
    12
    Trophy Points:
    18
    Location:
    Orlando, FL
    cPanel Access Level:
    Root Administrator
    Add the cron in:

    /var/spool/cron/root

    * * * * * /bin/bash /home/script.sh > /home2/script.log 2>&1

    or maybe:

    * * * * * /bin/sh /home/script.sh > /home2/script.log 2>&1

    The last one is how I've done mine with just a "#!/bin/sh" at the top of the file.
     
  5. server9host

    server9host Well-Known Member

    Joined:
    Sep 18, 2013
    Messages:
    160
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    India
    cPanel Access Level:
    Root Administrator
    Hello,

    To run bash file you need to copy script.sh file to /bin then set the cron for your bash script as above you set.

    Please check this solution let me know it's work for you or not.
     
  6. wswd

    wswd Well-Known Member

    Joined:
    Aug 9, 2005
    Messages:
    112
    Likes Received:
    0
    Trophy Points:
    16
    cPanel Access Level:
    Root Administrator
    Hi kdean,

    Tried all of the above to no avail. Neither worked. I can execute the script just fine from the command line, so I know it's not the script. Has to be something with the CRON.

    Just to add. Attempted this on two different cPanel servers and it won't work on either one. Something is really strange...

    - - - Updated - - -

    Thanks server9host. Will try it right now.
     
  7. wswd

    wswd Well-Known Member

    Joined:
    Aug 9, 2005
    Messages:
    112
    Likes Received:
    0
    Trophy Points:
    16
    cPanel Access Level:
    Root Administrator
    @server9host

    That did not work either. :(
     
  8. server9host

    server9host Well-Known Member

    Joined:
    Sep 18, 2013
    Messages:
    160
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    India
    cPanel Access Level:
    Root Administrator
    Hello,

    Can you please post cron log for this script. You get cron log from "/var/log/cron" path.
     
  9. wswd

    wswd Well-Known Member

    Joined:
    Aug 9, 2005
    Messages:
    112
    Likes Received:
    0
    Trophy Points:
    16
    cPanel Access Level:
    Root Administrator
    Nothing is showing up in the log for that CRON. Others from root are showing up. That's part of the problem. It's almost as though the CRON isn't even running.
     
  10. server9host

    server9host Well-Known Member

    Joined:
    Sep 18, 2013
    Messages:
    160
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    India
    cPanel Access Level:
    Root Administrator
    Hello,

    What about cron service. It's working or not. Please restart the cron server using below command :

    Code:
     service crond restart 
     
  11. wswd

    wswd Well-Known Member

    Joined:
    Aug 9, 2005
    Messages:
    112
    Likes Received:
    0
    Trophy Points:
    16
    cPanel Access Level:
    Root Administrator
    Yes, the cron service is running. I restarted it a few times to be certain my entries were taking. It's processing other crons (even other items listed in the root cron) just fine, according to the log. It just won't process the one I add.

    # service crond status
    crond (pid 209796) is running...
     
  12. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,854
    Likes Received:
    676
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    You should see an entry for it in the /var/log/cron log file. If you add another, generic cron job, does it work normally and result in output to /var/log/cron? How have you added the cron job?

    Thank you.
     
  13. wswd

    wswd Well-Known Member

    Joined:
    Aug 9, 2005
    Messages:
    112
    Likes Received:
    0
    Trophy Points:
    16
    cPanel Access Level:
    Root Administrator

    Thanks Michael.

    Nothing is showing up for it in the log at all. I added it to /var/spool/cron/root
     
  14. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,854
    Likes Received:
    676
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    Do you see other entries in the log for root cron jobs, indicating it's working as intended? Have you ensured you added the cron job to that file correctly (e.g. no incorrect formatting)?

    Thank you.
     
  15. kdean

    kdean Well-Known Member

    Joined:
    Oct 19, 2012
    Messages:
    262
    Likes Received:
    12
    Trophy Points:
    18
    Location:
    Orlando, FL
    cPanel Access Level:
    Root Administrator
    If you added it to the bottom of the root cron, try moving if further up to the top in case another line is causing a syntax problem or other issue preventing other cron lines from running.
     
  16. wswd

    wswd Well-Known Member

    Joined:
    Aug 9, 2005
    Messages:
    112
    Likes Received:
    0
    Trophy Points:
    16
    cPanel Access Level:
    Root Administrator
    Yes and yes. Kdean, will give it a try.
     
  17. wswd

    wswd Well-Known Member

    Joined:
    Aug 9, 2005
    Messages:
    112
    Likes Received:
    0
    Trophy Points:
    16
    cPanel Access Level:
    Root Administrator
    Kdean, looks like your method worked. I moved it to the #1 slot and the CRON is running.

    Unfortunately, it's now logging the following error and the script is terminating:



    Again, the script runs great when I run it from the command line. No issues at all.
     
  18. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,854
    Likes Received:
    676
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    The "bad_alloc" error message typically indicates a lack of available memory. Check to see if any VPS resource limits were reached if this is a VPS, or check to ensure enough memory is available at the time this cron job runs.

    Thank you.
     
  19. wswd

    wswd Well-Known Member

    Joined:
    Aug 9, 2005
    Messages:
    112
    Likes Received:
    0
    Trophy Points:
    16
    cPanel Access Level:
    Root Administrator
    Hi Michael,

    Sorry for the delay. Got called out of the country and had very limited Internet access. It's actually a dedicated box with almost 30G of free RAM. That's the really strange part. Running the script from SSH does not cause the issue...it's only when I run it from the CRON. I can run it from the command line literally right before or after the CRON executes, and there are no problems at all. But running it via cron gets the error.

    Never seen anything like this before.
     
  20. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,854
    Likes Received:
    676
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    Feel free to open a support ticket so we can take a closer look. You can post the ticket number here so we can update this thread with the outcome.

    Thank you.
     
Loading...

Share This Page