Use the SC command to manage windows services manually

When you need to manage (add, delete, modify, etc.) system services in Windows system, you can use the SC command provided by Windows system to manage system services.

(take Windows 7 as an example)

Most of the “command parameters” in the SC command need to be “run as an administrator” (that is, start the CMD command line window as an administrator, and then execute the relevant commands in the command line window; note that any command executed in the command line window will be executed with the administrator’s permission. The operation method of starting the CMD command line window as an administrator: click the start menu, input the CMD letter in the “search program and file” input box at the bottom of the start menu, and display the CMD letter in the “program” at the top of the menu cmd.exe Click the right mouse button above, click the “run as administrator (a)” menu item, and click “yes” in the new pop-up “user account control” dialog box to open the “run as administrator” command line window.) Therefore, the following “run as administrator” all the SC command operations.

View SC command help

Execute the SC command without parameters to view the help information; that is, enter SC in the command line window and enter. As a rule, I am used to adding /?To check the help content: SC /

SC command format

sc [server] <command> <ServiceName> [OptionName= “value”] [“option2”]

(according to convention, in the above parameters, the options wrapped by “bracket [XXX]” are expressed as “optional”, and the options wrapped by “angle bracket & lt; XXX &>” are expressed as “required”; the actual command should not include the bracket or angle bracket symbol.)

Server: when managing a remote machine, it can be the machine name or IP address, for example, \ \ \ \ Myserver or \ \ \ 192.168.0.1. You can omit this item when managing this machine.

Command: command parameter. Such as create, description, config, query, start, stop, delete, etc. (Note: you can execute the query command as an ordinary user without trying other command parameters that are not listed).

Servicename: a user-defined non repeatable service name, which is used for subsequent operations (similar to the role of the unique primary key of database records).

Optionname = “value”: “option name” and “option value”, setting related options and values. The option value can be enclosed in double quotation marks. Note that there must be an English space after the equal sign; if the option value in the command needs to be nested in double quotation marks, use the backslash and quotation mark “\” to escape.

Option 2: option value without option name, which can be enclosed in double quotation marks.

Examples

(take managing nginx services as an example)

Add service

sc create Nginx1.12.0 binpath= “D:\GreenSoftwares\nginx-1.12.0\ nginx.exe -P \ “D/greensoftwares/nginx-1.12.0 \” displayName = nginx web server http://nginx.org ” start= delayed-auto

Nginx1.12.0, which is immediately after the create command, is a custom service name (no spaces)

Binpath: the command to be executed when starting the service (note that there must be an English space to the right of the equal sign, the same below)

DisplayName: user defined display name of the service

Start: the start type of the service. For example: boot, system, demand, auto, delayed auto, disabled.

Set the description information for the service

SC description nginx1.12.0 “start or stop nginx server”

Modify service configuration

Use the SC config command to modify one or more configuration information of existing services.

Modify display name: SC config nginx1.12.0 displayName = “nginx server v1.12.0”

Modify start type: SC config nginx1.12.0 start = Auto

View service status

sc query Nginx1.12.0

Start the service

SC start nginx1.12.0, the command will not output startup information (or net start nginx1.12.0, it will output startup information)

Stop service

SC stop nginx1.12.0, the command will not output stop information (or net stop nginx1.12.0, it will output stop information)

Delete service

sc delete Nginx1.12.0

References and notes

I am used to downloading and using the green version (that is, zip compressed version) of MySQL database, so I need to add the MySQL database as a system service

REM fileName: installServiceForMySQL.bat
REM Run as administrator: Save the following contents to a bat file [change the path to the actual path] and right click on the file name and select "Run as administrator (A)".

REM Place this file in the /bin/ directory under the MySQL directory; then change the path of the ini file to the real path with the following parameters
D:/mysql-5.5.16-winx64/bin/mysqld.exe --install-manual MySQL5.5.16 --defaults-file="D:/mysql-5.5.16-winx64/my-medium.ini"

REM Modify MySQL service startup mode: [demand (manual start), auto (auto start), delayed-auto (auto start-delayed start), disabled (disabled start)
sc config MySQL5.5.16 start= auto
REM Add description information to the MySQL service.
sc description MySQL5.5.16 "Start, stop MySQL database services"

Right click the “computer” icon on the windows desktop and select “management (g)” to quickly enter the “computer management” interface. The “services” item is included under “services and Applications”. Press F5 to refresh the list of services.

 

Similar Posts: