简体   繁体   中英

Running multiple system commands in background via php system() command?

I want to call a script on my server by opening a url. The script takes a long time to execute, so I want to run it in the background so there is no need to keep the browser open and worry about timeouts.

It looks like the PHP function system() should work, but it isn't working for me.

The script must execute from within the correct directory so that all relative paths are correct. Here is the system command I would like to execute:

cd /path/to/script/directory/;php my_php_script &

And this is the latest thing I've tried to no avail.

system('(cd /path/to/script/directory/;php my_php_script & > /dev/null)');

Anyone know how to do this?

On a Unix like system such as Linux this is very easy to do. Nohup is the command you need to start with, and the following commands will continue to run. The commands will even run if the script exits or a the user logs out. This is very similar to your command but it is easier to read.

system("nohup php /path/to/script/directory/my_php_script > /dev/null &");

PHP's garbage collector is prone to leaking memory and a simple script can eat up gigabytes of memory in a matter of day. It is better to schedule php scripts to run as cron jobs that exit within hours or minutes so that memory can be freed.

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