Linux service registration summary

Most mainstream Linux use init.d or systemd to register services. The following demonstrates the init.d registration service with centos6.6 (centos6 does not use systemd, so there is no systemctl command) ; demonstrates the systemd registration service with centos7.1. Note xxx.sh must remember (chmod + x test.sh) to add executable permissions .

1. Linux-based init.d deployment

sudo ln -s /etc/systemd/xxxx.sh  /etc/init.d/xxxx

Where xxxx is the service name

service xxxx start       启动服务
service xxxx stop        停止服务
service xxxx status      服务状态
chkconfig xxxx on          开机启动

Note: When executing the boot command, if the report service does not support chkconfig solution

For example, the first three lines of auto_run are as follows:

#!/bin/sh
#chkconfig: 2345 80 90
#description:auto_run

The first line, told the shell used by the system, the there is a shell script like this.

In the second line, there are three parameters 2345, 80 and 90 behind chkconfig to tell the chkconfig program that you need to create a file named S80auto_run in the rc2.d ~ rc5.d directory to connect to /etc/rc.d/init.d The auto_run script in the directory. The first character is S. When the system starts, when running the script auto_run, a start parameter will be added to tell the script that it is now in startup mode. At the same time, in the rc0.d and rc6.d directories, create a file connection named K90auto_run, the first character is K, when the system is shut down, it will run auto_run, add a stop, and tell the script that it is now in shutdown mode.

Note that the above three lines, Di Second, the third line is a must, otherwise when you run chkconfig --add auto_run, will complain.

2. Linux-based systemd deployment

Create a new file test.service in the / etc / systemd / system / directory and fill in the following:

[Unit]
Description=test
After=syslog.target
[Service]
ExecStart= /etc/systemd/xxxx.sh  ./xxxx.sh
[Install]
WantedBy=multi-user.target

Note that in actual use, modify the content behind Description and ExecStart

systemctl start test 或  systemctl start test.service           启动服务
systemctl stop test 或  systemctl stop test.service             停止服务
systemctl status test 或  systemctl status test.service         服务状态
systemctl enable test 或  systemctl enable test.service         开机启动

Note: journalctl is used to query the logs collected by the systemd-journald service. The systemd-journald service is a service provided by the systemd init system to collect system logs.

journalctl -u test 或  journalctl -u test.service

Remarks: These are the problems encountered in the actual operation process and a small summary and accumulation of myself. I have borrowed many other people's articles. I will not cite them in detail here.

Published 8 original articles · Like1 · Visits 265

Guess you like

Origin blog.csdn.net/qq_40635011/article/details/88427057