Enable windows service to use command line - cmd

I have written a simple service which is now active on my windows services. My problem is that I cannot access the cmd through the windows services. I am using nodejs express for my service, node-cmd for the cmd access & node-windows to create the service. My app does this:
app.get('/check', (req, res) => {
cmd.run('start chrome');
res.status(200).send('The server is working correctly :)');
});
For those of you that don't understand this, it basically means:
Listen for the GET '.../check' call, then do:
cmd: start chrome
return response 'The server is working correctly :)'
When I run this manually, then it starts chrome. When I use it as a windows services, then it doesn't start chrome but it DOES resond with The server is working correctly :)
For some reason, cmd command are not working in windows services?

From Windows Vista service processes run in a different session/desktop that user processes run (there is Microsoft white paper).
So, if you start chrome.exe from a service, it will be run inside the service session and will not be visible in the user's desktop. You can check the task list to ensure it has been started.

Related

How to Run WAS Liberty Core server as a Background Service in Windows

We have installed WAS Liberty Core 8.5.5 to run Maximo anywhere mobile applications.
If we start the server from CMD then we are able to access worklight console and maximo anywhere apps.
Command we are using to start the server.
server start server1
But once we closed the CMD window the server stops automatically.
Any workaround to run the server as a background Service ?
Also we need info on how to add WAS Liberty server to Windows startup service so that it will start automatically on system restart.
Thanks,
Ajay
You can run Liberty as a Windows service:
https://www.ibm.com/support/knowledgecenter/en/SSEQTP_liberty/com.ibm.websphere.wlp.doc/ae/twlp_setup_new_server_winserv.html
bin\server registerWinService serverName
bin\server startWinService serverName
bin\server stopWinService serverName
Of course "net stop/net start" or services.msc can be used instead of startWinService/stopWinService
The feature to setup a proper windows service is not available in Websphere Liberty 8.5.5.x.
You can use sc.exe to create a service linked to the server executable, but it won't behave correctly i.e. it won't respond correctly to the commands from the services application. As an example, you will be able to start the service and the server will run, but then you will see an error saying the service did not respond in a timely fashion.
According to this question the proper functionality, as described in the other answer, only became available around version 17.0.0.1.
Hope that helps,
John

How to call an already running windows service?

Is there anyway to call a windows service that's already running or a process to get info? What my goal is to find out if my windows service is an infinite loop or dead lock and see if it responds. So I want to be able to pass an argument from another program to a windows service and want it to to return a string or number. Is this possible? I can change the windows service to accommodate this. I am thinking of an event or something.
Note: I am not supposed to have the service write to a file or database.
You can host a WCF service in your Windows Service which you could call to get status information.
Here are a couple of links on doing that:
Can I host a WCF Service in a windows service?
How to: Host a WCF Service in a Managed Windows Service

How an application can start its own service after it closes in Delphi?

I have an application that the user can start in two different ways, as a normal application or as a Windows service. When the service is already running (there's a icon of it in the system tray) and the user tries to start the application (not just open by clicking on the tray icon, but start again clicking in Start>Applications>Blah blah), it says: "The application is already running as a Service, do you want to close the service and start the App?" When the user clicks 'Yes' the application closes the service and starts itself as a normal application.
When that occurs I put a flag indicating that the user had once stopped the service and when they close the application the software sees the flag and the service has to start running again, because it was closed before to start as a normal app.
I tried to put:
WinExec(PChar('NET START MyApplicationName'),SW_shownormal);
in the OnClose event, before this line:
ExitProcess(0);
but it says that the service is already running and closes the application without starting the service, and if I put it after that line it doesn't do anything at all.
Is there any way to do this?
When I put the line before the ExitProcess(0); it opens a cmd window with the message:
"The service is not responding to the control function"
then it closes the application, closes the cmd window and it doesn't start the service.
In my view you are over-complicating the problem. Starting and stopping services is a complicated action and indeed it is forbidden by default for non-admin users. This very fact will make your application very annoying to use. You will limit its use to admin users, and also pepper them with UAC dialogs. Nobody likes apps like that.
The alternative is to simply leave your service running the whole time, but make it inactive whilst the standard desktop app is running. When the desktop app starts it strikes up communication with the service using some form of IPC and asks the service to stop work. The service does this and then the desktop app proceeds. When the desktop app closes, the service is asked to resume work. You would want to keep a communication channel open the whole time to guard against the desktop app terminating abnormally – if that happened the service would simply start up again.
Now, if you really wanted to do a good job you could let all the processing of work always happen in the service. The desktop app would merely be a front end for the service. That would seem to me to be the most efficient and logical design for this app.
Write a simple Batch file like that
#ECHO OFF
ping 1.1.1.1 -n 1 -w 10000 > NUL
NET START MyApplicationName
Then run execute this batch file before close your program.
ShellExecute(0, 'Open', YourBatchFile, nil, nil, SW_HIDE)

How do I get Topshelf to run as a domain user

How do I get Topshelf to run as a specific domain user account? I have in the configuration
x.RunAs("domain\username", "password");
I have in the code a console statement that prints out the Windows Identity that the process is running under. It is not the one I specified.
The code snip-it you indicated is the correct method to apply a user. A couple of things apply to this...
This only works for windows services, if you run it as a console app it will still run as you
All shelved processes run as the host user, so shelves will ignore any user settings
You can double check the user the service is registered to run as by going into services in mmc, find your service, goto the properties panel, and there's a Log On tab which will display the user the service is to run under.
If you are just running as a console app, just servicename install start and it should be running as the user it's been setup for.
If this doesn't help, reach out on the mailing list http://groups.google.com/group/topshelf-discuss.

Debugging Topshelf service that won't run under restrictive account

I have a Windows service written using Topshelf. I'm trying to configure it to run using a Windows account with restricted privileges rather than using LocalSystem. That's also necessary as I'd like to connect to a database using integrated authentication.
The service works when run as LocalSystem (albeit with a database connection string containing credentials) and running the console application as my limited account (using runas) also works.
However, when I try to start the service the service control manager times out waiting for a response:
The service did not respond to the start or control request in a timely fashion.
I also get the following in the Application Popup event log:
Application Error : The exception unknown software exception (0xc06d007e) occurred in the application at location 0x77e4bef7.
The first thing that the application does is writes to a log file but it doesn't reach that when I start the service. The logging works if I run via the console.
Any suggestions what I might be missing or what I might try next?
This problem seems to be related to the server (a domain controller) rather than TopShelf. A service built with the .NET service component also exhibits the same behaviour.
The service runs successfully on a different machine (in the same domain).
Unfortunately this doesn't help diagnose the problem but gives me an acceptable workaround.
Check the MSDN article Debugging windows services which describes how you debug windows services.
I've just started seeing this on a few of my services written in .net 2.0. They'll start fine when the server boots, but if I were to restart them throughout the day, they would not start, and give this error message.
They currently ran under a domain account which has admin rights on the box, but for kicks, I switched it to Local System, and the service started normally. I stopped it, changed it back to the domain account (reentering the password), and it started normally again as expected.
Don't know if this counts as a 'fix' so much, but that's what worked for me.

Resources