3

I have this exec command which runs just fine, but doesn't free up the browser (ie. there is a php timeout issue, but the actual command continues to run):

exec("/usr/local/php53/bin/php csv.php $file $user > /dev/null");

When I run ps auxw I see these two running processes:

sh -c /usr/local/php53/bin/php csv.php /tmp/php9Pwu9e 294  >
/usr/local/php53/bin/php csv.php /tmp/php9Pwu9e 294

However, when I run the code below, the browser immediately is free, but the command doesn't continue to run:

exec("/usr/local/php53/bin/php csv.php $file $user > /dev/null &");

When I run ps auxw I see one running process that dies after 16 seconds (and seems to eat up memory quickly and use a lot of CPU%):

/usr/local/php53/bin/php csv.php /tmp/php9Pwu9e 294

Then the process dies without having actually done anything. Not sure what the ampersand is doing that would cause this.

Also why does sh -c appear when no ampersand is present at the end? I feel that this may be indicative of something, but have no idea what.

EDIT:

Because this keeps cropping up as an answer, I have also tried:

exec("/usr/local/php53/bin/php csv.php $file $user > /dev/null 2>&1 &");

Which demonstrates the same issue as mentioned above. The code below, does run, but does NOT free up the browser.

exec("usr/local/php53/bin/php csv.php $file $user > /dev/null 2>&1");
kylex
  • 14,178
  • 33
  • 114
  • 175

3 Answers3

0

try this instead

exec("nohup php csv.php $file $user > /dev/null &");

Kuldeep Singh
  • 696
  • 10
  • 25
0

Try both redirecting both the output and the errors to /dev/null (or another output file of your choice, provided that it has write permissions set for the user running the web server), as well as making it a background process with the ampersand, as stated on a PHP manual user note:

<?php

exec("php /var/www/foo.php > /dev/null 2>&1 &");

Just tried it and it works as expected: the calling script finishes right away, and the background process keeps running for as long as it takes to complete.

Diego Agulló
  • 9,298
  • 3
  • 27
  • 41
0

The issue was much simpler than I had thought. The problem was the $file (which was an uploaded file being stored in the /tmp directory) was not found. I'm not sure why, but I imagine it has something to do with the file being destroyed before the background process can get to it.

The solution was to move_uploaded_file() the file to a permanent location, and run the script with that file. That worked.

kylex
  • 14,178
  • 33
  • 114
  • 175