Easily Create Custom Services in the Windows System

Sometimes we need to run command line programs in Windows that need to be started in the background (such as Caddy, Aria2 or V2Ray, etc.).
At this time, generally we need to open a cmd or PowerShell window. After running the command line program, minimize the window and cannot close it.
When you need to close or restart the command line program, manually close or restart it in the cmd or PowerShell window. In short, it is quite troublesome to operate.
So, is there a simple way to integrate these command line programs into Windows services? The answer is yes.
NSSM – the Non-Sucking Service Manager, you can use it to easily create custom services in Windows systems with simple steps.

Download NSSM

Official website:https://nssm.cc/
Download link:https://nssm.cc/ci/nssm-2.24-101-g897c7ad.zip
Alternative links:https://dl.lamp.sh/files/nssm-2.24-101-g897c7ad.zip

Install NSSM

Unzip nssm-2.24-101-g897c7ad.zip, which is divided into two folders: win32 and win64.
According to the judgment of the system, Windows 10 x64 is used for demonstration here, and the folder win64 is copied to the root directory of the D drive.
And rename the folder win64 to nssm. The directory structure at this time is as follows:

At this point, the installation is complete.

Demonstrate NSSM usage

1. Install Aria2 v1.35.0

Download Aria2 v1.35.0 for Windows, download link:https://dl.lamp.sh/files/aria2.rar
Unzip aria2.rar to the root directory of drive D. The directory structure at this time is as follows:

Open the cmd (command prompt) window in administrator mode and enter the following command:

D:\nssm\nssm.exe install Aria2 D:\aria2\aria2c.exe
D:\nssm\nssm.exe set Aria2 AppDirectory D:\aria2
D:\nssm\nssm.exe set Aria2 AppParameters --conf=aria2.conf
D:\nssm\nssm.exe set Aria2 Description "aria2 is a lightweight multi-protocol & multi-source, cross platform download utility operated in command-line. It supports HTTP/HTTPS, FTP, SFTP, BitTorrent and Metalink."


Win + R, enter services.msc to open the service window.

You can see that the Aria2 service has been successfully installed.

Select Aria2, right-click, properties, you can see as shown in the figure below, click the[Start]button to start the Aria2 service.

Go back to the D:\aria2 directory and there is aria2.html (AriANG), use the browser to open the html file, the interface is as shown below.

At this point, Aria2 v1.35.0 + AriaNG has been installed successfully.

2. Install Caddy v1.0.5

Download Caddy v1.0.5 for Windows.
Download link (32-bit):https://dl.lamp.sh/files/caddy_386.exe
Download link (64-bit):https://dl.lamp.sh/files/caddy_amd64.exe

Here we take downloading caddy_amd64.exe as an example and put it in the caddy directory of drive D. The directory structure at this time is as follows:
D:\caddy\caddy_amd64.exe

Create a new configuration file Caddyfile under D:\caddy. For details on how to configure it, please refer to the online tutorial.
Here is an example:

:80 {
root D:\download
index index.html
log access.log
}
:443 {
root D:\download
tls xxxxxxx.crt xxxxxxx.key
index index.html
log access_ssl.log
}

Open the cmd (command prompt) window in administrator mode and enter the following command:

D:\nssm\nssm.exe install Caddy D:\caddy\caddy_amd64.exe
D:\nssm\nssm.exe set Caddy AppDirectory D:\caddy\
D:\nssm\nssm.exe set Caddy AppParameters -conf Caddyfile
D:\nssm\nssm.exe set Caddy Description "Fast, multi-platform web server with automatic HTTPS"

The subsequent usage is exactly the same as Aria2 above.

3. Uninstall the service

Take uninstalling the Aria2 you just installed as an example.
First, stop the Aria2 service in the Windows service.

Open the cmd (command prompt) window in administrator mode and enter the following command:

D:\nssm\nssm.exe remove Aria2 confirm

Conclusion

NSSM actually encapsulates the command line into a Windows service, thereby simplifying command line operations. It is a very convenient tool for creating custom services.
There are actually many ways to use this tool, so I won’t list them all here.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button