CentOS 7. x sets custom startup and adds custom system services

January 15, 2017 1424 point heat 0 likes 0 comments

Centos system service script directory:
/usr/lib/systemd/

There are systems and users,
If you need to start the program that can run without login, it is stored in the system service, that is:
/lib/systemd/system/

On the contrary, the programs that can be run only after the user logs in are stored in the user
The service ends with. service.
Take nginx for example
1. Establish service documents

 vim /lib/systemd/system/nginx.service
 [Unit]   Description=nginx   After=network.target     [Service]   Type=forking   ExecStart=/www/lanmps/init.d/nginx start   ExecReload=/www/lanmps/init.d/nginx restart   ExecStop=/www/lanmps/init.d/nginx  stop   PrivateTmp=true     [Install]   WantedBy=multi-user.target

[Unit]: Description of the service
Description: Describe the service
After: Describe the service category
[Service] Setting of service running parameters
Type=forking is the form of background running
ExecStart is the specific running command of the service
ExecReload is a restart command
ExecStop is a stop command
PrivateTmp=True indicates that independent temporary space is allocated to the service
Note: The start, restart, and stop commands of [Service] all require an absolute path
[Install] Settings related to service installation, which can be set to multiple users

2. Save directory
Save in directory with 754 permissions:
/lib/systemd/system

3. Set the startup to start automatically

 systemctl enable nginx.service

4. Other commands

Task old instruction new instruction
Make a service automatically start chkconfig -- level 3 httpd on systemctl enable httpd.service
Make a service not automatically start chkconfig -- level 3 httpd off systemctl disable httpd.service
Check the service status service httpd status systemctl status httpd.service (service details)
Systemctl is active httpd.service (only show whether it is Active)
Display all started services chkconfig -- list systemctl list units -- type=service
Start a service httpd start systemctl start httpd.service
Stop a service httpd stop systemctl stop httpd.service
Restart a service httpd restart systemctl restart httpd.service

Start nginx service

 systemctl start nginx.service

Set automatic startup

 systemctl enable nginx.service

Stop startup and start automatically

 systemctl disable nginx.service

View the current status of the service

 systemctl status nginx.service

Restart service

 systemctl restart nginx.service

View all started services

 systemctl list-units --type=service

Gcod

If life is just like the first sight, what is the sad autumn wind painting fan

Article comments