I have server and client, client is running in a background using wrapper. Server sends commands to client using SocketChannels and client runs commands this way:
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(command);
This works good when I run client from console as I usually do. But when I run it as a Windows Service some commands not working. For example:
runtime.exec("shutdown -s -t 300");// works perfectly
runtime.exec("shutdown -a"); // works perfectly
runtime.exec("shutdown -l"); // works only when client started from console
// note that these commands will shutdown your computer or log off
Third command is simply not doing anything, then program works normally. I run commands separate, in random order, there is no difference. I'm using Windows.
So what am I doing wrong?
Also I cannot run from service "notepad.exe" (or another .exe with full path). I'd like to know what is the right way to do this(again, from console it works).
1 Answer 1
Windows services cannot interact with the desktop by default. In case your command requires any kind of response from the user via some GUI, it will hang. One way to solve it is to allow your service to interact with the desktop (on the service properties in SCM). The other way is to simply avoid such interaction. I haven't tried it, but I think the -y switch should solve the problem with the restart. The second option is what I would go for.
2 Comments
-y parameter makes no difference, works only from console. Thanks for help, I'll use -y anyway.-y switch is not enough. Also, take a look at Sysinternal's PsShutdown. The -f flag looks promising.