Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


cron job is not updating the database
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

cron job is not updating the database

AICAIC Member

Hi,

I created a php file and when i am running the file through browser it is providing the output and update the database, i run the file through terminal and it is working fine.

when i run the file through cron it is not creating any error, but also not updating the database entries as well.

can anybody help to resolve this issue.

Comments

  • edited May 2015

    Check the logs to see if the cron is even running.

  • BharatBBharatB Member, Patron Provider

    Could you post the contents of your cron file pertaining to the php file ?

  • AICAIC Member

    @aggressivenetworks said:
    Check the logs to see if the cron is even running.

    I do check and the cron is running.

    @BharatB said:
    Could you post the contents of your cron file pertaining to the php file ?

    the cron entry is */5 * * * * php -q /path/server/status/getserverstatus.php

  • BharatBBharatB Member, Patron Provider
    edited May 2015

    If error reporting is enabled then try

    php -q /path/server/status/getserverstatus.php

    In command line. You'll get your error.

    or enable error reporting by error_report(E_ALL);

  • AICAIC Member

    when i am going to run the php file through browser the database updated and the update query is in the php file.

    when i run the same file from the terminal the database entries updated successfully. but when i am trying to run the file through cronjob the job run but the db is not updating the values.

  • AICAIC Member

    @BharatB said:
    If error reporting is enabled then try

    php -q /path/server/status/getserverstatus.php

    In command line. You'll get your error.

    or enable error reporting by error_report(E_ALL);

    I run the command through terminal and it is working fine. that's why i list in my previous comment. the db is also updating the value when i run through terminal. but it is not going to update the db records when it is running through cron.

  • BharatBBharatB Member, Patron Provider
    edited May 2015

    Could you try , which php or where php or try with php5 you might get an output like

    /usr/bin/php or /usr/bin/php5

    Now use that to make your cron job look like

    */5 * * * * /usr/sbin/php5 -q /path/server/status/getserverstatus.php

    or

    */5 * * * * /usr/sbin/php -q /path/server/status/getserverstatus.php

  • What's about ownership (chown) and permissions (chmod) of cron file?.

  • AICAIC Member

    @mustafaramadhan said:
    What's about ownership (chown) and permissions (chmod) of cron file?.

    it is root:root

    i have change it to the web user but no success. i did one more thing. i change the script and push the output to a txt file.

    where i get the following error.
    PHP Warning: require(..f/init.php): failed to open stream: No such file or directory in /var/myserver/getserverstatus.php

  • KeithKeith Member

    Try adding cd /var/myserver/; before the php command.

  • BharatBBharatB Member, Patron Provider
    edited May 2015

    Do one thing

    if(PHP_SAPI == 'cli' && $_SERVER['REMOTE_ADDR'] == '127.0.0.1')

    {

    //code here

    }


    use that and then use

    */5 * * * * /usr/bin/GET http://localhost/path/to/whatever/blabla.php

  • sleddogsleddog Member
    edited May 2015

    In a script to be run by cron, always use absolute paths. Cron does not have the same environment or path settings that you have as a user.

    Thanked by 3yomero JustAMacUser AIC
  • edited May 2015

    Create /tmp/cron.sh file with content:

    #!/bin/sh
    cd /path/server/status/
    exec php -q getserverstatus.php

    chmod to 755 and then create cron with content '*/5 * * * * /tmp/cron.sh

  • @mustafaramadhan said:
    Create /tmp/cron.sh file with content:

    Why would the OP create a cron to a script in /tmp? It's ephemeral..

    Like others have said, use absolute paths.

    Also, @AIC, unless you need the script to run with root privileges, it's a best practice to run with only the privileges that you need (e.g. www-data, or some other user). And as @BharatB suggested, if the script should only be run from CLI, it's a good idea to check that right at the beginning before doing anything.

  • The reason why in /tmp because permissions always 1777 and then ownership unnecessary.

  • @mustafaramadhan said:
    The reason why in /tmp because permissions always 1777 and then ownership unnecessary.

    Right. But it's tmp. One should never rely on things to be there, that's why it's called tmp (i.e. temporary).

    Plus, do you see anything inherently insecure about running scripts from a location like that? Some people even mount /tmp with noexec, so the solution wouldn't work under those situations.

    Notwithstanding, you're advising the OP to workaround a permission/ownership issue when it hasn't even been established that that is the cause. Even if it was, the correct solution is to fix the permissions and ownership, not workaround the issue... And it likely isn't an issue since the OP was running the cron job as root.

  • yomeroyomero Member

    @sleddog said:
    In a script to be run by cron, always use absolute paths. Cron does not have the same environment or path settings that you have as a user.

    This

  • Eliminate other factors, test the code and then if running well, move the code to other safe place. OK?.

    I know about secure and it's my concern for my Kloxo-MR.

  • howardsl2howardsl2 Member
    edited May 2015

    If not used in "crontab", you may need to put a username before your command:

    */5 * * * * RUN-AS-USERNAME /usr/bin/php -q /path/server/status/getserverstatus.php
    
  • edited May 2015

    Try this :

     . $HOME/.profile; /path/server/status/getserverstatus.php
    

    If it doesn't work, try this:

     . $HOME/.bash_profile; /path/server/status/getserverstatus.php
    

    Keep the

    .

    it means source

  • madtbhmadtbh Member
    edited May 2015

    You could always run it via curl:
    * * * * * curl --silent "http://status.example.com/cron.php" >/dev/null 2>&1

    Updated: Fixed time thanks to @TinyTunnel_Tom

  • @madtbh said:
    You could always run it via curl:
    */1 * * * * curl --silent "http://status.example.com/cron.php" >/dev/null 2>&1

    Why */1? Just * is enough

    Thanked by 1madtbh
  • madtbhmadtbh Member

    @TinyTunnel_Tom said:
    Why */1? Just * is enough

    My bad, I changed it from */20.

    Thanked by 1TinyTunnel_Tom
  • AICAIC Member

    @sleddog said:
    In a script to be run by cron, always use absolute paths. Cron does not have the same environment or path settings that you have as a user.

    You are right, cron doesn't take the relative path. I have provided the absolute path and then it run correctly.

    I did all the options before changing to absolute path.
    change the permission, change the owner of all the include files, change the cron to my webuser, check the time stamp of cron and php etc.

    None of them were successful.

    So the last option was i open the serverstatus.php file and change the requireonce (../init.php) to requireonce(/path/to/my/init.php);

    Thanks for help everyone.

    The issue is resolved now. :)

  • sleddogsleddog Member
    edited May 2015

    AIC said: The issue is resolved now. :)

    Glad to hear you got it working.

    If hard-coding the path becomes a nuisance for portability, you can have a look at PHP's getcwd() and dirname(__FILE__). There's a nice explanation and comparison here.

  • AICAIC Member

    @sleddog said:

    Thanks i will look into this.

  • raindog308raindog308 Administrator, Veteran

    You can set PATH in your crontab file.

    Another useful thing you should always set is MAILTO. I suspect every time this failed, mail was generated to someone...if it had been you, you would have seen the error immediately.

Sign In or Register to comment.