Centos service configuration and service startup

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:

[plain] view plain copy View code snippets on CODE Derive to my code slice
  1. vim /usr/lib/systemd/system/tomcat.service  
[plain] view plain copy View code snippets on CODE Derive to my code slice
  1. [Unit]  
  2. Description=tomcatapi  
  3. After=network.target  
  4.    
  5. [Service]  
  6. Type=forking  
  7. PIDFile=/usr/local/tomcat/tomcat.pid  
  8. ExecStart=/usr/local/tomcat/bin/startup.sh  
  9. ExecReload=  
  10. ExecStop=/usr/local/tomcat/bin/shutdown.sh  
  11. PrivateTmp=true  
  12.    
  13. [Install]  
  14. 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:

[html] view plain copy View code snippets on CODE Derive to my code slice
  1. 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

 

Start the nginx service

systemctl start nginx.service

Set up to start automatically

systemctl enable nginx.service

stop auto-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=326305895&siteId=291194637