如何使用一个命令一次启动多个 Windows 服务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/556801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:04:10  来源:igfitidea点击:

How do I start multiple Windows Services at once, with one command?

windowswindows-services

提问by Macha

My primary computer for programming is the same computer I use for gaming etc. So to increase speed while gaming I turned off services like Apache, MySQL, Subversion etc. from starting at boot as I use it about 50/50 for gaming/programming.

我用于编程的主要计算机是我用于游戏等的同一台计算机。因此,为了提高游戏速度,我从启动时关闭了 Apache、MySQL、Subversion 等服务,因为我大约 50/50 使用它来进行游戏/编程。

This is fine most of the time but it's a bit of a nuisance to start them all separately.

大多数情况下这很好,但单独启动它们有点麻烦。

Could someone show me how to write a batch file or something similar to start all the services?

有人可以告诉我如何编写批处理文件或类似的东西来启动所有服务吗?

回答by kemiller2002

Well you can write a batch file like

好吧,您可以编写一个批处理文件,例如

net start "Service Name"

There should be a way to dependency link the services together also, so when one starts all the others will too. I'll see if I can find the switch to do that.

也应该有一种方法可以将服务依赖链接在一起,因此当一个服务启动时,所有其他服务也将启动。我会看看我是否能找到开关来做到这一点。

EDIT:

编辑:

I haven't tried this, but this should tell you where to find in the registry to make the services dependent on each other and all start automatically:

我没有试过这个,但这应该告诉你在注册表中的哪里可以找到使服务相互依赖并自动启动:

http://www.softwaretipsandtricks.com/windowsxp/articles/490/1/Removing-Service-Dependencies

http://www.softwaretipsandtricks.com/windowsxp/articles/490/1/Removing-Service-Dependencies

回答by ReinstateMonica Larry Osterman

You can also have the services start in parallel by calling:

您还可以通过调用并行启动服务:

SC START servicename

SC START 服务名

I don't know if that helps...

不知道有没有帮助...

I wouldn't mess with the dependencies for services unless you reallyknow what you're doing.

除非您真的知道自己在做什么,否则我不会弄乱服务的依赖项。

回答by Orion Edwards

If you're using powershell, you can do this:

如果您使用的是 powershell,则可以执行以下操作:

"service1", "service2" | %{ start-service $_ }

To explain the above, it's as follows:

为了解释上述内容,如下所示:

  1. create an array containing "service1" and "service2"
  2. pipe that array to the foreachcommand (which is the %sign)
  3. the foreachcommand will run the code block (delimited by { }), and the "current item" is represented by $_
  4. It will therefore run start-serviceon each of the things in your array
  1. 创建一个包含“service1”和“service2”的数组
  2. 通过管道将该数组传递给foreach命令(这是%符号)
  3. foreach命令将运行代码块(以 分隔{ }),“当前项”由$_
  4. 因此,它将start-service在您的数组中的每个事物上运行

回答by Craig

The command for starting a service is "net start <servicename>". Just add the ones you need to a file called Something.bat and run it. simple. :)

启动服务的命令是"net start <servicename>“。只需将您需要的那些添加到名为Something.bat的文件中并运行它。简单。:)

net stop <service name>will also stop them.

net stop <service name>也会阻止他们。

回答by Nige D

I am a tad late on this, though I was researching something similar.

我在这方面有点晚了,尽管我正在研究类似的东西。

create a batch file and in that batch file use the sc command:

创建一个批处理文件,并在该批处理文件中使用 sc 命令:

sc start "first servicename" sc start "second servicename"

sc 开始“第一个服务名” sc 开始“第二个服务名”

and so on... save the batch file and either double click it to run it, or schedule it to run accordingly.

等等...保存批处理文件,然后双击它运行它,或安排它相应地运行。

回答by Wouter Vanherck

Like other answers mentioned, writing a batch/cmd file works wonders.

像提到的其他答案一样,编写批处理/cmd 文件会产生奇迹。

@echo off

echo Press any key to start the Services
echo ===================================

pause

echo Starting Apache
net start Apache

echo Starting MySQL
net start MySQL

echo Starting Subversion
net start Subversion

echo == Services have started ==

timeout 3
exit /b

回答by JTE

simply use powershell if you have a common name

如果您有一个通用名称,只需使用 powershell

Start Services

启动服务

Get-Service *pattern* | start-service

Stop Services

停止服务

Get-Service *pattern* | stop-service

回答by Bogdan Gavril MSFT

An easier way:

更简单的方法:

  1. Install ps tools (it has a lot of really cool cmd line tools for developers)

  2. One of the tools that ps tools comes with is PsService

  3. you can start stop your services using start and stop

  1. 安装 ps 工具(它为开发人员提供了很多非常酷的 cmd 行工具)

  2. ps 工具自带的工具之一是 PsService

  3. 您可以使用 start 和 stop 开始停止您的服务

回答by bluelurker

Although I'm late to the party , however here is exactly what you need. ProgramUtilityv1I was looking for such application too, so i wrote it myself. It works only on Windows and that too on Windows Vista and above.I'll soon release a linux version too.

虽然我参加聚会迟到了,但这里正是您所需要的。 ProgramUtilityv1我也在寻找这样的应用程序,所以我自己写了它。它仅适用于 Windows,也适用于 Windows Vista 及更高版本。我也将很快发布 linux 版本。