I am having a very hard time getting an executable to run from within a PHP script (using exec(), or a number of other PHP commands I've tried).

The issue seems to be coming down to a question of user permissions (possibly?) or Windows/server configuration, which I know very little about.

I'm currently testing on Windows localhost, using WAMPserver. I currently have a .bat file with the command to run my executable, just as I would run it from the command line. When running from the command line, the .exe works fine; similarly, when I click the .bat file directly, it starts up and runs the .exe as it should. However, when I try to run it from the PHP script (currently by using exec('open.bat');, although I've tried numerous other ways of launching it), it activates something called "Windows Interactive Services Detection".

From what I've read, this has something to do with Windows' "sessions", whereby user programs are run in session 1, and system processes are run in session 0, for the sake of security. But for some reason, I'm allowed to run the .bat fine if I simply click on it.

So perhaps it has to do with the user in which PHP is executing the batch file?

I've tried a few methods of running the file as a different user, such as using CPAU, which allows a program to execute under a different user. Again, when clicking on the batch file directly, the program worked fine, but when activating it from PHP, it didn't work (when using CPAU, rather than Interactive Services Detection popping up, it simply didn't do anything).

Has anybody encountered a similar issue while attempting to run an executable from within PHP, or have an idea of how I might get it to run?

closed as off topic by MDMarra, Ward, John Gardeniers, Scott Pack, Sven Dec 29 '12 at 16:56

Questions on Server Fault are expected to relate to server, networking, or related infrastructure administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here. If this question can be reworded to fit the rules in the help center, please edit the question.

  • I'll add some more specifics down here if it helps. This isn't a question pertaining directly to the executable that I'm attempting to run; I've tried it using other batch files as well. For instance, I tried a batch file containing notepad.exe example.txt. Again, when I click directly on the batch file, it opens up 'example.txt' as it should. Yet when I use my PHP script to execute the batch file, the 'Interactive Services Detection' window pops up. So there's some difference in the way the .bat file is run when clicked vs. with PHP that is not allowing things to run properly. – Nik Dec 29 '12 at 1:32
  • I was thinking possibly it may have to do with the operations dictated in the batch file attempting to access files or programs outside of the localhost directory? e.g. I'm perhaps I'm not allowed to run notepad.exe, which lives outside of my WAMP directory, from PHP? (Whereas clicking on it directly as the logged-in user would not invoke the restriction of being within the WAMP directory). That seems like a possible reason for what's occurring.. – Nik Dec 29 '12 at 1:35
  • What version of Windows are you working with? There was a pretty big shift in how this stuff works between the XP/2003 era and the Vista and beyond era. I'm trying to think of a solution for you. – Ryan Ries Dec 29 '12 at 1:55
  • Using Windows 7 Home Premium – Nik Dec 29 '12 at 2:21

All user-mode processes in Windows execute as "a user," even if that "user" is "Local System." Therefore, all Windows Services run as "a user." IIS or Apache, whatever you're using, runs as a user.

That user is not you.

Even if you ran your web server (the Windows service) under your user account, a new logon session that isn't "your" logon session would still be created because the system is logging on another instance of your user account. User accounts and logon sessions are different things. Every interactive logon session has a separate desktop, so anything the service did that involved showing things on its desktop would take place in its desktop, and not yours.

The batch file and executable work as intended when you click on them because they launch in your session. When Windows detects that a program running as a service is trying to interact with a desktop, Interactive Service Detection is triggered (which itself runs as a Windows service.) The reasoning being that Windows services should be completely non-interactive and have no GUI that requires input from a human. Otherwise you're probably dealing with an executable that was not well designed for being a Windows service. Even still, you should be able to click "View the Message" or whatever the prompt is from Interactive Services Detection and it will take you to Session 0's desktop temporarily to interact with the "service's" GUI, but it will also interrupt your desktop while you're at Session 0's desktop. (It creates a new process in your session to inform you that something in Session 0 wants your attention.)

Alright so what can you actually do about it? Well, you didn't really give any details about what this executable that you're running as a Windows service is or what it does, but it seems that you want it to run in your session, on your desktop, so that you can interact with it like you would with Solitaire.exe.

Well launching things in other users' sessions is not a routine action in Windows, but psexec should be able to do it.

psexec.exe \\computername -i 1 program.exe

In the above example, the number 1 specifies which session to run program.exe in. If you want to manually trigger Interactive Services Detection, use session 0.

For further reading: http://blogs.msdn.com/b/patricka/archive/2010/04/27/what-is-interactive-services-detection-and-why-is-it-blinking-at-me.aspx

  • Hi Ryan, thanks for the detailed reply. The executable that I'm trying to make work, in theory, shouldn't even need to be interactive. It opens up an input text file (given as a parameter in command-line usage), processes the text, then saves the resultant output in a separate .txt file, also defined as a command line argument. However, I didn't write the .exe myself, so I'm not sure why it's causing interactivity - although it may the because it is accessing database files elsewhere on the system. – Nik Dec 29 '12 at 5:26
  • Regardless, PsExec seems like a useful tool to make it work. I'm having a hard time getting it to work when attempting to force it to use session 1, however. – Nik Dec 29 '12 at 5:29
  • For the sake of simplifying the problem, I wrote a small .exe that opens a file called 'test.txt' and prints a line to it. The program works when clicked directly, and works with PsExec if I don't try to change the session. However, when I use it like: psexec \\computername -i 1 test.exe, no output occurs... Would forcing it into session 1 make it unable to access the text file I want it to write to? – Nik Dec 29 '12 at 5:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.