0

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

asked Jul 3, 2012 at 12:05

1 Answer 1

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.

answered Jul 3, 2012 at 12:25
Sign up to request clarification or add additional context in comments.

2 Comments

Same thing, -y parameter makes no difference, works only from console. Thanks for help, I'll use -y anyway.
@alaster, try having the service run as the currently logged in user, and check the "interact with desktop" option. You should then see any UI this command brings up, maybe the -y switch is not enough. Also, take a look at Sysinternal's PsShutdown. The -f flag looks promising.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.