linux系统服务管理

linux系统服务管理

CentOS 中有一些的工具可以使系统的服务开机自动启动和关闭。

管理工具 - chkconfig

CentOS6中使用,CentOS7保留过度的。

[root@localhost ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'

netconsole      0:关    1:关    2:关    3:关    4:关    5:关    6:关
network         0:关    1:关    2:开    3:开    4:开    5:开    6:关

chkconfig 配置的启动脚本路径/etc/init.d/

[root@localhost ~]# ls /etc/init.d/
functions  netconsole  network  README

chkconfig的使用:

用法:

  • chkconfig [--list] [--type <type>] [name]

    :列出所有服务

  • chkconfig --add <name>

    :添加开机启动服务

  • chkconfig --del <name>

    :删除开启启动服务

  • chkconfig --override <name>

    :服务概览

  • chkconfig [--level <levels>] [--type <type>] <name> <on|off|reset|resetpriorities> :设置启动级别

CentOS6即以下的linux发行版的服务运行级别:

  • 0 关机

  • 1 单用户,不用密码即可登录,修改root密码

  • 2 多用户级别,但没有NFS

  • 3 完整多用户级别

  • 4 保留

  • 5 图形界面级别

  • 6 重启级别 init6

[root@localhost ~]# chkconfig --level 2345 network off # network 2345 级别关闭
[root@localhost ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

netconsole      0:关    1:关    2:关    3:关    4:关    5:关    6:关
network         0:关    1:关    2:关    3:关    4:关    5:关    6:关
[root@localhost ~]# chkconfig --level 2345 network on # network 2345 级别启动
[root@localhost ~]# chkconfig --del network  # 删除服务(注:启动脚本要再/etc/init.d/ 下)
[root@localhost ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

netconsole      0:关    1:关    2:关    3:关    4:关    5:关    6:关
[root@localhost ~]# chkconfig --add network # 添加服务(注:启动脚本要再/etc/init.d/ 下)
[root@localhost ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

netconsole      0:关    1:关    2:关    3:关    4:关    5:关    6:关
network         0:关    1:关    2:开    3:开    4:开    5:开    6:关

启动脚本的格式:

# chkconfig: 2345 10 90 #(必须:运行级别 开机开启顺序,越小越早,关机关闭顺序)
. /etc/init.d/functions #(必须)

chkconfig 设置开启启动实例:nginx

[root@localhost ~]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings

NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"

start() {
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}

stop() {
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}

reload(){
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}

restart(){
stop
start
}

configtest(){
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}

case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac

exit $RETVAL
[root@localhost ~]# chmod 755 /etc/init.d/nginx
[root@localhost ~]# chkconfig --add nginx

管理工具 - systemd

在CentOS7及以后的版本中代替chkconfig。

用法:

  • systemctl list-units --all --type=service // 列出系统服务

  • systemctl enable crond.service //让服务开机启动

  • systemctl disable crond //不让开机启动

  • systemctl status crond //查看状态

  • systemctl stop crond //停止服务

  • systemctl start crond //启动服务

  • systemctl restart crond //重启服务

  • systemctl is-enabled crond //检查服务是否开机启动

[root@localhost ~]# systemctl disable network
network.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig network off
[root@localhost ~]# systemctl enable network
network.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig network on
[root@localhost ~]# systemctl restart network
[root@localhost ~]# systemctl stop network
[root@localhost ~]# systemctl start network
[root@localhost ~]# systemctl is-enabled network
network.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig network --level=5
enabled

systemd 中的系统运行级别:

[root@localhost ~]# ls -l /usr/lib/systemd/system/runlevel*
lrwxrwxrwx. 1 root root 15 7月  12 10:32 /usr/lib/systemd/system/runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 7月  12 10:32 /usr/lib/systemd/system/runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 7月  12 10:32 /usr/lib/systemd/system/runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 7月  12 10:32 /usr/lib/systemd/system/runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 7月  12 10:32 /usr/lib/systemd/system/runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 7月  12 10:32 /usr/lib/systemd/system/runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 7月  12 10:32 /usr/lib/systemd/system/runlevel6.target -> reboot.target

ls /usr/lib/systemd/system //系统所有unit,分为以下类型

  • service 系统服务

  • target 多个unit组成的组

  • device 硬件设备

  • mount 文件系统挂载点

  • automount 自动挂载点

  • path 文件或路径

  • scope 不是由systemd启动的外部进程

  • slice 进程组

  • snapshot systemd快照

  • socket 进程间通信套接字

  • swap swap文件

  • timer 定时器

unit相关的命令

  • systemctl list-units //列出正在运行的

  • unit systemctl list-units --all //列出所有,包括失败的或者inactive的

  • systemctl list-units --all --state=inactive //列出inactive的unit

  • systemctl list-units --type=service//列出状态为active的service

  • systemctl is-active crond.service //查看某个服务是否为active

系统为了方便管理用target来管理unit

  • systemctl list-unit-files --type=target

  • systemctl list-dependencies multi-user.target //查看指定target下面有哪些unit

  • systemctl get-default //查看系统默认的target

  • systemctl set-default multi-user.target

    一个service属于一种类型的unit ,多个unit组成了一个target 一个target里面包含了多个service

cat /usr/lib/systemd/system/sshd.service //看[install]部分

systemd设置开启启动实例:nginx

[root@localhost ~]# vim /etc/systemd/system/nginx
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

解析:

[Unit]

Description : 服务的简单描述

Documentation : 服务文档

Before、After:定义启动顺序。Before=xxx.service,代表本服务在xxx.service启动之前启动。After=xxx.service,代表本服务在xxx.service之后启动。

Requires:这个单元启动了,它需要的单元也会被启动;它需要的单元被停止了,这个单元也停止了。

Wants:推荐使用。这个单元启动了,它需要的单元也会被启动;它需要的单元被停止了,对本单元没有影响。

[Service]

Type=simple(默认值):systemd认为该服务将立即启动。服务进程不会fork。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型。

Type=forking:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求,使用此类型启动即可。使用此启动类型应同时指定 PIDFile=,以便systemd能够跟踪服务的主进程。

Type=oneshot:这一选项适用于只执行一项任务、随后立即退出的服务。可能需要同时设置 RemainAfterExit=yes 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。

Type=notify:与 Type=simple 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 libsystemd-daemon.so 提供。

Type=dbus:若以此方式启动,当指定的 BusName 出现在DBus系统总线上时,systemd认为服务就绪。

Type=idle: systemd会等待所有任务(Jobs)处理完成后,才开始执行idle类型的单元。除此之外,其他行为和Type=simple 类似。

PIDFile:pid文件路径

ExecStart:指定启动单元的命令或者脚本,ExecStartPre和ExecStartPost节指定在ExecStart之前或者之后用户自定义执行的脚本。Type=oneshot允许指定多个希望顺序执行的用户自定义命令。

ExecReload:指定单元停止时执行的命令或者脚本。

ExecStop:指定单元停止时执行的命令或者脚本。

PrivateTmp:True表示给服务分配独立的临时空间

Restart:这个选项如果被允许,服务重启的时候进程会退出,会通过systemctl命令执行清除并重启的操作。

RemainAfterExit:如果设置这个选择为真,服务会被认为是在激活状态,即使所以的进程已经退出,默认的值为假,这个选项只有在Type=oneshot时需要被配置。

[Install]

Alias:为单元提供一个空间分离的附加名字。

RequiredBy:单元被允许运行需要的一系列依赖单元,RequiredBy列表从Require获得依赖信息。

WantBy:单元被允许运行需要的弱依赖性单元,Wantby从Want列表获得依赖信息。

Also:指出和单元一起安装或者被协助的单元。

DefaultInstance:实例单元的限制,这个选项指定如果单元被允许运行默认的实例。

最后更新于

这有帮助吗?