Cant open Gnome terminal from php - php

I cant seem to open gnome terminal from php script
tried DISPLAY=:0 did not work
php code:::
<?php
shell_exec("/opt/lampp/htdocs/py/test.sh");
?>
test.sh shell script code
#!/bin/sh
gnome-terminal --working-directory=/opt/lampp/htdocs/py -x python3 MaxTemperature.py

try this one
shell_exec('DISPLAY=:0 bash /opt/lampp/htdocs/py/test.sh');
system.sh file should be like this
SCRIPTCURRENT=`readlink -m $0`
SCRIPTCURRENTPATH=$(dirname "$SCRIPTCURRENT")
runintoterminal () {
if ! [ -t 1 ]; then
DISPLAY=:0 gnome-terminal --working-directory=/opt/lampp/htdocs/py -x python3 MaxTemperature.py
exit 0
fi
}
runintoterminal $SCRIPTCURRENT

Related

PHP shell_exec - bash kill httpd.exe then run httpd.exe again (need to restart Apache on Windows without service installed)

I have a php script that running on my local Windows machine and i need to restart Apache server from that script. I may stop Apache from that script but can't start it because it is being stopped and php execution after apache-stop is interrupted.
My php code is
shell_exec('bash -c "source ~/.bashrc && apache-stop && some-command && apache-start"');
//bash.exe is in PATH already
Bash functions ~/.bashrc
function apache-stop {
( tskill httpd )
if [[ $? -eq 0 ]]; then
echo "Apache stopped."
fi
}
function apache-start {
tasklist | grep -a "httpd.exe" > /dev/null
if [[ $? -eq 0 ]]; then
echo "Apache already running."
else
echo "Start Apache."
( /c/xampp/apache/bin/httpd.exe & )
fi
}
I use Apache on Windows 8.1 only as console application and run/kill it from bash console, hence i can't use Apache as a Windows Service. How to run apache-stop, some-command and apache-start from that php script, may be there is a way to run externally all this bash functions?

Open gnome-terminal from php script

I want to try to open gnome-terminal from php script:
shell_exec('bash /data/manager/application/.shell/system.sh');
This script use a function to check terminal:
SCRIPTCURRENT=`readlink -m $0`
SCRIPTCURRENTPATH=$(dirname "$SCRIPTCURRENT")
runintoterminal () {
if ! [ -t 1 ]; then
gnome-terminal -e "bash $1"
exit 0
fi
}
runintoterminal $SCRIPTCURRENT
I've tried:
shell_exec('gnome-terminal');
But it's doesn't work... (I know it's possible...) But how to ?
I use nginx and php-fpm. With my own socket. nginx and socket use my user and not www-data. (I'm on ubuntu 14.04LTS)
I've try 0777 rights...
My bash script can run from netbeans IDE ans terminal... but not from php...
The problem is most likely that gnome-terminal doesn't know where it should draw itself. Normally it shows up on the same display as the program that launched it (IDE, terminal), but web servers don't have displays so it doesn't know where to go. You can try DISPLAY=:0 gnome-terminal -e "bash $1" to show it on the current first local graphical login session, if it has permissions for that.
that other guy
shell_exec('DISPLAY=:0 bash /data/manager/application/.shell/system.sh');
Or on function:
SCRIPTCURRENT=`readlink -m $0`
SCRIPTCURRENTPATH=$(dirname "$SCRIPTCURRENT")
runintoterminal () {
if ! [ -t 1 ]; then
DISPLAY=:0 gnome-terminal -e "bash $1"
exit 0
fi
}
runintoterminal $SCRIPTCURRENT

START a proccess in background in WINDOWS

I have a problem running a command from a PHP script. The command I am trying to run is
echo y | plink -ssh -N -D 9999 admin#1.2.3.4 -pw admin -v
The thing is that the command runs but the script freezes until the execution of plink command, which I don't want. I also tried (running in background) this :
START /MIN "cmd.exe" /C "plink -ssh -N -D 9999 admin#1.2.3.4 -pw admin"
and I see minimized plink running, and as soon as I close it, the script continues.
I also tried:
START /B /MIN "cmd.exe" /C "plink -ssh -N -D 9999 admin#1.2.3.4 -pw admin"
and it does the same thing, but is showing the output in the PHP script.
this is the function :
function create_tunnel($ip,$user,$pass,$port)
{
exec('START /min cmd /c "echo y | plink -ssh -N -D '.$port.' '.$user.'#'.$ip.' -pw '.$pass.' -v" > nul');
}
What must I do to run this command and let the PHP script continue execution? In linux this would be very simple, I would just use screen command.
Thanks.
Try Symfony Process component:
$process = new Process('ls -lsa');
$process->start();
while ($process->isRunning()) {
// waiting for process to finish
}
echo $process->getOutput();

PHP exec() nohup with redirect

I am trying to execute a command with exec() and redirecting stdout and stderr to a file.
exec("nohup python main.py -i 1 > /var/scripts/logs/1_out.log 2>&1 &");
It will create the file but it will not print anything to it.
If I run the command in a terminal everything outputs without a problem.
Got it working. Python does its own output buffering which kept it from writing to the file. Running it with the -u option disables this. Final code looks like this:
exec("nohup python -u main.py -i 1 > /var/scripts/logs/1_out.log 2>&1 </dev/null &");
Thanks.

execute a linux echo command (.sh) in Php

I created a .sh file in my linux and want to run it from webpage (php + Apache).
Some of the simple example execute without problem. But I can't run with echo Pipe:
#!/bin/sh
set +v
cp /tmp/test/test1.tar.gz.gpg /tmp/test/ts2.gpg
echo 'myPassword' | /usr/bin/gpg --passphrase-fd 0 --output /tmp/test/test1.tar.gz --decrypt /var/backups/test1.tar.gz.gpg
exit 0
It can copy ts2.gpg but can't run the second command (decrypt).
You want shell_exec.
<?php
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
?>
Enjoy

Categories