Centos 7.x write startup service

Centos 7 and above use Systemd to initialize the system. Systemd is the latest initialization system (init) in the Linux system. Its main design goal is to overcome the inherent shortcomings of sysvinit and improve the startup speed of the system. Details about Systemd are introduced here.

The Systemd service file ends with .service. For example, if you want to create nginx for boot-up now, if you install it with the yum install command, the yum command will automatically create the nginx.service file. Use the command

1
systemcel enable nginx.service
to set the boot-up directly.
Here I compiled and installed it with source code, so I have to manually create the nginx.service service file.
The programs that can be run without logging in at boot are stored in the system service (system), namely:

1
/lib/systemd/system/
1. Create the nginx.service file in the system service directory

1
vim /lib/systemd/system/ The content of nginx.service
is as follows

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target


[Unit]: Service description
Description: Describe the service
After: Describe the service category
[Service] The setting of the service running parameters
Type=forking is the form of background operation
ExecStart is the specific running command of the service
ExecReload is the restart command
ExecStop is the stop command
PrivateTmp=True means to assign an independent temporary to the service Space
Note : The start, restart and stop commands of [Service] all require
the relevant settings of the service installation under the [Install] run level of the absolute path, which can be set to multi-user, that is, the system run level is 3,

save and exit.

2. Set the boot


systemctl enable nginx.service
3. Other commands
start the nginx service

systemctl start nginx.service
Set the boot self-start

systemctl enable nginx.service
Stop booting and self-start

systemctl disable nginx.service
View the current status of the service

systemctl status nginx.service
Restart the service

systemctl restart nginx.service
View all started services

systemctl list-units --type=service

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326381081&siteId=291194637