0

I need to start OpenOffice server in background (if it's not instanciate) then do a file convertion with JODConverter.
The scenario is as follows :

  • The user inputs a *.html file by a form.
  • I get the file by the $_FILES variable in the PHP script corresponding to the form action
  • I run a bash script with shell_exec, and pass the temp path to it, to access the file ine my bash script, then do the convertion

This is the PHP code :

        $fichier = $_FILES['html_file'];
        if($fichier) {
            if(is_uploaded_file($fichier['tmp_name'])) {

                if(move_uploaded_file($fichier['tmp_name'],"/var/www/test/doc/".$fichier['name'])) {
                   $output = shell_exec("./converter {$fichier['name']}");
                   echo $output;
                } 
            } 
        } 

The script "converter" code :

#!/bin/bash
fichier=$1
pid=$$

echo RUNNING sOFFICE :

SERVICE=soffice
if P=$(pgrep $SERVICE)
then
    echo sOFFICE IS ALREADY RUNNING 
else
    echo sOFFICE WILL BE START WAIT 5s PLEASE 
    soffice --headless --accept="socket,host=127.0.0.1,port=8100;urp;" --nofirststartwizard &
    sleep 5
fi

echo CONVERSION START
java -jar ./jodconverter/lib/jodconverter-cli-2.2.2.jar ./$1 ./$1.odt
echo CONVERSION END

My problem is in the first part of the bash script, when I try to run the sOFFICE server in background, this command blocks the final execution of the script, even with the use of &.
I've tried to run this script by the terminal, and it works perfectly.
So I think, the problem come with the php execution of the bash script. Have an idea ?

EDIT :
Solution found thanks to Jim Rubenstein response :

redirect the standard out and standard error streams

: Replace

soffice --headless --accept="socket,host=127.0.0.1,port=8100;urp;" --nofirststartwizard &

by

soffice --headless --accept="socket,host=127.0.0.1,port=8100;urp;" --nofirststartwizard > /dev/null 2> /dev/null &
jbrtrnd
  • 3,815
  • 5
  • 23
  • 41

1 Answers1

4

This problem is typically solved with just adding a & at the end of the command; but it seems like you also need to redirect the standard out and standard error streams. I found responses to the same question on stack overflow here:

php execute a background process

Why this process is not running in background?

Community
  • 1
  • 1
Jim Rubenstein
  • 6,836
  • 4
  • 36
  • 54