1

So I already know how to run two batch commands in parallel like so

start /b cmd /c command1 
start /b cmd /c command2

Now I would like to create a Windows service that does the same thing, however I don't think I can just make a service that directly runs these two commands, as stopping the service would not kill the background processes that the service created.

My service I want to make needs to run command1 and command2 in parallel (fire command1 first, then command2 while command1 is still running) and when the service stops, both (all) of the associated processes should also stop.

I also want to avoid having to write some Python/C# program or something and do it directly from Windows/Batch/Powershell scripting if possible.

  • Those are not `batch commands` they're `cmd.exe` commands. Please tell us what the commands are and what they're supposed to do, _they're certainly not called `command1` and `command2`_. – Compo Aug 24 '18 at 16:51
  • @Compo hmmm I thought it wouldn't matter exactly what they were, they are both commands that "stay on" and need to be exited with ctrl-c. if you really want me to be specific, `command1` is iisexpress `command2` is iisexpress-proxy (with a bunch of parameters but i don't think that should matter) – golgi apparatus Aug 24 '18 at 16:56
  • 1
    You may have thought so, but you cannot just run any command as a service. If you really want a service you could consider `srvany.exe`, which although not officially supported can still work on more modern systems. You can find it in the [Windows Server 2003 Resource Kit Tools](http://www.microsoft.com/en-us/download/details.aspx?id=17657). I would suggest that you may want to investigate just running your command(s) using the Task Scheduler. – Compo Aug 24 '18 at 18:09

2 Answers2

0

you can definitely do what you need from a Window Service, instead of opening a console window with the start command in .NET you can use the Process and ProcessStartInfo classes, see here:

Executing Batch File in C#

in this way you can keep a reference to the Process object you started and on service stop you simply kill both processes.

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • Do I need to write a whole C# program to do it? I should have been more specific about not wanting to write an actual program (meaning, anything that isn't powershell or batch) to accomplish this. Thanks for the resource though. – golgi apparatus Aug 24 '18 at 16:46
  • 1
    A Windows service is perhaps not what you're looking for, then. Can you elaborate on what you mean by 'service'? What functionality do you require that lead you to choose Windows Service in the first place? – veefu Aug 24 '18 at 16:50
  • @veefu I want to be able to manage this "service" in a fashion similar to `sc start MyCommands` `sc stop MyCommands` and services also take care of automatically restarting/stopping/etc those processes right? – golgi apparatus Aug 24 '18 at 16:53
  • @veefu I'm willing to use something else that isn't a service if I can get this same functionality – golgi apparatus Aug 24 '18 at 16:58
0

Not 100 % sure why you want a service and what you need to do, but from your comments, the following approach might make sense.

Create Scheduled Tasks in Windows to Start, Stop, Run at system start, and run in SYSTEM (or any other) context. You an automate the creation of scheduled tasks with PowerShell.

Use PowerShell jobs for concurrent processing within PowerShell.

vrdse
  • 2,899
  • 10
  • 20