简体   繁体   中英

Running PHP file using command line as background process

I have centos VPS hosting and installed WHM/cPanel . I want to run a php script using command line for unlimited time.

My script is look like:

<?php
set_time_limit(0);
while(true)
{
//code to send me email

sleep(600);
}
?>

I know that this script should be run for unlimited time.

I have used these commands:

php myfile.php &
nohup php myfile.php &

I found these commands on stackoverflow. And these are running fine. But after one hours, It stop automatically.

I think, i am doing right. But i do not know, which is killing that process.

If not,

i want to know that How to run this script for unlimited time.

If you have console access try using cronjob to run this file.

see : https://www.centos.org/docs/5/html/Deployment_Guide-en-US/ch-autotasks.html

also : http://alvinalexander.com/linux/linux-crontab-file-format-example

Make sure you use php-command in cron-job

Note : You'll require admin rights to edit/work with cron-jobs

What you are doing is correct. It should run. I have PHP scripts that run for much longer than an hour. They run for days on end. I also have programs that are not PHP that should run forever, but don't. It is because they die due to a bug in the program. For example, xscreensaver dies on me about once a week. To keep it running, I use this shell script (which you can use to keep your PHP running):

while:
do
    xscreensaver &
    wait
done

Now, running that shell script will start the program again if it ever dies for any reason.

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