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.