PHP - ShellExec screen execution not working - php

Im making a webscript starting a minecraft server and i have a problem with shell_exec executing command on screen:
$output = shell_exec('screen -d -m -S ServerOne');
$output2 = shell_exec('screen -S ServerOne -p 0 -X stuff "cd /var/www/html/servers/asd/ && java -Xmx1024M -Xms1024M -jar server.jar nogui > /screenlog ^M"');
It creates a screen with name ServerOne but didnt happened nothing after it!
I want to execute on it:
cd /var/www/html/servers/asd/ && java -Xmx1024M -Xms1024M -jar server.jar nogui > /screenlog ^M
I tried a lot of things and nothing worked, please help! :)
Sorry for my bad English.

Related

Incorrect Webpage Output for PHP exec running bash script for audtool (part of Audacious)

On a local linux server (Rapsberry Pi debian stretch with desktop), I am working on sending "audtool" commands to a running Audacious media player using php, exec and bash scripts. Audacious is autostarted when the server starts up with user "pi". I have apache2 and php set up and working on the server, and I can ssh to the server and run all the commands from the cli. I believe I have resolved the issues with running audtool (dbus and setting the right environment variables) and running the php on the command line works successfully. However when running the php on a webpage I get back a long string of information about apache2
I have spent several hours (getting on for a whole day) researching this on the web in order to get to this stage, so close I can almost touch it, but stuck on this last element. The example is to display the current song from a running instance of Audacious. Audtool requires a running dbus (looks for a display). Using exec or shell_exec I have no problems running bash commands such as whoami or ls.
The php page (cursong.php):
<?php
echo exec('/var/www/html/cursong.sh');
?>
The bash script (cursong.sh):
#!/bin/bash
##call current song
pid=`pidof audacious`
user=`ps -p $pid -o user=`
export `strings /proc/$pid/environ | grep DBUS_SESSION_BUS_ADDRESS`
sudo -E -su $user /usr/bin/audtool --current-song
(from here: https://redmine.audacious-media-player.org/boards/1/topics/1058?r=1059)
Output from command line:
php -f cursong.php
Artist - Song Title (for example - so this works)
Output on webpage:
declare -x APACHE_LOCK_DIR="/var/lock/apache2" declare -x
APACHE_LOG_DIR="/var/log/apache2" declare -x
APACHE_PID_FILE="/var/run/apache2/apache2.pid" declare -x
APACHE_RUN_DIR="/var/run/apache2" declare -x APACHE_RUN_GROUP="www-
data" declare -x APACHE_RUN_USER="www-data" declare -x
INVOCATION_ID="4ce76136ca8842bd9108d6b1b9a5b9ed" declare -x
JOURNAL_STREAM="8:23896" declare -x LANG="C" declare -x OLDPWD
declare -x
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
declare -x PWD="/var/www/html" declare -x SHLVL="1"
I have set www-data, the apache2 user with the following in
/etc/sudoers:
www-data ALL=NOPASSWD: ALL
and /var/www/html is rwx for anyone
Obviously, I am expecting to see "Artist - Song Title" on the webpage, but instead I get back all the apache2 info. What am i missing, or where have I gone wrong?
I hate answering my own question, makes it look like I wasn't trying hard enough! After a further five hours or so of searching around and attempting fixes, I happened upon this post on SO:
Running command-line application from PHP as specific user
which suggested putting a "sudo -u user" in the exec of the php file. I tried this with the "pi" user and it still didn't work, then I simply tried it with "sudo" and hey presto!!
The php file now looks like this:
<?php
echo shell_exec('sudo /var/www/html/cursong.sh 2>&1');
?>
Now to do some testing on how it works with the other audtool commands that don't ask for a response but require action from audacious, and to see how I can reduce scripting php files by passing a parameter to the bash script!
Just for completeness, the php and bash scripts for both a request and an action, using a parameter fed to the php url and then on to the bash script:
PHP File with Parameter
<?php
$request = $_GET["request"];
echo shell_exec("sudo /var/www/html/cursong.sh \"${request}\" 2>&1");
?>
url example:
http://192.168.1.92/cursong.php?request="--playlist-shuffle-status"
Bash Script with parameter
#!/bin/bash
##call request
pid=`pidof audacious`
user=`ps -p $pid -o user=`
export `strings /proc/$pid/environ | grep DBUS_SESSION_BUS_ADDRESS`
sudo -E -su $user /usr/bin/audtool $1
PHP file for an action
<?php
$action = $_GET["action"];
shell_exec('sudo /var/www/html/playsong.sh \"${request}\" ');
?>
url example:
http://192.168.1.92/cursong.php?action="--playback-play"
Bash script for an action
#!/bin/bash
##call action
pid=`pidof audacious`
user=`ps -p $pid -o user=`
export `strings /proc/$pid/environ | grep DBUS_SESSION_BUS_ADDRESS`
sudo -E -su $user /usr/bin/audtool $1

Running php exec() for background process

I have set up a line of code in my class to run a function in the background using the following line on my linux server:
$cmd = "nohup wget -q <-url here-> /dev/null 2>&1 &")";
$exec = exec( 'bash -c "'.$cmd.'"' );
(with the url just pointing to my script)
This is working fine and doing what I want it do, however everytime this runs it is creating a new file in my root (public_html) and they are all numbered like 10, 10.1, 10.2, 10.3 etc.
Does anybody know how I can stop these being created?
Also if this is bad practice and anybody wants to suggest a better way of doing something like this, any advice is greatly appreciated.
$cmd = "nohup wget -q <-url here-> -O /dev/null 2>&1 &")";
$exec = exec( 'bash -c "'.$cmd.'"' );
note the -O flag, with which you tell wget where to save the downloaded file. with this, you direct it to /dev/null and therefore delete it.

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();

Using wget with shell_exec and at command?

I've been struggling with shell_exec PHP function and at linux command for 2 days.
To make it short, this works:
shell_exec('/usr/bin/at 09:32 <<EOF
touch /var/www/website/hello.txt
EOF'
);
this doesn't:
shell_exec('/usr/bin/at 09:32 <<EOF
wget -O - -q -t 1 "http://192.168.56.101/website/test.php?param=hello" >/dev/null 2>&1
EOF'
);
Why ?
(note: the code above does work in console)
Thanks in advance.
Ok I've got it at last !!
For those who are interested the pb comes that the wget command also need to be invoked with the full path (ie: /usr/bin/wget).
What misleaded me is that the touch command doesn't need it. It's weird but anyway here's the working code:
shell_exec('/usr/bin/at 09:32 <<EOF
/usr/bin/wget -O - -q -t 1 "http://192.168.56.101/website/test.php?param=hello" >/dev/null 2>&1
EOF'
);

How to make php run a prolog program,The command works when i use on command line but not with php

I am trying to run a program on swi prolog through php. I am running on windows
My command are as follow
$cmd = "C:\Program Files (x86)\pl\bin\swipl -f test1.pl -g test " halt;
exec( $cmd );
nothing happens when i try to run my php code it waits for while and returns to command prompt on windows/system32/exe
but when i execute the same command on cmd line i get the desired out put
C:\Program Files (x86)\pl\bin\swipl -f test1.pl -g test " halt;
i had been trying it for last two days, any help will be highly appreciated
Thanks in advance for all people who will give it a thought and help me
Try using double-backslashes (and including "halt" within the double-quotes):
$cmd = "C:\\Program Files (x86)\\pl\\bin\\swipl -f test1.pl -g test halt";
Program Files (x86) => progra~2
$cmd = 'C:\progra~2\swipl\bin\swipl.exe -s C:\prolog\web\base.pl -g test,halt';
it's work for me

Categories