This is the fifth day of my participation in Gwen Challenge

Sometimes there was a server instability often hang up, the deployment in service on the server will be down, need to be done manually restart the service, this is very troublesome, is also very smart, heavily influence the customer experience at the same time, so at this moment we only need to set up a server since the rev. Service can be perfect to solve this problem.

Centos7 System parameters:

Create a service file in the /lib/systemd/system/ directory (some may be in /usr/lib/systemd/system/ depending on the system).

vim /lib/systemd/system/test.service

[Unit]

Description=test

After=network.target

[Service]

Type=forking

ExecStart=/root/test.sh

ExecReload=/root/testreload.sh

ExecStop=/root/teststop.sh

PrivateTmp=true

[Install]

WantedBy=multi-user.target
Copy the code

Where their fields respectively represent

Description: Describes the service

After: Describes the service type

[Service] Sets Service running parameters

Type=forking is run in the background

For example, / WWW /lanmps/init.d/nginx restart

ExecReload is the restart command

ExecStop indicates the stop command

PrivateTmp=True assigns independent temporary space to the service

Then save the file if you save the file according to authority is not enough, you can switch the super administrator sudo su, then save the file, give the file permissions, 754 gives is completed to see if success can be observed by ll, first on behalf of the file type, and then to a set of three RWX represents the current user permissions, r – x on behalf of the user group permissions, R — stands for other permissions

After the permission is granted, systemctl enable test.service is set to start upon system startup. Then, you can run the systemctl status nginx.service command to check the status of the service

Other commands

Systemctl start test.service Starts the nginx service systemctl enable test.service Enables the automatic startup upon startup systemctl disable test.service Stops the automatic startup upon startup Systemctl status test.service Checks the current service status systemctl restart test.service Restarts the service systemctl list-units --type=service View all services that have been startedCopy the code

If you set active to inactive after startup, the service is waiting to be executed and has succeeded. In this case, you can reboot the service to check whether you have successfully set it.

Centos6.6 use the following methods:

Since Centos6 does not have systemctl, there are two ways to resolve centos6: chkconfig and rc.local. However, because of this method, I did not succeed. /etc/rc.d/rc.local = /etc/rc.d/rc.local = /etc/rc.d/rc.local = /etc/rc.d/rc.local = /etc/rc.d/rc.local = /etc/rc.d/rc.local = /etc/rc.d/rc.local = /etc/rc.d/rc.local = /etc/rc.d/rc.local = /etc/rc.d So I’m not going to describe it here. Instead, I use chkconfig to create a sh script file in /etc/init.d/

#! /bin/sh #add for chkconfig #chkconfig: 2345 70 30 #description: The description of the shell # XXXX # the first process name, which will be used when setting autobootCopy the code

# command to start the service

Service iptables restart sh /home/hipay/test.sh(Run the script)Copy the code

Chkconfig: 2345 70 30,2345 indicates the running level

Running level 0: the system is stopped. The default running level cannot be set to 0. Otherwise, the system cannot start normally

Running level 1: single user, root for system maintenance. Remote login is prohibited

Run level 2: Multi-user (without NFS)

Run level 3: Full multi-user (with NFS), login to console command line mode

Running level 4: The system is not used. Reserved

Run level 5: X11 console, enter graphical GUI mode after login

Run level 6: The system shuts down and restarts normally. The default run level cannot be set to 6. Otherwise, the system cannot start normally

70 is the future startup sequence number of the script. If the startup sequence number of other programs is smaller than 70 (for example, 44 or 45), the script must wait until all of these programs are started.

30 indicates the stop sequence number of the script when the system is shut down

Sh to chkconfig (chkconfig –add test.sh). Run the chkconfig command to view all startup files.

It should be ok after reboot!!

If you have any questions, please comment (0—–0)