php How do I start an external program running - Having trouble with system and exec
What behavior are you expecting? Calling system('notepad')
works fine - it just doesn't display the GUI. It runs in the background, and PHP sits there patiently waiting for notepad to close itself (and only continues if you kill notepad from the process list).
If you're expecting it to pop up a GUI, I'm fairly certain that you can't do that. ;) An option might be to write out a batch script (file_put_contents('runme.bat', 'notepad hello.txt')
) and have that batch script queued (with Windows scheduler or whatever the cron-equivalent is on Windows) to run in an async fashion (and clear itself at the end).
How can I execute an external program without php script interrupt
There is a bunch of ways you can achieve this:
Running background process using &
:
exec('nohup ./loop.php > loop.out 2>&1 &');
Using pcntl_fork
and running your process from child:
$pid = pcntl_fork();
switch ($pid){
case -1:
die('Fork failed');
break;
case 0:
exec('nohup ./loop.php > loop.out');
exit();
default:
break;
}
echo "I'm not really a superman, just a parent process';
There is more ways to do this, just lurk into PHP documentation and questions in php here...
How can I start a Windows GUI program using PHP?
To launch a program on the computer which runs the webserver:
<?php
exec('"C:\Program Files (x86)\Notepad++\notepad++.exe" "C:\foo.php"');
The above will work on vista/win7 IF the webserver does not run as a windows service. For example, if you run apache and it automatically starts when your computer boots, you probably installed it as a service. You can check to see if apache shows up in the windows services tab/thingy.
If the webserver runs as a service, you'll need to look into enabling the "allow desktop interaction" option for the service. But otherwise:
An easy test using php's new built in webserver(php 5.4+). The key thing here is you manually start the server from a shell, so it runs as your user instead of as a service.
<?php
// C:\my\htdocs\script.php
exec('"C:\Program Files (x86)\Notepad++\notepad++.exe" "C:\foo.php"');
start a webserver via a command window
C:\path\to\php.exe -S localhost:8000 -t C:\my\htdocs
Then in your browser
http://localhost:8000/script.php
php exec() background process issues
Note:
If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.
http://php.net/manual/en/function.exec.php
so:
exec("php csv.php $file $user > /dev/null &"); // no $output
Open .exe from php webapp and login
So, in light of the comment/chat we had under your question, using phpseclib, you should be able to connect to the remote machine, which will run your the application:
require_once('Net/SSH2.php');
$ssh = new Net_SSH2('winhostaddress');
//if needed:
if (!$ssh->login('user', 'pass'))
{
exit('Failed to connect to win');
}
$ssh->exec('C:\\Path\\to\\app.exe argument1 argument2 argument3');
That should do the trick. When using flags, like on *NIX systems: ls -a
, you should know that the Windows counterpart is /
. Other than that, it's all pretty much the same. escapeshellarg
is not to be forgotten, when passing client-data to the executable.
Arguments containing spaces should be delimited using double quotes.
If you're not sure weather or not the application you're launching is accessible to the user you used to connect via ssh, try prepending this to your actual command:
runas /user:username C:\\Path\\To\\App.exe
Where username
is a user of which you're sure that has the appropriate privileges. Note that you might be prompted for a pwd.
For any other issues, here's some related questions:
- php How do I start an external program running - Having trouble with system and exec
- How can I start a Windows GUI program using PHP?
- PHP - Any chance to run GUI program through exec()?
Related Topics
PHP Sessions Timing Out Too Quickly
How to Get the Sqlsrv Extension to Work With PHP, Since Mssql Is Deprecated
MySQL Server Has Gone Away - in Exactly 60 Seconds
Best Way to Automatically Remove Comments from PHP Code
Alternative to MySQL_Real_Escape_String Without Connecting to Db
How to Make Dot Match Newline Characters Using Regular Expressions
How to Make This Preg_Match Case Insensitive
How to Clear Apc Cache Entries
Fastest Way to Convert String to Integer in PHP
Use One Bind_Param() With Variable Number of Input Vars
MySQL_Fetch_Array, MySQL_Fetch_Assoc, MySQL_Fetch_Object
Is There Any Particular Difference Between Intval and Casting to Int - '(Int) X'