有没有办法不等待shell_exec完成?

I am using shell_exec() to run perl program which takes more than an hour to complete a task by asking users to enter some values. I would like to run shell_exec() as a background program and refresh the summary page back to index page. I found some suggestions to use '2>/dev/null &' at the end of the shell_exec(), however, it is not working while running my index.php it stays on the same page with waiting sign.

If there is any other trick to handle such situation would also be awsome.

Any suggestion?

You need to reassign stdout as well as stderr.

command >/dev/null 2>&1 &

> is used to send all standard output to /dev/null. 2> is sending all the errors. &1 means to send it to the same place as standard output.

This could also be used with a log file

command >>command.log 2>>&1 &

>> will append instead of overwrite.

Finally, I managed to make system() work accordingly to the desire output using cgi instead of PHP, which was failrly simple. I think similar solution can be found in PHP:

# fork this process
 my $pid = fork();
 die "Fork failed: $!" if !defined $pid;
 if ($pid == 0) {
# do this in the child
 open STDIN, "</dev/null";
 open STDOUT, ">/dev/null";
 open STDERR, ">/dev/null";
 system("perl script.pl > output.log");
 }

Hope this will be useful!