CentOS 7.x allows you to set custom startup and add custom system services.
Centos System Service Script directory:
- /Usr/lib/systemd/
There are systems and users,
If you need to start a program that can run without logging on to the system, it exists in the system service, namely:
- /Lib/systemd/system/
Otherwise, the program that can run only after the user logs on exists in the user
1/lib/systemd/system equals/usr/lib/systemd/system2/lib/systemd/user equals/usr/lib/systemd/user
The service ends with. service.
The following uses nginx as an example.
1. Create a service file
1 cd /lib/systemd/system/2 cp -p tuned.service httpd.service3 vim httpd.service
1 [Unit] 2 Description=Apache Httpd Serveice 3 After=httpdlog.target 4 5 [Service] 6 Type=forking 7 ExecStart=/usr/local/apache2/bin/apachectl start 8 ExecReload=/usr/local/apache2/bin/apachectl restart 9 ExecStop=/usr/local/apache2/bin/apachectl stop10 PrivateTmp=true11 12 [Install]13 WantedBy=multi-user.target14 ~
[Unit]: Service Description
Description: describes the service.
After: Describes the service category.
[Service] Service running parameter settings
Type = forking is the form of background running
ExecStart is the specific running command of the service.
ExecReload is the restart command
ExecStop is the Stop command
PrivateTmp = True indicates that an independent temporary space is allocated to the service.
Note: the absolute path is required for the [Service] startup, restart, and stop commands.
[Install] service installation settings, which can be set to multiple users
2. Save the Directory
Save as 754 permissions in the directory:
- /Lib/systemd/system
3. Set auto-start upon startup
- Systemctl enable nginx. service
4. Other commands
| Task |
Old commands |
New command |
| Enable a service automatically |
Chkconfig -- level 3 httpd on |
Systemctl enable httpd. service |
| Disable Automatic startup of a service |
Chkconfig -- level 3 httpd off |
Systemctl disable httpd. service |
| Check service status |
Service httpd status |
Systemctl status httpd. service (service details) Systemctl is-active httpd. service (only show whether it is Active) |
| Show all started services |
Chkconfig -- list |
Systemctl list-units -- type = service |
| Start a service |
Service httpd start |
Systemctl start httpd. service |
| Stop a service |
Service httpd stop |
Systemctl stop httpd. service |
| Restart a service |
Service httpd restart |
Systemctl restart httpd. service |
Start the nginx Service
systemctl start nginx.service
Set auto-start
systemctl enable nginx.service
Stop Auto-start
systemctl disable nginx.service
View Current Service Status
systemctl status nginx.service
Restart the service
systemctl restart nginx.service
View all started services
systemctl list-units --type=service