1

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?

Freddy Bonda
  • 1,189
  • 6
  • 15
  • 33
  • 1
    You should check the task list. Services run in a separate, non visible desktop and the started chrome instance should be there. – MC ND Jan 29 '19 at 06:51
  • Ok, I see you are correct. Google Chrome did start in the background. But why doesn't a cmd command like `dir > some.txt` work? I tried this at the correct location and it is not creating a file at all. – Freddy Bonda Jan 29 '19 at 07:10
  • 1
    `dir` and `>` (redirection operator) are internal elements in `cmd.exe`. I don't know how `node-cmd` implements command execution, but maybe you will need something like `cmd /c"dir > some.txt"` and, of course, you need to know what the active directory is when running the command (or include the full path of the file) and ensure the account used to start the service has write rights in the folder. – MC ND Jan 29 '19 at 07:25
  • Yeah you are correct. I actually got it working now. Would you like to add your first comment as a answer? It helped me understand what is going on hey. I will mark it as the answer for you – Freddy Bonda Jan 29 '19 at 07:53

1 Answers1

2

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.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Is there any way to right click on this `chrome.exe` task and do something like "bring to this user's desktop"? – Freddy Bonda Jan 29 '19 at 09:23
  • Also, any idea why the command `git` does not working in this `node-cmd`? – Freddy Bonda Jan 29 '19 at 09:25
  • @FreddyBonda, For the `chrome` process No. The usual way to deal with this kind of things is to have a separate process running in user session that will receive messages from the service process. For the `git`, are you using a full path to the executable? if not, is the path to the `git` command included in system path or user path? Remember your service runs under a separate account. Has the service account rights to access/read/change the folder structure being processed by `git`? – MC ND Jan 29 '19 at 09:29
  • Yes you are correct, I think this is where my problem is. BUT my `system variables` DOES have a path `%USERPROFILE%\AppData\Local\Programs\Git\cmd`. Why is this then saying git is not recognized? It seems like `%USERPROFILE%` is unkown? – Freddy Bonda Jan 29 '19 at 09:58
  • @FreddyBonda, no, but `%USERPROFILE%` is a variable that will be expanded to the profile folder of the user. Two different users will have different profiles with (almost sure) different paths. You need to ensure the profile of the account for the service has a copy of the `git` folders OR you can place the binaries in a folder shared by all accounts and adapt the path. – MC ND Jan 29 '19 at 10:32
  • Again perfect! My problem was that git was installed in my user folder, not the system. So I had to reinstall/move the path to a global place...thanks for everything man! Jeeze you helped me with stuff that would've taken me forever because I never knew about any of this. Learnt a lot! Thank you!!! :) – Freddy Bonda Jan 29 '19 at 11:33