http://blog.csdn.net/gbenson/article/details/51083817
The service systemctl script of CentOS 7 is stored in: /usr/lib/systemd/, which is divided into system (system) and user (user), and programs that need to be run without logging in are stored in system services, namely: /usr/ lib/systemd/system directory
Each service ends with .service and is generally divided into three parts: [Unit], [Service] and [Install]. This service I wrote is used to start the tomcat project:
- vim /usr/lib/systemd/system/tomcat.service
- [Unit]
- Description=tomcatapi
- After=network.target
- [Service]
- Type=forking
- PIDFile=/usr/local/tomcat/tomcat.pid
- ExecStart=/usr/local/tomcat/bin/startup.sh
- ExecReload=
- ExecStop=/usr/local/tomcat/bin/shutdown.sh
- PrivateTmp=true
- [Install]
- WantedBy=multi-user.target
The [Unit] section is mainly for the description of this service, including Description and After, Description is used to describe the service, and After is used to describe the service category;
The [Service] part is the key to the service and is the setting of some specific operating parameters of the service. Here Type=forking is the form of background operation, PIDFile is the file path where the PID is stored, ExecStart is the running command of the service, ExecReload is the restart command, ExecStop In order to stop the command, PrivateTmp=True means to allocate an independent temporary space to the service. Note: The start, restart and stop commands in the [Service] section all require the use of absolute paths, and an error will be reported if relative paths are used;
The [Install] section is the related settings for service installation, which can be set to multi-user
After the service script is written as above, it is saved in the /usr/lib/systemd/system directory with the permission of 754. At this time, systemctl can be used for testing.
Finally, use the following command to add the service to boot and start:
- systemctl enable tomcat
Task
old order | new order | |
make a service start automatically | chkconfig --level 3 httpd on | systemctl enable httpd.service |
Make a service not start automatically | 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 shows if 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 |
systemctl start nginx.service
systemctl enable nginx.service
systemctl disable nginx.service
systemctl status nginx.service
systemctl restart nginx.service
systemctl list-units --type=service