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.

Shell cron

Discussion in 'General Discussion' started by php4ever, Dec 31, 2008.

  1. php4ever

    php4ever Member

    Joined:
    Sep 5, 2006
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    I have a very simple script that runs when I use the command line method but when I add it to a cron it simply wont run.

    My script is called "updateresi.sh" and contains two lines as follows;

    #!/bin/sh
    php -q /updates/run_interactive_job.php

    Then my CRON looks like;

    40 2 * * * /updates/updateresi.sh
    40 2 * * * bash /updates/updateresi.sh (I tried this method to)

    When I run it manually it works just fine using the following command line;
    bash /updates/updateresi.sh

    Also very important to note, I shell in as root and the folder for updates is absolute from the root meaning its root-folder/updates/updateresi.sh

    Any ideas what I could be doing wrong here?

    ~ Jared
     
  2. sparek-3

    sparek-3 Well-Known Member

    Joined:
    Aug 10, 2002
    Messages:
    1,381
    Likes Received:
    23
    Trophy Points:
    38
    cPanel Access Level:
    Root Administrator
    The cron's shell environment probably doesn't have the correct path to the php binary.

    When you log into shell and run the script successfully type:

    Code:
    which php
    to reveal the full path to the php binary. Then replace the call to the php binary in your /updates/updateresi.sh script to include this full path. Then let the cron run and see if this run successfully.

    You can also add a:

    Code:
    MAILTO="your@address.com"
    line at the top of your crontab, this will send any errors or output from your cron tasks to the e-mail address given.
     
  3. php4ever

    php4ever Member

    Joined:
    Sep 5, 2006
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    Yeah the full path to PHP is /usr/local/bin/php and I've added that to the shell script with no success. I'll try the email method you suggest and report back.

    ~ Jared
     
  4. sparek-3

    sparek-3 Well-Known Member

    Joined:
    Aug 10, 2002
    Messages:
    1,381
    Likes Received:
    23
    Trophy Points:
    38
    cPanel Access Level:
    Root Administrator
    Actually you may need to also give the full path to the updateresi.sh script.

    The general rule of thumb that I take with cron entries is to always use the full path for everything. Never assume anything.

    Where is this script located at? Is it located at /home/user/updates/updateresi.sh if so then I would give this full path in the cron entry.

    Also make sure the script is executable

    Code:
    chmod 755 /home/user/updates/updateresi.sh
     
  5. php4ever

    php4ever Member

    Joined:
    Sep 5, 2006
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    Actually its quite literally in the root path. I log into the cpanel server with my root credentials (its a leased server) and I do the following.

    Here are examples from the shell
    root@server [~]# cd /
    root@server [/]# cd /update
    root@server [/update]#

    Then if I want to manually run the sh script I do this
    root@server [/update]# cd /
    root@server [/]# bash /update/updateresi.sh

    It runs fine exactly as it should.

    I then do
    root@server [/]# crontab -e
    I set the cron as follows;

    45 9 * * * bash /update/updateresi.sh

    The contents of my updateresi.sh script is;

    #!/bin/sh
    php -q /updates/run_interactive_job.php

    When I added the updateresi.sh I had already CHMOD +x updateresi.sh
     
  6. sparek-3

    sparek-3 Well-Known Member

    Joined:
    Aug 10, 2002
    Messages:
    1,381
    Likes Received:
    23
    Trophy Points:
    38
    cPanel Access Level:
    Root Administrator
    Set the crontab entry to just run the php script directly, no need for an extra bash scripting layer.

    45 9 * * * /usr/local/bin/php -q /updates/run_interactive_job.php
     
  7. php4ever

    php4ever Member

    Joined:
    Sep 5, 2006
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    do I have to stop and start something to make the cron take effect. I added it exactly as you have and I added the mailto feature at the top and nothing.

    J
     
  8. sparek-3

    sparek-3 Well-Known Member

    Joined:
    Aug 10, 2002
    Messages:
    1,381
    Likes Received:
    23
    Trophy Points:
    38
    cPanel Access Level:
    Root Administrator
    No, you shouldn't have to restart anything, although if the cron daemon isn't running, then it won't run. To restart crond use:

    /etc/init.d/crond restart

    If you use the exact line as given, you will have to wait until 9:45AM server time comes back around for the task to be executed.
     
Loading...

Share This Page