コントロールサービスとデーモンた.md

Controlサービスとデーモン

1.systemd

1.1.systemdプロフィール

systemdには、第1のユーザ・アプリケーション・スペース、すなわち、/ sbinに/ INITであります

型initプログラム
SysVのスタイル:INIT(centos5)は、システムの初期化のために、後続の初期化動作がスクリプトによって達成され
ています:
コマンドスクリプトの多数は、各コマンドに対して、含むコマンドを実行し、プロセスを開始する必要がありますプロセスを停止しようとしました。結果として、システムの初期化プロセスが多数作成され、破壊プロセスは、効率が非常に低いです。
厳密にサービスを開始する特定の順序に従う必要があり、サービス間の依存関係があるかもしれません、サービスは起動時に実行できないサービスのバックを開始する前に終了しませんでした。平行ではありません。
設定ファイル:の/ etc / inittabの
成り上がりスタイル:INIT(centos6)、SYSVより効率的な並列方法でワークの形でバスに近いUbuntuの、によって開発されました。
特長:
プロセス間のアプリケーションを許可するバスの方法に基づいて相互に通信
サービスの起動を待たずに限り、初期化は、他のプロセスのに自分の状態に戻りを置くことができるよう、完了した
設定ファイル:の/ etc / inittabファイルは、/ etc / initを/ * confに。
systemdにスタイル:システムD(centos7)
特徴:SysVのとUpstarはほとんどよりも速く起動
自体をsystemdに、任意のスクリプトでサービスを開始する必要はありません自体が強力なインタプリタですサービス、ノーSH / bashのスタートアップサービスを開始することができます参加
システムは、任意のサービスを開始するために初期化されるときにsystemdは真実ではない
サービスが使用されていない限り、それはあなたが実際に開始されませんでした、始め伝えます。最初は本当にサービスにアクセスするために開始されます場合にのみ、
プロフィール:は/ usr / libに/にsystemd /システムは、/ etc / systemdに/システム

システムの起動とサーバープロセスがsystemdにシステムおよびサービスマネージャによって管理されます。このプログラムは、サーバー・デーモンや他のプロセスにシステムの起動時と動作時のシステムリソースを有効にする方法を提供します。
デーモンは、さまざまなタスクや実行を行うのを待って、バックグラウンドでの処理です。接続デーモンソケットを聞くために。ソケットは、デーモンによって生成されてもよい、またはデーモンを分離し、次にクライアントがソケット接続をデーモンに送信される確立し、他のプロセス(例えばsystemdに)によって作成されてもよいです。
サービスは、一般に、1つのまたは複数のデーモンプロセスを指すが、サービスのステータスを開始または停止するデーモンを残すことなく後に実行を継続し、(例えばネットワークインタフェースを構成するような)システムに1回の変化であるかもしれません。

1.2.systemdの新機能

サービス・システムのブートを達成し、開始
-demand活性化処理
システムの状態のスナップショット
依存ベースのサービス制御ロジックの定義を

1.3.systemd中核となる概念ユニット

systemdにユニットは、ユニットのパフォーマンスプロファイルであるマネージドサービスの概念を、使用しています。
管理サービスを実現するために、これらの構成ファイルの識別および設定のsystemdに目的別:

//这些unit文件中主要包含了系统服务、监听socket、保存的系统快照
//及其它与init相关的信息保存至以下目录:
    /usr/lib/systemd/system
    /run/systemd/system
    /etc/systemd/system

ユニットタイプ

Service unit    //文件扩展名为.service,用于定义系统服务
Target unit     //文件扩展名为.target,用于模拟实现“运行级别”
    runlevel0.target和poweroff.target        //关机
    runlevel1.target和rescue.target          //单用户模式
    runlevel2.target和multi-user.target      //对于systemd来说,2/3/4级别没有区别
    runlevel3.target和multi-user.target      //对于systemd来说,2/3/4级别没有区别
    runlevel4.target和multi-user.target      //对于systemd来说,2/3/4级别没有区别
    runlevel5.target和graphical.target       //图形级别
    runlevel6.target和reboot.target          //重启
Device unit     //文件扩展名为.device,用于定义内核识别的设备
Mount unit      //文件扩展名为.mount,用于定义文件系统挂载点
Socket unit     //文件扩展名为.socket,用于标识进程间通信用的socket文件
Snapshot unit   //文件扩展名为.snapshot,用于管理系统快照
Swap unit       //文件扩展名为.swap,用于标识swap设备
Automount unit  //文件扩展名为.automount,用于实现文件系统的自动挂载点
Path unit       //文件扩展名为.path,用于定义文件系统中的一个文件或目录

ユニットの主な特長

//基于socket的激活机制:
    socket与服务程序分离,当有人去访问时才会真正启动服务,以此来实现按需激活进程与服务的并行启动
//基于bus的激活机制:
    所有使用dbus实现进程间通信的服务,可以在第一次被访问时按需激活
//基于device的激活机制:
    支持基于device激活的系统服务,可以在特定类型的硬件接入到系统中时,按需激活其所需要用到的服务
//基于path的激活机制:
    某个文件路径变得可用,或里面出现新文件时就激活某服务
//系统快照:
    保存各unit的当前状态信息于持久存储设备中,必要时能自动载入
//向后兼容sysv init脚本

互換性のない特性

//systemctl命令固定不变
//非由systemd启动的服务,systemctl无法与之通信
//只有已经启动的服务在级别切换时才会执行stop,在centos6以前是所有S开头的服务全部start,所有K开头的服务全部stop
//系统服务不会读取任何来自标准输入的数据流
//每个服务的unit操作均受5分钟超时时间限制

2. systemctl管理サービス

//语法:systemctl COMMAND name[.service|.target]
//常用COMMAND:
    start name.service      //启动服务
    stop name.service       //停止服务
    restart name.service    //重启服务
    status name.service     //查看服务状态
[root@localhost ~]# systemctl stop postfix.service
[root@localhost ~]# systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since 三 2019-09-25 15:11:46 CST; 23s ago
  Process: 2146 ExecStop=/usr/sbin/postfix stop (code=exited, status=0/SUCCESS)
  Process: 1177 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
  Process: 1165 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
  Process: 1097 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
 Main PID: 1321 (code=killed, signal=TERM)

9月 25 10:06:41 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 10:06:43 localhost.localdomain postfix/postfix-script[1319]: starting the Postfix mail system
9月 25 10:06:43 localhost.localdomain postfix/master[1321]: daemon started -- version 2.10.1, configuration ...fix
9月 25 10:06:43 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
9月 25 15:11:46 localhost.localdomain systemd[1]: Stopping Postfix Mail Transport Agent...
9月 25 15:11:46 localhost.localdomain systemd[1]: Stopped Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# systemctl start postfix.service
[root@localhost ~]# systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
   Active: active (running) since 三 2019-09-25 15:12:51 CST; 9s ago
  Process: 2146 ExecStop=/usr/sbin/postfix stop (code=exited, status=0/SUCCESS)
  Process: 2168 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
  Process: 2165 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
  Process: 2163 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
 Main PID: 2240 (master)
   CGroup: /system.slice/postfix.service
           ├─2240 /usr/libexec/postfix/master -w
           ├─2241 pickup -l -t unix -u
           └─2242 qmgr -l -t unix -u

9月 25 15:12:51 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 15:12:51 localhost.localdomain postfix/master[2240]: daemon started -- version 2.10.1, configuration ...fix
9月 25 15:12:51 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# systemctl restart postfix.service
[root@localhost ~]# systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
   Active: active (running) since 三 2019-09-25 15:14:20 CST; 16s ago
  Process: 2250 ExecStop=/usr/sbin/postfix stop (code=exited, status=0/SUCCESS)
  Process: 2264 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
  Process: 2262 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
  Process: 2259 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
 Main PID: 2336 (master)
   CGroup: /system.slice/postfix.service
           ├─2336 /usr/libexec/postfix/master -w
           ├─2337 pickup -l -t unix -u
           └─2338 qmgr -l -t unix -u

9月 25 15:14:20 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 15:14:20 localhost.localdomain postfix/master[2336]: daemon started -- version 2.10.1, configuration ...fix
9月 25 15:14:20 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
 try-restart name.service            //条件式重启服务,若服务已经启动则重启,若服务未启动则不做任何操作
[root@localhost ~]# systemctl stop postfix.service
[root@localhost ~]# systemctl try-restart postfix.service
[root@localhost ~]# systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since 三 2019-09-25 15:17:46 CST; 42s ago
  Process: 2346 ExecStop=/usr/sbin/postfix stop (code=exited, status=0/SUCCESS)
  Process: 2264 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
  Process: 2262 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
  Process: 2259 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
 Main PID: 2336 (code=killed, signal=TERM)

9月 25 15:14:20 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 15:14:20 localhost.localdomain postfix/master[2336]: daemon started -- version 2.10.1, configuration ...fix
9月 25 15:14:20 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
9月 25 15:17:46 localhost.localdomain systemd[1]: Stopping Postfix Mail Transport Agent...
9月 25 15:17:46 localhost.localdomain systemd[1]: Stopped Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
reload-or-restart name.service      //重载或重启服务,能reload则reload,否则restart
    reload-or-try-restart name.service  //重载或条件式重启服务,能reload则reload,否则try-restart
mask name.service       //禁止设定为开机自启
    unmask name.service     //取消禁止设定为开机自启
[root@localhost ~]# systemctl mask postfix.service
Created symlink from /etc/systemd/system/postfix.service to /dev/null.
[root@localhost ~]# systemctl status postfix.service
● postfix.service
   Loaded: masked (/dev/null; bad)
   Active: inactive (dead) since 三 2019-09-25 15:17:46 CST; 3min 11s ago
 Main PID: 2336 (code=killed, signal=TERM)

9月 25 15:14:20 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 15:14:20 localhost.localdomain postfix/master[2336]: daemon started -- version 2.10.1, configuration ...fix
9月 25 15:14:20 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
9月 25 15:17:46 localhost.localdomain systemd[1]: Stopping Postfix Mail Transport Agent...
9月 25 15:17:46 localhost.localdomain systemd[1]: Stopped Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# systemctl unmask postfix.service
Removed symlink /etc/systemd/system/postfix.service.
[root@localhost ~]# systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since 三 2019-09-25 15:17:46 CST; 3min 27s ago
 Main PID: 2336 (code=killed, signal=TERM)

9月 25 15:14:20 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 15:14:20 localhost.localdomain postfix/master[2336]: daemon started -- version 2.10.1, configuration ...fix
9月 25 15:14:20 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
9月 25 15:17:46 localhost.localdomain systemd[1]: Stopping Postfix Mail Transport Agent...
9月 25 15:17:46 localhost.localdomain systemd[1]: Stopped Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
list-dependencies name.service      //查看服务的依赖关系
    is-active name.service      //查看某服务当前激活与否的状态
    is-enable name.service      //查看服务是否开机自动启动
[root@localhost ~]# systemctl list-dependencies postfix.service
postfix.service
● ├─system.slice
● └─basic.target
● ├─microcode.service
● ├─rhel-autorelabel-mark.service
● ├─rhel-autorelabel.service
● ├─rhel-configure.service
● ├─rhel-dmesg.service
● ├─rhel-loadmodules.service
● ├─[email protected]
● ├─paths.target
● ├─slices.target
● │ ├─-.slice
● │ └─system.slice
● ├─sockets.target
● │ ├─dbus.socket
● │ ├─dm-event.socket
● │ ├─systemd-initctl.socket
● │ ├─systemd-journald.socket
● │ ├─systemd-shutdownd.socket
● │ ├─systemd-udevd-control.socket
● │ └─systemd-udevd-kernel.socket
● ├─sysinit.target
● │ ├─dev-hugepages.mount
● │ ├─dev-mqueue.mount
● │ ├─kmod-static-nodes.service
● │ ├─lvm2-lvmetad.socket
● │ ├─lvm2-lvmpolld.socket
● │ ├─lvm2-monitor.service
● │ ├─plymouth-read-write.service
lines 2-30
[root@localhost ~]# systemctl is-active postfix.service
active
[root@localhost ~]# systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
   Active: active (running) since 三 2019-09-25 15:22:42 CST; 4min 22s ago
  Process: 2413 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
  Process: 2411 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
  Process: 2408 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
 Main PID: 2485 (master)
   CGroup: /system.slice/postfix.service
           ├─2485 /usr/libexec/postfix/master -w
           ├─2486 pickup -l -t unix -u
           └─2487 qmgr -l -t unix -u

9月 25 15:22:41 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 15:22:42 localhost.localdomain postfix/master[2485]: daemon started -- version 2.10.1, configuration ...fix
9月 25 15:22:42 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
enable name.service     //设定某服务开机自动启动
    disable name.service    //禁止服务开机自动启动
[root@localhost ~]# systemctl enable postfix.service
[root@localhost ~]# systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
   Active: active (running) since 三 2019-09-25 15:22:42 CST; 8min ago
 Main PID: 2485 (master)
   CGroup: /system.slice/postfix.service
           ├─2485 /usr/libexec/postfix/master -w
           ├─2486 pickup -l -t unix -u
           └─2487 qmgr -l -t unix -u

9月 25 15:22:41 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 15:22:42 localhost.localdomain postfix/master[2485]: daemon started -- version 2.10.1, configuration ...fix
9月 25 15:22:42 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# systemctl disable postfix.service
Removed symlink /etc/systemd/system/multi-user.target.wants/postfix.service.
[root@localhost ~]# systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; disabled; vendor preset: disabled)
   Active: active (running) since 三 2019-09-25 15:22:42 CST; 10min ago
 Main PID: 2485 (master)
   CGroup: /system.slice/postfix.service
           ├─2485 /usr/libexec/postfix/master -w
           ├─2486 pickup -l -t unix -u
           └─2487 qmgr -l -t unix -u

9月 25 15:22:41 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
9月 25 15:22:42 localhost.localdomain postfix/master[2485]: daemon started -- version 2.10.1, configuration ...fix
9月 25 15:22:42 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
 isolate name.target     //切换至某级别,如systemctl isolate graphical.target就是切换至图形界面
    list-unit-files --type service      //查看所有服务的开机自动启动状态(是否开机自启)
    list-units --type service           //查看所有已经激活的服务状态信息
    list-units --type target            //查看所有已装载的级别
    list-units --type service --all     //查看所有服务(已启动/已停止)的状态信息
    list -units --type target --all     //查看所有的级别
[root@localhost ~]# systemctl list-unit-files --type service
UNIT FILE STATE   
arp-ethers.service disabled
auditd.service enabled 
[email protected] enabled 
blk-availability.service disabled
brandbot.service static  
[email protected] static  
chrony-wait.service disabled
chronyd.service enabled 
console-getty.service disabled
console-shell.service disabled
[email protected] static  
cpupower.service disabled
crond.service enabled 
dbus-org.fedoraproject.FirewallD1.service enabled 
dbus-org.freedesktop.hostname1.service static  
dbus-org.freedesktop.import1.service static  
dbus-org.freedesktop.locale1.service static  
dbus-org.freedesktop.login1.service static  
dbus-org.freedesktop.machine1.service static  
dbus-org.freedesktop.NetworkManager.service enabled 
dbus-org.freedesktop.nm-dispatcher.service enabled 
dbus-org.freedesktop.timedate1.service static  
dbus.service static  
debug-shell.service disabled
dm-event.service disabled
dracut-cmdline.service static  
dracut-initqueue.service static  
dracut-mount.service static  
get-default     //查看默认运行级别
    set-default name.target     //设置默认运行级别
[root@localhost ~]# systemctl get-default
multi-user.target
[root@localhost ~]# systemctl set-default multi-user.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.
    rescue      //切换至紧急救援模式(大多数服务不启动,但是会加载驱动)
    emergency   //切换至emergency模式(驱动不会加载,系统不会初始化,服务不会启动)
    halt        //关机
    poweroff    //关机
    reboot      //重启
    suspend     //挂起系统,此时不能关机,否则无用
    hibernate   //创建并保存系统快照,下次系统重启时会自动载入快照
    hybrid-sleep    //混合睡眠,快照并挂起

おすすめ

転載: www.cnblogs.com/liping0826/p/11585105.html