php How do I start an external program running - Having trouble with system and exec

php exec not working
php exec background
running shell command in php with real time output
php run exe with parameters
exec in php - w3schools
php system() example
php exec() example
php run windows command

I'm running in a controlled, xp, intranet only environment and I need to start external processes from a PHP applications. (Backups, Reports etc.)

I can get system or exec to start processes that work silently. Here's a simple example

<?php exec ("echo hello > hello.txt");?> 

I can get it to execute a bat file that has no visible output.

I can't get any program that has a screen to run such as a report generator or notepad...

<?php exec ("explorer");?>

doesn't do anything. or same for system

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).

exec - Manual, If a program is started with this function, in order for it to continue running in the background, If you want to start an external program without starting cmd.exe use If you're having problems with any of the exec(), system() etc. functions not​  thank Charles ,refer to php How do I start an external program running - Having trouble with system and exec. Start->Run, type "services.msc" to bring up Services control (other ways to get there, this is easiest IMO) Locate your Apache service (mine was called "wampapache" using WampServer 2.0)

Program execution Functions - Manual, The only syntax I found to work for the command portion of an an exec() call on a will be testing on more OSes right away, in PHP4 and 5, with exec, system, to cmd.exe (potential security hole!), so PHP can fork and execute external programs. Well, I had this issue when I wanted to start an indefinitely running PERL  Create custom client-server kind of application. The server part will always be running inside in User mode where the GUI needs to be displayed. And the client will be called from PHP. When client is invoked, it will signal the Server part using IPC like event. Server can start the GUI application in turn.

Another super late answer, but this comes up on Google when searching for "php run gui program"...

I have been able to launch a GUI app in Windows 8.1 by making, running and deleting a scheduled task:

shell_exec('SCHTASKS /F /Create /TN _notepad /TR "notepad.exe" /SC DAILY /RU INTERACTIVE');
shell_exec('SCHTASKS /RUN /TN "_notepad"');
shell_exec('SCHTASKS /DELETE /TN "_notepad" /F');

shell_exec - Manual, It is not possible to detect execution failures using this function. exec() should be If you're trying to run a command such as "gunzip -t" in shell_exec and getting Shell scripts that start with #!/usr/bin/bash return their output properly. If you need to run a system command with elevated privileges think through the problem  In PHP there are two important methods to execute programs, and these are exec() and passthru(). Both of these two take a minimum of one parameter, which is the name of the program you want to run, but the difference between them is that exec() runs the program then sends back the last line outputted from that program as its return value.

system - Manual, system — Execute an external program and display the output If a program is started with this function, in order for it to continue running in the This works in exec() as well as system() in PHP since it uses stream redirection to redirect the 3 times, waiting for the prior command to complete before starting a new one If you plan to start programs on the server that show message boxes (things that require OK from the server-side), or remain (like notepad.exe), and the exec-command seems to go into an deadly loop, then MAYBE your program has started, but you can't see it.

PHP: exec - Manual, exec — Execute an external program If you need to execute a command and have all the data from the command passed directly outputs the username that owns the running php/httpd process // (on a system with the "whoami" executable in the path) You may use it to start a process or join an exisiting PID process. exec — Execute an external program passthru — Execute an external program and display raw output proc_close — Close a process opened by proc_open and return the exit code of that process proc_get_status — Get information about a process opened by proc_open proc_nice — Change the priority

The Process Component (Symfony Docs), The Process class executes a command in a sub-process, taking care of the differences between operating system and escaping arguments to prevent security issues. The Process component internally uses a PHP iterator to get the output while it is You can also start the subprocess and then let it run asynchronously,  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.

Comments
  • I see what you mean. You are right notepad is running without a GUI. (My configuration timed out after 60 seconds.) So I can't start a GUI based program out of PHP and have to resort to OS based solutions. It's good to know. thanks !
  • I'm curious as to how this got 10 upvotes--it still only runs notepad as a background process for my setup even though I have it on "interact with desktop". I'm using Windows 7.
  • @user24601 This has only been tested for Windows XP. I will make a note of that in the answer. As for the upvotes... who cares?
  • Working fine on Windows 7
  • Clearly this solution only works in a limited number of environments. I have Windows 7 and it does not work, nor have I found anyone else except for @CertaiN who says that it does work.
  • I have windows 7 and tried other answers while failed to start gui but this one is working perfect.
  • also been battling on Win10 with none of the solutions working - this works 100% +1 for outthebox..
  • How to open local folders i.e. file system?
  • Thanks a ton. It worked for my AWS Windows server 2012 instance. I was looking for this solution past 2 days.
  • Works beautifully in Windows 7. Switched argument to cmd.exe /c {Path-to-blah} and viola. Works perfect.