Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, I'm trying to run a php script from a cronjob and can't seem to get it working. When I run the script from the shell command prompt: # ./myscript.php ...
  1. #1
    Just Joined!
    Join Date
    Feb 2010
    Posts
    2

    run a php script from a cron

    Hi,

    I'm trying to run a php script from a cronjob and can't seem to get it working.

    When I run the script from the shell command prompt:

    # ./myscript.php

    the scripts works fine

    I then inserted a cron job using:

    crontab -e

    and inserted the following:

    3 23 * * * /usr/bin/php /var/pathtoMyscript/myscript.php

    and nothing seems to happen.

    I left off the -q and have a few echo commands within the script but I don't get an email of the output either.

    Any ideas??

    Thanks.

  2. #2
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    616
    i will assume apache is running, perhaps you should invoke your php page using wget. still use cron but try:

    Code:
    /usr/bin/wget http://localhost/path/to/mypagename.php
    If you are wanting output then don't use personal crontabs, use the system's crontab, /etc/crontab. There you can set your mailto and even who to run as. Should give you any output the script generates as well.
    linux user # 503963

  3. #3
    Trusted Penguin elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    2,298
    You could also try

    3 23 * * * /usr/bin/php /var/pathtoMyscript/myscript.php >> /home/usershome/myscript.log

    If you want to run the cron job as root then (under Ubuntu) sudo crontab -e wont do the job reliably. This drove me nuts for hours!

    Instead try

    sudo su -
    crontab -e
    If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)


    My new blog. It's probably not as good as I think it is.

  4. #4
    Just Joined!
    Join Date
    Feb 2010
    Posts
    2
    Thanks for the input everyone. I got it to work by first having the cron navigate to the directory the php was in:

    1 3 * * * cd /pathtoPHPfile/;/usr/bin/php -q -f myscript.php

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...