windows sc命令
Operating systems run services to server operating system or user level features. These windows services generally starts in the startup and stopped at the shutdown. But some time we need to start service after the start or a configuration change.
操作系统运行针对服务器操作系统或用户级别功能的服务。 这些Windows服务通常在启动时启动,在关闭时停止。 但是有些时候,我们需要在启动或配置更改后启动服务。
Windows provides different tools to manage services. We have all ready looked the native Powershell service management tools following tutorial.
Windows提供了不同的工具来管理服务。 我们已经按照教程准备好了本机Powershell服务管理工具。
Service Management With Windows Powershell From Command Line
使用Windows Powershell从命令行进行服务管理
帮帮我(Help)
Detailed help about sc
command can be get with /?
option like below.
可以通过/?
获得有关sc
命令的详细帮助/?
如下所示的选项。
$ sc /?
句法(Syntax)
sc command syntax is similar to the most of the windows command. First we specify server names if we want to run service command remote systems and then provide real command to operate on service. After those we specify service name and other options.
sc命令的语法与大多数Windows命令相似。 如果要在远程系统上运行服务命令,则首先指定服务器名称,然后提供实际命令以对服务进行操作。 在这些之后,我们指定服务名称和其他选项。
sc <server> [command] [service name] <option1> <option2>...
命名服务 (Naming Service)
There are two identifier to name services. SERVICE_NAME
is real name where we use for related service operations and will be used for identifier. DISPLAY_NAME
is like a tag and used to provide more readable and understandable service names. In the following example we should use Appinfo
for service operations.
有两个用于命名服务的标识符。 SERVICE_NAME
是实名,我们用于相关服务操作,并将用作标识符。 DISPLAY_NAME
就像一个标签,用于提供更具可读性和可理解性的服务名称。 在下面的示例中,我们应该使用Appinfo
进行服务操作。
SERVICE_NAME: Appinfo
DISPLAY_NAME: Application Information
启动服务 (Start Service)
Services should be started in order serve. There are a lot of services in a windows operating systems. We will use start
command in order to start windows service. Some services can be depended other services. In this situations we should start first dependency services.
服务应按顺序启动。 Windows操作系统中有很多服务。 我们将使用start
命令来启动Windows服务。 某些服务可以依赖于其他服务。 在这种情况下,我们应该启动第一依赖服务。
In this example we will start the service named ProtectedStorage
.
在此示例中,我们将启动名为ProtectedStorage
的服务。
$ sc start ProtectedStorage
If the service starts without a problem it will print the status of the service. The STATE
line shows the service is pending for start.
如果服务启动没有问题,它将打印服务状态。 STATE
行显示该服务正在等待启动。
停止服务 (Stop Service)
Stopping a windows service is very similar to starting it. We will just change the start command with stop
command. In this example we will stop the service named ProtectedStorage
where we have started it in previous step.
停止Windows服务与启动它非常相似。 我们将只使用stop
命令来更改启动命令。 在此示例中,我们将停止在上一步中启动的名为ProtectedStorage
的服务。
$ sc stop ProtectedStorage
显示详细的服务状态(Display Detailed Service Status)
Detailed information about a service can be displayed with query
command by providing the service name. In this example we display information about ProtectedStorage
service.
通过提供服务名称,可以使用query
命令显示有关服务的详细信息。 在此示例中,我们显示有关ProtectedStorage
服务的信息。
$ sc query ProtectedStorage
This command output will provide following information about the service.
此命令输出将提供有关该服务的以下信息。
SERVICE_NAME
SERVICE_NAME
TYPE
line shows service typeTYPE
行显示服务类型STATE
line shows current status of service likeSTOPPED
,RUNNING
, etc.STATE
行显示服务的当前状态,如STOPPED
,RUNNING
等。WIN32_EXIT_CODE
line shows last stop event exit codeWIN32_EXIT_CODE
行显示了最后一次停止事件的退出代码SERVICE_EXIT_CODE
SERVICE_EXIT_CODE
CHECKPOINT
CHECKPOINT
WAIT_HINT
WAIT_HINT
列出所有服务 (List All Services)
There is a lot of service in a default Windows installation. After the installation 3 party applications may add new services too. All of these services can be listed with query
command and state=all
options like below.
默认的Windows安装中有很多服务。 安装后,第三方应用程序也可能会添加新服务。 所有这些服务都可以使用query
命令和state=all
选项列出,如下所示。
$ sc query state= all
仅列出正在运行的服务(List Only Running Services)
Previous example lists all services those running or stopped. We may interested with only running services. If we provide no option to the query
command it will only print running services by default.
上一个示例列出了所有正在运行或停止的服务。 我们可能只对正在运行的服务感兴趣。 如果我们不提供query
命令的选项,则默认情况下将仅打印正在运行的服务。
$ sc query
重新启动服务 (Restart Service)
Services have configurations and these configurations can be changed during they are running. Or some services may start work incorrectly. In this situations restart services is the best way to make it work and apply new service configuration.
服务具有配置,并且可以在运行期间更改这些配置。 否则某些服务可能无法正常启动。 在这种情况下,重新启动服务是使其工作并应用新服务配置的最佳方法。
$ sc stop ProtectedStorage
$ sc start ProtectedStorage
暂停服务 (Pause Service)
A service can be paused without loosing its session related information and data. And then this service can be resumed too. In order to pause a service the service should support pausing. In this example we will pause ProtectedStorage
service.
可以在不丢失与会话相关的信息和数据的情况下暂停服务。 然后也可以恢复此服务。 为了暂停服务,服务应支持暂停。 在此示例中,我们将暂停ProtectedStorage
服务。
$ sc pause ProtectedStorage
恢复或继续服务 (Resume or Continue Service)
We will resume and continue an all ready paused service with continue
command like below.
我们将使用如下continue
命令恢复并继续所有就绪的暂停服务。
$ sc continue ProtectedStorage
翻译自: https://www.poftut.com/service-management-windows-sc-command-line/
windows sc命令