开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜