CentOS7 add a custom system services

CentOS 6 version of the system service startup mode is /etc/init.d script, CentOS 7 with powerful systemctl to manage system services, significantly improving the efficiency of the system services, but the configuration of the service and the previous version is completely different, which is great progress, systemctl a very easy to use.

To add a custom CentOS7 systems and services is as follows:

1) write a script file system services customized;

2) with systemctl command to customize a system service is set to power on / off from the start / stop.

In this paper, Oracle database as an example to introduce the knowledge to add custom system services. Assume ORACLE_HOME environment variable value / oracle / home, you adjust the content of the script according to their actual situation, the paper / oracle / home replaced value of your ORACLE_HOME.

First, write Oracle Database start / restart / off script

1, start the Oracle database shell script

Script starts the Oracle database is / oracle / home / bin / dbstart, reads as follows:

sqlplus / as sysdba <<EOF
startup;
EOF

Modify the script permissions to perform.

chmod +x /oracle/home/bin/dbstart

2, restart the Oracle database shell script

Script starts the Oracle database is / oracle / home / bin / dbrestart, reads as follows:

sqlplus / as sysdba <<EOF
shutdown immediate;
startup;
EOF

Modify the script permissions to perform.

chmod +x /oracle/home/bin/dbrestart

3, close the Oracle database shell script

Script starts the Oracle database is / oracle / home / bin / dbshut, reads as follows:

sqlplus / as sysdba <<EOF
shutdown immediate;
EOF

Modify the script permissions to perform.

chmod +x /oracle/home/bin/dbshut

Second, write a configuration file to customize services

Start system services / restart / stop is determined by its configuration file, the paper Oracle database system service named oracle.service.

Create a service profile /usr/lib/systemd/system/oracle.service, reads as follows:

[Unit]
Description=Oracle RDBMS
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/su - oracle -c "/oracle/home/bin/dbstart >> /tmp/oracle.log"
ExecReload=/usr/bin/su - oracle -c "/oracle/home/bin/dbrestart >> /tmp/oracle.log"
ExecStop=/usr/bin/su - oracle -c "/oracle/home/bin/dbshut >> /tmp/oracle.log"
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Next comes the meaning of each part of the service profile.

1, Unit part

Unit is part of the boot sequence and dependencies.

[Unit]
Description=Oracle RDBMS
After=network.target

Description field: Gives a brief description of the current service.

Documentation field: document given location.

After field: indicates that this service should start after a service.

Before field: indicates that this service should start before a service.

Before and After field involves only boot sequence does not involve dependencies. Setting dependencies need to use Wants field and Requires fields.

Wants field: Local service between a service and the existence of "dependency" system, if the dependent services failed to start or stop the run, does not affect the continued operation of the Service.

Requires field, indicates that this service between a service and there is "strong dependence" system, if the dependent services failed to start or stop running, the service must also withdraw.

2, Service section

How to Service section defines the start / restart / stop the service.

[Service]
Type=simple
ExecStart=/usr/bin/su - oracle -c "/oracle/home/bin/dbstart >> /tmp/oracle.log"
ExecReload=/usr/bin/su - oracle -c "/oracle/home/bin/dbrestart >> /tmp/oracle.log"
ExecStop=/usr/bin/su - oracle -c "/oracle/home/bin/dbshut >> /tmp/oracle.log"
RemainAfterExit=yes

1) Start type (Type Field)

Type field defines the startup type. It can be set to the following values.

simple (default value): ExecStart field to start the process of the main process.

forking: ExecStart field will fork () way to start, then the parent process will exit, the child becomes the primary process.

oneshot: similar to the simple, but only once, Systemd will wait for it to finish execution before start other services.

dbus: similar to the simple, but will wait for the signal to start D-Bus.

notify: similar to the simple, notification will be issued after the start signal, then Systemd then start other services.

idle: similar to the simple, but to wait until other tasks are executed, it will start the service.

2) to start the service (ExecStart field)

Command to execute when to start the service, which can be executable programs, system commands or shell script.

3) Restart Service (ExecReload field)

Restart command to execute when the service can be executable programs, system commands or shell script.

4) Stop the service (ExecStop field)

Command to execute when out of service, can be executable programs, system commands or shell script.

5) If RemainAfterExit field is set to yes, saying that he withdraw from the process, the service still remains unchanged.

6) the service profile can also read the environment variable parameter file, I personally think that is too much trouble, there is no need, not introduced, the environment variable There are many ways the program can be configured in the script, you can also use "su
- "Methods.

3, Install section

Install section defines how to install the configuration file, that is, how to do boot.

[Install]
WantedBy=multi-user.target

WantedBy field: Target indicate that the service is located.

Target is the meaning of the service group, represents a set of services. WantedBy = multi-user.target refers, oracle where Target is multi-user.target (multi-user mode).

This setting is very important, because the execution systemctl enable
when oracle.service command, oracle.service will be linked into /etc/systemd/system/multi-user.target.wants directory to achieve start-up function.

4. Restart behavior

Service section there are some fields, it defines the restart behavior.

1) KillMode field

How KillMode field definitions Systemd stop the sshd service, value can be set as follows:

control-group (the default values): current control process of all sub-groups which will be killed.

process: only kill the main process.

mixed: the main process will receive the SIGTERM signal, the child received SIGKILL signal.

none: No process will be killed, just stop command execution services.

2) Restart field

Restart service after field defines the program exits, Systemd restart mode, the value can be set as follows:

no (default value): not restart after exit.

on-success: only when normal exit (exit status 0), will restart.

on-failure: non-normal exit (exit status is non-zero), and comprising a signal termination timeout occurs restart.

on-abnormal: only the signal termination and overtime, will restart.

on-abort: terminate only upon receipt of the signal is not captured, it will restart.

on-watchdog: time-out exit will restart.

always: Whatever the reason for exit, always restart.

3) RestartSec field.

RestartSec fields: representation before Systemd restart the service, we need to wait a number of seconds.

Third, use a custom service

1, reload the service profile

After each modify the service configuration file, execute the following command to reload the configuration file services.

systemctl daemon-reload

2, start / stop / re-start oracle service

systemctl start oracle # 启动oracle服务。

systemctl restart oracle # 重启oracle服务。

systemctl stop oracle # 关闭oracle服务。

3, the oracle service is set to power on / off from the start / stop

systemctl is-enabled oracle # 查看oracle服务是否是开机自启动。

systemctl enable oracle # 把oracle服务设置为开机自启动。

4, view an Oracle instance to start / stop logs

Oracle instance starts in /tmp/oracle.log log file.

Note that only by systemctl enable / disable Oracle instances and listeners will write the log, manually execute the script does not write the log.

Fourth, the copyright notice

C Language Technology Network original article, reproduced please indicate the source link to the article, the author and original.

Source: C Language Technology Network ( www.freecplus.net )

Author: Ethics Code farming

Published an original article · won praise 2 · Views 6720

Guess you like

Origin blog.csdn.net/u010806950/article/details/105059437