1

I am trying to achieve similar to what is being asked here running console app from php script. But as i see no concrete answer here so reposting my question specific to my environment.

Disclaimer: I do not have PHP scripting knowledge nor a regular programmer on web techologies. Thus far whatever I achieved is through googling only. Please answer thinking me as beginner. Also bear with my long descriptions of usecase but i believe its important to describe my target and development setup

I have pjsua a command line based soft phone application compiled for Raspberry pi(see snapshot here http://www.pjsip.org/pjsua.htm). This is console app, which after run, expects key board input from user. example 'm' to make calls , 'q' for quit etc.

I have created a bash shell script in my home directory named 'pjsip' which has start() / stop() / restart() interfaces implemented and looks like:

DAEMON="/usr/bin/pjsua"
DEFAULT="/home/pi/pjsip_default"
START="1"

. $DEFAULT "$2"

case "$1" in
start)
    if [ "$START" = "0" ] ; then
        if test -z "$OPTIONS" ; then
            echo "You have to define OPTIONS in $DEFAULT before running pjsip"
        fi
        exit 0
    fi

    echo -n "Starting $DESC: "
    $DAEMON $OPTIONS
    ;;

This all is working when I run ./pjsip start locally on Raspberry pi. pjsip shell script invokes /usr/bin/pjsua with command line args in console mode.

Next I setup apache web server on Raspberry pi and hosted a web page created in PHP which user can access from remote PC over internet and enter sip server IP details.Intention is when user press 'submit' button then backend softphone app(which is pjsua) running on server can be started/restarted with user passed-in details.

Now here is stuck point:

When user presses submit button, I am invoking a shell script 'pjsip' (as mentioned above) from a PHP page to starts my soft phone app with user passed in details using command 'shell_exec('/home/pi/pjsip',$output). I am able to invoke a shell script from php but with two problems:

1) it is directing all console output to php page - for this I commented out $echo output

2)it is killing itself with error "Cannot switch back to console from file redirection" - this is main error.

To resolve second problem rather than modifying a code, would like to ask is there any way in PHP to launch a backend app independently using shell script so that it once launched, remain active and kill/quit only when user intentionally press 'q' through keyboard? i.e. it should run in console mode only.

Is it possible through PHP? If not, any other alternative way exists to solve problem??

In current implementation, my PHP script looks like:

<?php
if($_SERVER["REQUEST_METHOD"] == "POST")
{ $output = shell_exec('/home/pi/pjsip start');
      exit();
     }
?>
<html>
   <body>
      <form action = "<?php $_PHP_SELF ?>" method = "POST">
         Name: <input type = "text" name = "IP" /><br/>
          <input type = "submit" />
      </form>

   </body>
</html>

I referred to many links on google including various options given here: php execute a background process . But nothing helped me. I am stuck with this issue. Please help me with your valuable input. If its better doable through some other approach example java script, please suggest me that too.

Community
  • 1
  • 1
  • If you don't want console output to display why do you have `echo $output;` in your php? – But those new buttons though.. Aug 07 '16 at 09:54
  • As i said am very new to PHP scripting. Thus, you may please correct me on correct syntax I should be using to achieve my target. What command should be in php so that app gets installed in console mode only? Thanks. – shallyverma Aug 07 '16 at 10:04
  • I don't know anything about **pjsip**, I'm just addressing problem #1 you mentioned: *"1) it is directing all console output to php page."*. To fix that just get rid of `echo $output;`. – But those new buttons though.. Aug 07 '16 at 10:06
  • Ok. Tried that. It just disable the output on page. But still application is not active. Believe question as such is not specific to pjsip but any console based app which is interactive in nature. Example, an app waiting for user input from keyboard and printing an output on stdout and exit only if use rpress 'q'. can we launch such apps independly from pHP scripts? – shallyverma Aug 07 '16 at 10:20
  • I don't really understand what you're asking. Are you trying to feed user input to the console after the script has run? – But those new buttons though.. Aug 07 '16 at 10:23
  • I am trying to invoke a console based application from PHP script using a shell script. I have posted details above. – shallyverma Aug 07 '16 at 10:37

0 Answers0