0

My below code works fine to execute a bash script in the background from PHP:

$cmd = "(touch $run_file && java -jar $trimmomatic SE -threads 8 $file $trimmed_file HEADCROP:5 && rm $run_file)";
exec($cmd . " > /dev/null &");

What I want to add is to execute an external php file upon the completion of this command.

I tried this it but didn't work:

$cmd = "(touch $run_file && java -jar $trimmomatic SE -threads 8 $file $trimmed_file HEADCROP:5 && rm $run_file && php -f confirm.php)";
exec($cmd . " > /dev/null &");

How can I make sure to execute confirm.php upon completion of the bash script?

Ömer An
  • 600
  • 5
  • 16
  • give the full path of the source folder like /var/www/cloud/confirm.php – JYoThI Aug 31 '16 at 10:22
  • Possible duplicate of [Error while executing php script from bash](http://stackoverflow.com/questions/8700426/error-while-executing-php-script-from-bash) – LF00 Aug 31 '16 at 10:40
  • The trivial solution is to not run it in the background. If you need some other processing to continue on the PHP side while Bash runs the background command, you need to be more specific about how exactly the running PHP process should be notified when Bash is done with the background process. In real programming languages, you get a signal, but I don't know if that's available / reliable in PHP. – tripleee Aug 31 '16 at 10:44
  • Maybe see also http://stackoverflow.com/questions/27071051/sigchild-not-catching-signal-when-child-process-dies – tripleee Aug 31 '16 at 10:45
  • @tripleee If I don't send the command to the background with `&`, php waits for the command to be completed which may take hours. I catch the success or the failure of the command from the output files produced. – Ömer An Aug 31 '16 at 14:26

2 Answers2

0

1)give the full path to php file

2)pass the second and third parameter and know the output and return value.

$cmd = "(touch $run_file && java -jar $trimmomatic SE -threads 8 $file $trimmed_file HEADCROP:5 && rm $run_file && php -f /var/www/cloud/confirm.php)";

exec($cmd . " 2>&1", $output, $return_var);

print_r($output);
 print_r($return_var);
JYoThI
  • 11,977
  • 1
  • 11
  • 26
  • If I don't send the command to the background with `&`, php waits for the command to be completed which may take hours. – Ömer An Aug 31 '16 at 14:21
  • This solution will still work if you put the background ampersand back on and remove the output and return vars. – tripleee Aug 31 '16 at 14:43
  • I checked and it does not work, however that is not my problem, nor absolute path fixes it, hence this is not a solution. I don't care about the return values, my problem is `confirm.php` just doesn't get executed, although it works if I go to that page and refresh. The only way to save my day is to append the bash code in `confirm.php` to this page rather than calling it. – Ömer An Aug 31 '16 at 14:56
0

Finally I fixed my problem. I am posting here the solution if anybody else gets stuck at the same point as I did and desperately reach here.

In my confirm.php file, I included another file which contained functions as well as a Class object. The command line cannot execute methods in the Class object unless it is called from the original file with echo or explicitly passing the class to the command like php -r 'include "Class_file.php"; echo MyClass::method(); '. Since my confirm.php file depended on this Class for further processing, it failed here. This post helped me to figure it out:

How do you execute a method in a class from the command line

Community
  • 1
  • 1
Ömer An
  • 600
  • 5
  • 16