-1

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?

ThisSuitIsBlackNot
  • 23,492
  • 9
  • 63
  • 110
nikki
  • 1
  • 3
  • 1
    Please provide the code that's not working. – Jonnix Jun 03 '15 at 15:34
  • I am using following command - $output=shell_exec("perl test.pl '".$var1."' 2>&1 &); if I do $output=shell_exec("perl test.pl '".$var1."' >/dev/null 2>&1 &); then the $output is null. – nikki Jun 04 '15 at 14:20
  • You can't have the output _and_ not wait for the response. It's one or the other in this case. An alternative would be to write the output to a file as @Devon suggests and checking the file contents when the perl process is complete. – Jonnix Jun 04 '15 at 14:25
  • Thanks for your reply, is there a way around this where instead of using /dev/null I can write the output in other file? for example shell_exec("perl test.pl '".$var1."' > path/to/dir/outputfile.txt /dev/null 2>&1 &) – nikki Jun 04 '15 at 15:00
  • Sure, just like that just without the `/dev/null` bit. – Jonnix Jun 04 '15 at 15:01

2 Answers2

2

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.

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
  • When I use this command in shell_exec like -- $output=shell_exec("perl test.pl '".$var1."' >/dev/null 2>&1 &); then $output is empty. I would like to save values $output in text file. – nikki Jun 04 '15 at 11:39
  • You can't have it both ways. You can either wait for it to complete or send it to the background. Maybe you need to think of different ways to get the output. – Devon Bessemer Jun 04 '15 at 18:25
  • Thanks for your comment, do you have any suggestion? At the moment, my webpage can handle only one user when using $output=shell_exec("perl test.pl '".$var1."' 2>&1 &); Is there any other way to put theresult in outptu.txt file and multiple user can access this website? – nikki Jun 05 '15 at 12:15
  • That's really beyond the scope of this question and stackoverflow. – Devon Bessemer Jun 05 '15 at 13:09
0

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!

nikki
  • 1
  • 3