Executing php with crontab

后端 未结 6 977
误落风尘
误落风尘 2020-11-27 19:08

I\'m trying to run a php-script on a scheduled basis. So I\'d thought crontab was a good idea. The server I\'m using is on Linux which I\'m not that familiar with. So the pr

相关标签:
6条回答
  • 2020-11-27 19:32

    You can also use env, it will find and launch php for you:

    /usr/bin/env php /var/www/some/path/script.php
    

    Or you can place a shebang in your script.php (first line):

    #!/usr/bin/env php
    

    then make it executable, and make crontab call it directly, like in your first example:

    5  * * * * /var/www/some/path/script.php
    
    0 讨论(0)
  • 2020-11-27 19:36

    Start by typing at a command line:

    whereis php

    Do this as the user that the cron job will be run under. This will show you the path to your executable. You can then use that path (if it's not already in your PATH variable) in your cron entry:

    5 * * * * /your/path/to/php /var/www/some/path/script.php

    Edit: you may need to install the php5-cli (Ubuntu package name) package if all you have is the Apache PHP module installed. This will give you the binary executable that you can run from a command line.

    0 讨论(0)
  • 2020-11-27 19:36

    You can use the wget command locally:

    5  * * * * wget http://localhost/some/path/script.php
    
    0 讨论(0)
  • 2020-11-27 19:37

    I had to find to follow a trail to find the executable:

    andy@ararat:~$ type php
    php is /usr/bin/php
    
    andy@ararat:~$ file /usr/bin/php
    /usr/bin/php: symbolic link to `/etc/alternatives/php'
    
    andy@ararat:~$ file /etc/alternatives/php
    /etc/alternatives/php: symbolic link to `/usr/bin/php5'
    
    andy@ararat:~$ file /usr/bin/php5
    /usr/bin/php5: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, stripped
    

    so you need to include /usr/bin/php5 as the path to the php executable like so:

    andy@ararat:~$crontab -e
    #*/1 * * * * /usr/bin/php5 /home/andy/www/dev.com/corp_rewards_cron.php
    
    0 讨论(0)
  • 2020-11-27 19:46

    Is this a Linux system?

    In newer Linux distributions, there is
    actually a convienient crontab-setup system
    that doesn't require any entry in the crontab by the user. E.g in SuSE Linux, you have directories

    /etc/cron.hourly/
    /etc/cron.daily/
    /etc/cron.monthly/
    /etc/cron.weekly/
    

    Just put an invocation script (konno_php_start) in any of these directories, like

    /etc/cron.hourly/konno_php_start
    

    which is executable (chmod 755 or so) and contains:

    #!/bin/sh
    cd /var/www/some/path/
    php  script.php >> logfile.txt 2>&1
    

    and restart the cron daemon. Thats it.

    From the logfile, you'll see if your php interpreter
    will be found in the PATH. If not, change the line in /etc/cron.hourly/konno_php_start to

    /full/path/to/php  script.php >> logfile.txt 2>&1
    

    Regards

    rbo

    0 讨论(0)
  • 2020-11-27 19:54

    I suggest that you do like this,

    */5 * * * * /path/gridmon2.pl 1> /dev/null 2> /dev/null
    

    where in you .pl code you should grep using wget or something like this:

    wget "/www/root/index.php"
    

    or you can just do like this:

    /usr/bin/wget "/www/root/index.php"
    

    It's just my suggestion, I've only try wget to external site not locally and it works.

    please try and revert.

    0 讨论(0)
提交回复
热议问题