Results 1 to 7 of 7

Thread: Exact instructions to execute PHP script from cron

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2007
    Posts
    4

    Question Exact instructions to execute PHP script from cron

    Spry Forums,

    I'm trying execute a PHP script with cron, similar to what is described in another thread (which I can't link to because I haven't made 15 posts.)

    However, that thread left me a little confused as to how exactly to structure the PHP script so that it will execute.

    Should my PHP script look like this:

    Code:
    #!/path/to/php
    <?php
    (here I'm intending to put code in that will check a POP3 mail account for messages)
    ?>
    What is the correct path to the PHP executable to use?

    In the crontab interface, do I use the full path to the PHP script:
    * * * * * /home/mywebsite/public_html/cronjob.php

    Thank you for helping me to be clear on this.

  2. #2
    Former employee Newbie
    Join Date
    Jul 2006
    Posts
    57

    Default

    Hello,

    You would need to put "php" before the path to the PHP script, in order to pass the file as an arguement to the PHP cli (Command Line) interpreter:

    * * * * * php /home/mywebsite/public_html/cronjob.php

    I would not recommend having the cronjob run every minute, though.

    --
    Max V.
    Spry Support

  3. #3
    Junior Member Newbie
    Join Date
    Jul 2007
    Posts
    4

    Question

    Thank you for responding.

    The syntax you suggested does get my cron job to work.

    The every minute setting was just for testing. However, eventually I'd like to run the PHP script every ten or 15 minutes.

    Two follow up questions:

    1. Email reporting:
    I entered in my admin email address in the cPanel cron interface. On every successful execution of the cron job, it sends me a message with my cron command in the subject line, and a body text that looks like this:
    X-Powered-By: PHP/5.1.4
    Content-type: text/html

    Is there a way to set it so that the cron only sends emails to my address on some kind of error or failure? If not, can I suppress emails entirely, and/or use log files?

    2. Memory and temp files:
    Should I be concerned about whether or not the PHP being executed generates any temp files, and if so, should I be adding additional parameters to clean up any memory used? I just want to be sure I'm using the server efficiently.

    Thank you for your time and advice.

  4. #4
    Forum Administrator Power Poster Lyle@Spry's Avatar
    Join Date
    May 2005
    Posts
    455

    Default

    Quote Originally Posted by Dave M G
    1. Email reporting:
    I entered in my admin email address in the cPanel cron interface. On every successful execution of the cron job, it sends me a message with my cron command in the subject line, and a body text that looks like this:
    X-Powered-By: PHP/5.1.4
    Content-type: text/html

    Is there a way to set it so that the cron only sends emails to my address on some kind of error or failure? If not, can I suppress emails entirely, and/or use log files?
    cron sends any output your script generates. So to supress sending anything except failures, ensure that your script generates zero output on success. (cron will not send a message if the command doesn't produce any output.) Log files are always an option, good place to have your script write informational messages (success) but then output errors/warnings to stdout (so cron delivers those to you in email.)
    Quote Originally Posted by Dave M G
    2. Memory and temp files:
    Should I be concerned about whether or not the PHP being executed generates any temp files, and if so, should I be adding additional parameters to clean up any memory used? I just want to be sure I'm using the server efficiently.
    Yes. AFAIK php will NOT delete temporary files you create in the script unless you specifically tell it to do so. Memory _should_ be freed automatically on php interpreter exit (end of script), unless you are manually allocating memory with malloc (does PHP even support that?) in which case you must still 'free' the memory yourself.

  5. #5
    Junior Member Newbie
    Join Date
    Jul 2007
    Posts
    4

    Default

    Thank you for responding.

    o to supress sending anything except failures, ensure that your script generates zero output on success.
    I think perhaps you misunderstood me. My current test script shouldn't generate any output.

    Or, to be more accurate, the output I showed above:
    X-Powered-By: PHP/5.1.4
    Content-type: text/htm
    ... is not output I intended for the script to create. So why is it creating that output, and how do I stop it?

  6. #6
    Forum Administrator Power Poster Lyle@Spry's Avatar
    Join Date
    May 2005
    Posts
    455

    Default

    Quote Originally Posted by Dave M G
    I think perhaps you misunderstood me. My current test script shouldn't generate any output.

    Or, to be more accurate, the output I showed above:
    X-Powered-By: PHP/5.1.4
    Content-type: text/htm
    That is output, this '' (ignore the quotes) is not output. If cron sees anything come out of the script at all (including those HTTP headers) it sends them.
    Quote Originally Posted by Dave M G
    ... is not output I intended for the script to create. So why is it creating that output, and how do I stop it?
    PHP is meant to be used primarily as a WEB scripting language, somewhere in your script it is sending the required (when used in a web scripting context) http headers. Post the code of your script, maybe someone familiar with using PHP as a shell scripting language can spot where they are being generated. (Usually at the very top of the script.)

  7. #7
    Member Newbie
    Join Date
    Oct 2006
    Posts
    46

    Default -q flag suppresses html headers

    use 'php -q /path/to/file' as your command in cron, and then no output will be generated, except what your script explicitly sends to stdout.

    The -q flag will keep php quiet and is recommended for running scripts via cron.

Bookmarks

Posting Permissions

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