Unit服务脚本实践

Unit服务脚本实践

  • 1、Unit服务脚本小试牛刀
  • 2、redis自启动脚本
    • 2.1、redis安装后自动生成的Unit脚本:redis.service
    • 2.2、redis安装后自动生成的Unit脚本:redis-sentinel.service
    • 2.3、我自己写redis的Unit脚本
  • 3、mysqld自启脚本
    • 3.1、使用/etc/rc.d/rc.local
    • 3.2、使用Unit脚本
    • 3.3、mysql安装后自动生成Unit脚本:mysqld.service

系统服务(system):开机不登陆就能运行的程序(常用于开机自启)。
用户服务(user):需要登陆以后才能运行的程序。

1、Unit服务脚本小试牛刀

在CentOS7下,使用Unit的方式管理开机自启动服务。服务配置脚本在/usr/lib/systemd/system目录。
(注意:自定义开机自启动服务的.service配置文件必须放在/usr/lib/systemd/system这个目录下面)

vim /usr/lib/systemd/system/nginx.service
=================
[Unit] #服务的说明
Description=nginx  #描述服务
After=network.target

[Service] #服务运行参数的设置
Type=forking #是后台运行的形式
ExecStart=/opt/nginx/sbin/nginx #为服务的具体启动运行命令
ExecReload=/opt/nginx/sbin/nginx -s reload #为服务重启命令
ExecStop=/opt/nginx/sbin/nginx -s quit #为服务停止命令
PrivateTmp=true #表示给服务分配独立的临时空间

[Install] #运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
WantedBy=multi-user.target

注意:[Service] 的启动、重启、停止命令全部要求使用绝对路径

2、redis自启动脚本

2.1、redis安装后自动生成的Unit脚本:redis.service

[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=/usr/bin/redis-server /etc/redis.conf --supervised systemd
ExecStop=/usr/libexec/redis-shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target

2.2、redis安装后自动生成的Unit脚本:redis-sentinel.service

[Unit]
Description=Redis Sentinel
After=network.target

[Service]
ExecStart=/usr/bin/redis-sentinel /etc/redis-sentinel.conf --supervised systemd
ExecStop=/usr/libexec/redis-shutdown redis-sentinel
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target

2.3、我自己写redis的Unit脚本

vim /usr/lib/systemd/system/redisd.service
=================
[Unit] #服务的说明
Description=redisd #描述服务
After=network.target

[Service] #服务运行参数的设置
Type=forking #是后台运行的形式
ExecStart=/usr/bin/redis-server /etc/redis.conf #为服务的具体启动运行命令
ExecReload=/bin/kill -s HUP $MNPID
ExecStop=/bin/kill -s QUIT $MNPID
Restart=always
User=root
PrivateTmp=true #表示给服务分配独立的临时空间

[Install] #运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable redisd
systemctl start redisd

3、mysqld自启脚本

3.1、使用/etc/rc.d/rc.local

将下面命令直接写入/etc/rc.d/rc.local文件内:
/etc/rc.d/init.d/mysqld start

关于CentOS7中,/etc/rc.d/rc.local失效问题
/etc/rc.d/init.d/mysqld start
在CentOS7中,官方将/etc/rc.d/rc.local 的开机自启的权限禁止掉了,他为了兼容性,设置了这个,但是并不默认启动.如果需要的话.执行以下代码

chmod +x /etc/rc.d/rc.local
1
将文件授权,这样他就可以开机自启了.
官方在新版上推荐使用systemcd进行自启动.
在rc.local文件中,注释中有这么一段话

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.









创建自己的systemd服务或udev规则是非常明智的

It is highly advisable to create own systemd services or udev rules
在引导过程中运行脚本,而不是使用这个文件。

to run scripts during boot instead of using this file.
#与以前的版本相比,由于在引导过程中并行执行

In contrast to previous versions due to parallel execution during boot
此脚本将不会在所有其他服务之后运行。

this script will NOT be run after all other services.
#请注意,您必须运行'chmod +x /etc/rc.d/rc.local'

Please note that you must run ‘chmod +x /etc/rc.d/rc.local’ to ensure
该脚本将在引导期间执行。*

that this script will be executed during boot.*

3.2、使用Unit脚本

cat > /etc/systemd/system/mysqld.service << EOF 
[Unit]
Description=MySQL SerVice
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf

[Install]
WantedBy=multi-user.target
EOF
cat > /etc/systemd/system/mysqld.service << EOF 
[Unit]
Description=MySQL SerVice
After=network.target

[Service]
Type=simple
ExecStart=/etc/rc.d/init.d/mysqld start

[Install]
WantedBy=multi-user.target
EOF
#重新加载配置文件
systemctl daemon-reload
 

3.3、mysql安装后自动生成Unit脚本:mysqld.service

# It's not recommended to modify this file in-place, because it will be overwritten during package upgrades.  
# If you want to customize, the best way is to use systemctl edit:
#
# $ systemctl edit mysqld.service
# this will create file
#  /etc/systemd/system/mysqld.service.d/override.conf
# which be parsed after the file mysqld.service itself is parsed.
#
# For example, if you want to increase mysql's open-files-limit to 20000
# add following when editing with command above:
#
#	[Service]
#	LimitNOFILE=20000
#
# Or if you require to execute pre and post scripts in the unit file as root, set
#       PermissionsStartOnly=true
#
# For more info about custom unit files, see systemd.unit(5) or
# http://fedoraproject.org/wiki/Systemd#How_do_I_customize_a_unit_file.2F_add_a_custom_unit_file.3F
#
# Don't forget to reload systemd daemon after you change unit configuration:
# root> systemctl --system daemon-reload

[Unit]
Description=MySQL 8.0 database server
After=syslog.target
After=network.target

[Service]
Type=notify
User=mysql
Group=mysql

ExecStartPre=/usr/libexec/mysql-check-socket
ExecStartPre=/usr/libexec/mysql-prepare-db-dir %n
# Note: we set --basedir to prevent probes that might trigger SELinux alarms,
# per bug #547485
ExecStart=/usr/libexec/mysqld --basedir=/usr
ExecStartPost=/usr/libexec/mysql-check-upgrade
ExecStopPost=/usr/libexec/mysql-wait-stop

# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=300

# Place temp files in a secure directory, not /tmp
PrivateTmp=true
Restart=on-failure
RestartPreventExitStatus=1

# Sets open_files_limit
LimitNOFILE = 10000

# Set enviroment variable MYSQLD_PARENT_PID. This is required for SQL restart command.
Environment=MYSQLD_PARENT_PID=1

[Install]
WantedBy=multi-user.target

你可能感兴趣的:(Linux,unit服务脚本,centos服务脚本)