I have an IIS server with Php and have been trying to execute scripts using exec() and system() with no luck. When i go to the machine and run it by double clicking it, or by opening command and running the batch file, 4 windows pop open with ffmpeg streams going and dump to folder.
With exec("cmd.exe 'c:\inetpub\wwwroot\estf\batch\srRecord.bat'");
I am getting some of the batch file process working (creates new folder and dumps to file, but i can't see the cmd gui anywhere. And my stop.bat won't taskkill the ffmpeg process.
Is there a way to bring up the cmd GUI on the external device, also a way to run the exec() command and pass on so that other devices or batch files can kill these tasks (i am thinking that because the php is still running it, nothing else has rights to stop it.
Regards,
Mitchell
Related
I've got this problem with very poor performance with PHP executing bash script with remote ssh and doing a grep on a log. In web browser I receive output after 40 seconds. Executing bash script(ssh + grep on remote machine) directly over my local machine is taking 8 seconds. I know I cannot bypass this so I came up with idea of :
Creating PHP script which will save to a text file on a local machine all "variables" I need. Let's call it "parameters.txt". Then other bash script "reader.sh" will read this "parameters.txt" file, do all of the magic of remote ssh and grep, then save output to "output.txt". Background script will run every 2 seconds the reader.sh
Is it a good idea?
If you really need to use PHP over SSH, you could use something like http://phpseclib.sourceforge.net/. They have a nice ssh implementation which I used for sometime for deploy applications and configure Linux servers (puppet replaced it). Your code will be cleaner and easy to maintain.
Another option is, if you are reading logs from Linux ( messages, apache, syslog, etc...) you have options with Rsyslog (http://www.rsyslog.com/) to centralize it on a server and work on it locally. This would be cheaper for your computers and easier to deploy.
Here is how you do it fast. I recently published a project that allows PHP to obtain and interact with a real Bash shell, even through SSH. Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
$shell = \MTS\Factories::getDevices()->getRemoteHost('ip_address')->getShellBySsh('username', 'secretPassword');
//execute the grep command
$return1 = $shell->exeCmd("cat /full/path/to/logfile | grep \"someExpression\"");
echo $return1; //contains all the return that matched the grep expression
Now I am trying to execute php websocket script as a background. For it, I used below command on the AWS terminal.
php chat-server.php >> log.txt &
But strangely it is terminated without any errors.
My question is two.
Why the php command is terminated on AWS? Is there any limitation to run a php script continually as a background service
How can I run it permanently on AWS? I understand I can use a linux script to run my web-socket server permantely (maybe linux script will be re-lunch my php script when it will be terminated). But I don't know well linux commands. Who can help me?
Thanks for your advice!
First, enable PHP logs to see if there are any errors.
Then, to run it permanently, you can use screen or Tmux. It's simple to use and lets you detach the process and run it in the background. So you are able to logout from your ssh session, and the process is still running.
Please read:
https://wiki.archlinux.org/index.php/tmux
http://linux.die.net/man/1/screen
Can we run PHP script just like jar's files.I want to execute PHP script in background without open it in browser.Is this thing possible ?
Using Crons we can run. Make a batch file with php commands and run it through task scheduler if you are using windows OS or run it as cron for apache.
I'm trying to run notepad on the server (localhost for now).
exec() and system() functions are working fine when for example write ping 127.0.0.1.
But this does not work (working fine if I write the command directly in the command prompt):
$command = "C:\WINDOWS\system32\notepad.exe";
$result = system($command);
print_r($result);
Using Windows XP with xampp. Probably I don't have permissions because the command is executed from some other account but I don't know how to check this.
Any advices?
Edit:
As bwoebi said, I have opened processes but they are opened from a different user (SYSTEM) and I can't see when the application is opened. So, I have to paraphrase my question: how to change the user which is used when executing commands from a PHP script?
First you need to escape the backslashes in your command string if you're not using single quotes :
$command = "C:\\WINDOWS\\system32\\notepad.exe";
Also note that if Apache is running as a Windows service, it does not have desktop interaction permission, so it can't open a GUI, try running the script directly with PHP on the command line.
EDIT
The user used to run command is the user that is running PHP. To change the user running PHP, you'll have to change the user running Apache, if you want this user to have desktop interaction permission, you'll have to run Apache yourself and not as a service.
let the process a bit sleep after executing the shell command and search in the TaskManager for a Notepad... Then you'll see that this are two different users (and you don't see the other users Notepad)
Note pad is a GUI program so requires the windows TTY to be active.
Ping is command line so can be ran by the system directly and piped results into the program calling it.
With out getting into too much detail of how os's work basically it can't be done on a windows machine (its possible on unix machines but more difficult.)
At preset we have need to launch apps on a linux box remotely
To do this we have php script that is run at boot via the rc.local file. This php script watches a command file. This has commands written to it.
The php script has trouble running some apps. For instance it can boot X11, but it can't run an app that is meant for X11.
But, if we run the php script from a terminal, them the system works just just fine
Here is the contents of the rc.local file (this fails).
sudo -u jacob /usr/bin/php /home/listener/ListenerThread.php > /var/www/html/out.txt &
The user jacob as sudo root access with no need for passwords
Please help
Most likely if it's an X11 issue the children aren't having DISPLAY set in their environment, but without error messages we can't help you.
One solution I would suggest is to start X11 at boot and put the line that launches your script into your .xinitrc. This way your script will be able to run GUI programs correctly.
If you don't like that solution, then try running your gui apps from within the script like this: env "DISPLAY=:0.0" your_gui_app