简体   繁体   中英

I need to run a PHP script I wrote as a task on my server

I need to run a PHP script which is scraping a website for data, and I need to run it on my VPS which has linux. I want to run it as a task so that I should be able to logout of my VPS and the script should keep running in background.

I've read about CRON job but it's more for like scheduling and repetitive tasks; but I need the PHP script only once.

Is there a way in PHP to do that? Please help, I'm just a newbie to this.

Thanks in advance! :)

I've tried it as a CRON job, but it doesn't seem to serve my exact purpose.

so I run my script like this from terminal.

php scrapethewebsite.php

and then it show this

Started scraping at 10:03:00 20-03-2019


and I can't logout or close my vps/ssh connection.

I look for

php scrapethewebsite.php
Started scraping at 10:03:00 20-03-2019

and then I should be able to logout or close my connection. And then I should be able to shutdown my PC and go for a walk..

Yes, You can do this with the screen. The screen is most of the time already installed in Linux VPS. but still, you can get by command.

apt-get install screen

Its give you the ability to have multiple screens in VPS where you can run multiple tasks at the same time. LIke you have. Get a screen with command.

screen -S sessionname

sessionname will be ur screen name. and you can dispach it with command/ CTRL + A, followed by D. then you can close your putty or any tool from via you accessing your vps. here you go. for more information, you can follow this link. Screen in Linux

Run this in your SSH session, then you can click X and close it and it will still run

nohup php scrapethewebsite.php >/dev/null 2>&1 &

to check if your file is running type this command

top

you should see the php file up there and press spacebar to update the list

_____________________________________________________________________

If your php script has some error from time to time or just runs out of execution time and you want to re-run it when it closes down.. you have to create a .sh file with a while loop inside it and run nohup on it so it will re-run the php file after it errors up.

If you want to use nohup on php file that needs to re-run from time to time, then you should do the following

$ echo 'while true ; do php scrapethewebsite.php ; done > /dev/null' > ~/php_run_loop.sh
$ chmod a+x ~/php_run_loop.sh
$ nohup ~/php_run_loop.sh

NOTE: If you have an error or other problem in your php script, this will cause it to run over and over again without any limit, forever.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM