tags:

views:

120

answers:

1

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?

+1  A: 

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.

Rook
This isn't working for me. First, I have some relative path includes inside the script that don't work because the script is being executed from the wrong directory. That is why I had the "cd" statement in my version. Second, when I run that command at the command line, I get the following: nohup: ignoring input and redirecting stderr to stdoutAlso, why do I need the nohup commmand? It looks like that is just to keep a background process running after a user logs out. Is that needed since the apache user won't be logging out?