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?
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
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);