Skip to main content

CRON: Run .sh Script as Root

Comments

20 comments

  • JacobPerkins
    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
    0
  • wswd
    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.
    0
  • kdean
    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.
    0
  • server9host
    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.
    0
  • wswd
    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... [COLOR="silver">- - - Updated - - - Thanks server9host. Will try it right now.
    0
  • wswd
    @server9host That did not work either. :(
    0
  • server9host
    Hello, Can you please post cron log for this script. You get cron log from "/var/log/cron" path.
    0
  • wswd
    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.
    0
  • server9host
    Hello, What about cron service. It's working or not. Please restart the cron server using below command : service crond restart
    0
  • wswd
    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...
    0
  • cPanelMichael
    [quote="wswd, post: 1718741">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.
    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.
    0
  • wswd
    [quote="cPanelMichael, post: 1719152">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.
    Thanks Michael. Nothing is showing up for it in the log at all. I added it to /var/spool/cron/root
    0
  • cPanelMichael
    [quote="wswd, post: 1719461">Thanks Michael. Nothing is showing up for it in the log at all. I added it to /var/spool/cron/root
    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.
    0
  • kdean
    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.
    0
  • wswd
    [quote="cPanelMichael, post: 1719471">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.
    Yes and yes. Kdean, will give it a try.
    0
  • wswd
    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: [QUOTE]...after throwing an instance of 'St9bad_alloc' what(): std::bad_alloc
    Again, the script runs great when I run it from the command line. No issues at all.
    0
  • cPanelMichael
    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.
    0
  • wswd
    [quote="cPanelMichael, post: 1722251">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.
    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.
    0
  • cPanelMichael
    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.
    0
  • iserversupport
    Is it a dedicated server or VPS if so try to run it from command line using sh script.sh If its working add the cron as root using command crontacb -e
    0

Please sign in to leave a comment.