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?

5
  • give the full path of the source folder like /var/www/cloud/confirm.php Commented Aug 31, 2016 at 10:22
  • Possible duplicate of Error while executing php script from bash Commented Aug 31, 2016 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. Commented Aug 31, 2016 at 10:44
  • Maybe see also stackoverflow.com/questions/27071051/… Commented Aug 31, 2016 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. Commented Aug 31, 2016 at 14:26

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

3 Comments

If I don't send the command to the background with &, php waits for the command to be completed which may take hours.
This solution will still work if you put the background ampersand back on and remove the output and return vars.
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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.