How to run the PHP script at scheduled time

后端 未结 4 884
梦谈多话
梦谈多话 2020-11-30 15:17

I need to run a php script at the scheduled time daily to update some fields in database and to send automated email. How I can do this?

Is it possible to write so

相关标签:
4条回答
  • 2020-11-30 15:56

    Just create the script that does the required job, test it by hitting the URL in your browser once you are sure it works right. Copy the URL and add a Cronjob

    Then schedule this command to run at whatever time you want to run

    php ABSOLUTE_URL_TO_SCRIPT >> logfile
    

    The log file is optional. But it will give you a chance to see what happened.

    For example if you want to run your script every 4 hours, and assuming your script is at http://localhost/work/scripty.php and assuming that your http root is /var/www,

    you would run "crontab -e" in terminal and add the following line:

    * */4 * * * php /var/www/work/scripty.php
    

    If you need more information just comment I will update the answer.

    0 讨论(0)
  • 2020-11-30 15:57

    You should use a Cron job to do it. Check out the examples on the Wikipedia page.

    The Cron Job should call a script using the php executable that runs the necessary task.

    0 讨论(0)
  • 2020-11-30 16:00

    PHP cannot run script by itself,since php doesn't have daemons like python !! So you have to take OS help to invoke your custom script .

    For example in linux : (example.sh) export USE_PHP=php cd $SCRIPT_ROOTDIR $USE_PHP -f cronfile.php service="checkdatabase" (service is the parameter passed to your cronfile).

    For setting up cron jobs ,have a look at this link http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/

    0 讨论(0)
  • 2020-11-30 16:04

    In Linux, We can create .sh file and can give a specific time to run that is called cron job. SO should use this method just make a shell file and give a time period to it. You should take a help with linux expert for that.

    Use the following: Cron Job

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