[Posts] systemd comparison command centos7

centos7 of systemd command comparison

 

 

https://www.cnblogs.com/MYSQLZOUQI/p/8351825.html

 

http://www.linuxidc.com/Linux/2014-09/106490p2.htm
http://www.linuxidc.com/Linux/2016-09/135464p2.htm
http://www.linuxidc.com/Linux/2014-09/106455.htm

http://www.linuxidc.com/Linux/2015-04/115937.htm

 


systemd core concepts: unit
unit identified, identify and configure its associated configuration file, that is the definition of a unit in the end or not, is identified by its configuration file. Such a configuration file contains several main categories: System service, listening socket, saving snapshots and other related information init, these profiles are primarily stored in:
/ usr / lib / systemd / System / each service the main startup script settings, /etc/init.d/ previous similar  
services during the execution of the script / run / systemd / system / system produced, than the above directories first run
/ etc / systemd / system / administrator to create the execution of the script, similar to the function /etc/rc.d/rcN.d/Sxx class, run priority than the above directory

/etc/rc.d/rc3.d/ |grep mysql
lrwxrwxrwx  1 root root 16 Dec 22  2016 /etc/rc.d/rc3.d/K36mysqld -> /etc/init.d/mysqld


relationship

/etc/systemd/system/K36mysqld -> /usr/lib/systemd/system/mysqld



Common types of unit:
Service unit: this type of unit of the file extension .service, mainly used to define system services (which plays a role in the /etc/init.d/ directory service script on centos6)
target unit: such unit file extension .target, mainly used to simulate achieve "operational level" concept
device unit: unit type of file extension .device, used to define the core recognition device, and then use systemd udev hardware identification complete the creation of the device file name
mount unit: unit type of file extension .mount, mainly used to define the file system mount point
socket unit: unit type of file extension .socket, for inter-process communication used to identify socket file
snapshot unit: unit type of file extension .snapshot, mainly used to implement management system snapshots
swap unit: unit type of file extension .swap, mainly for identity management swap device
automount unit: unit type of file extension named .automount, mainly for automatically mount the file system device
path unit: unit file extension such .path, mainly for file system file definitions Catalog



systemd unit of the management of the target class (similar to the run-level management)
unit configuration file: .target
run level of correspondence between:
0 -> runlevel0.target, poweroff.target
1 -> runlevel1.target, rescue.target
2 --> runlevel2.target,multi-user.target
3 --> runlevel3.target,multi-user.target
4 --> runlevel4.target,multi-user.target
5 --> runlevel5.target,graphical.target
6 --> runlevel6.target,reboot.target



systemd how to manage the various services on the system
systemd management services (service class management unit)
relies mainly on the service type of file control unit completed
systemctl command:
Syntax: systemctl [OPTIONS ...] COMMAND [ SERVICE_NAME.service ...]
centos7 state loaded inside information in the display indicates whether or not added to the list systemctl can be managed (-add concept similar centos6 in chkconfig, if it is loaded, said that it has added to the list, behind that there is no disabled at startup)



View all service, which is already loaded load of all services, including active and inactive:
chkconfig --list ==> systemctl List-service Units -t -a
displays the meaning of the information:
loaded: Unit configuration file has been processed
active (running): one or more continuous processing operation
active (exited): the successful completion of a one-time configuration
active (waiting): running, waiting for an event
inactive: not running
enabled: boot
disabled: boot does not start
static: boot It does not start, but can be enabled by another service activation


centos7- "centos6
service start up

systemctl start servicename.service  -> service  servicename start

Stop Service

systemctl stop servicename.service  -> service  servicename stop

Service Restart

systemctl restart servicename.service  -> service  servicename restart

service status

systemctl status servicename.service  -> service  servicename status

Service overloaded

systemctl reload-or-restart servicename.service  -> /bin/kill -HUP  servicename

Service details, see only enabled services, is not fully equivalent to chkconfig --list

systemctl list-units -t service  -a  |grep servicename  -> chkconfig --list |grep servicename

Set boot / disabled

systemctl enable/disable servicename.service  -> chkconfig  servicename on/off

View service dependencies

systemctl list-dependencies servicename.service -> 无

View disabled services

systemctl is-enabled  servicename.service

Add services, not exactly equivalent to chkconfig --add

systemctl daemon-reload    -> chkconfig --add  servicename

 


Run level switch

systemctl isolate name.target -> init operation corresponds to the digital level

View the current run level

systemctl list-units -t target  -> runlevel

View all loaded in the target

systemctl list-units -t target  -a -> 无

Review the default run level

systemctl get-default - defined> View / etc / inittab file

Set the default run level

systemctl set-default name.target -> Modify the definition of / etc / inittab file

 

System boot time analysis

systemd-analyze blame

 















centos7 new emergency mode and rescue mode

centos7 startup items change
startup by modifying the relevant kernel parameters grub options, enter a different mode (emergency mode, emergency mode) the realization of starting the system
at startup, start menu items Press e, then added after linux16 line :( set kernel parameters, affects only start when times)
press ctrl-x start


systemd version
CAT / etc / RedHat-release 
CentOS Linux release 7.3.1611 (core) 

systemctl --version
systemd 219
+ PAM + SELINUX the AUDIT + + IMA - APPARMOR + SMACK + SYSVINIT + UTMP + LIBCRYPTSETUP + GCRYPT + GNUTLS + ACL + XZ -LZ4 -SECCOMP + BLKID + eLFUTILS + KMOD + IDN

 


f

f

 


Composition service unit file documents

如利用systemctl set-default runlevel5.target 修改默认运行级别后,原来/etc/systemd/system/default.target会重新链接到/usr/lib/systemd/system/graphical.target

service unit file文件的组成:
文件通常由三部分组成:
[Unit]:定义与Unit类型无关的通用选项,用于提供unit的描述信息,unit行为及依赖关系等;
[Service]:与特定类型相关的专用选项,此处为service类型
[Install]:定义由“systemctl enable”以及“systemctl disable”命令在实现服务启用或仅用时用到的一些选项;

unit段的常用选项:
Description:描述信息,意义性描述
After:定义unit启动次序,表示当前unit应该晚于哪些unit启动,其功能与Before相反
Requies:依赖到的其他units;强依赖,被依赖的unit无法激活时,当前unit也无法激活
Wants:指明依赖到的其他units;弱依赖,被依赖的unit无法激活时,当前unit可以被激活
Conflicts:定义units间的冲突关系


service段的常用选项:
Type:用于定义ExecStart及相关参数的功能的unit进程启动类型;
类型:
simple:默认值,表示由ExecStart启动的进程为主进程
forking:表示由ExecStart启动的进程生成的其中一个子进程将成为主进程,启动完成后,父进程会退出
oneshot:功能类似于simple,但是在启动后续的units进程之前,主进程将会退出
notify:类似于simple,表示后续的units,仅在通过sdnotify函数发送通知以后,才能运行该命令

idle:
常见的是notify、forking、simple
EnvironmentFile :指明环境配置文件,为真正ExecStart执行之前提供环境配置或变量定义等文件
ExecStart:指明启动unit要运行的命令或脚本;ExecStartPre、ExecStartPost表示启动前或启动后要执行的命令或脚本
ExecStop:指明停止unit要运行的命令或脚本
Restart:表示进程意外终止了,会自动重启

install段的常用选项:
Alias:当前unit的别名
RequiredBy:被那些units所依赖,强依赖
WantedBy:被那些units所依赖,弱依赖

 

复制代码
cat /usr/lib/systemd/system/sshd.service 
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service

[Service]
Type=forking
PIDFile=/var/run/sshd.pid
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target
复制代码

 


添加服务步骤

 

xx.service 模版
vim /usr/lib/systemd/system/xx.service   #改

复制代码
[Unit]
Description=xx Server #改
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
Type=forking
#PIDFile=/var/run/mysqld.pid
ExecStart=/usr/local/mysql/support-files/mysql.server start       #改 /etc/init.d/nginx
ExecStop=/usr/local/mysql/support-files/mysql.server stop     #改
PrivateTmp=true
复制代码

 

第一步:

vim /usr/lib/systemd/system/mysqld.service   

复制代码
[Unit]
Description=MySQL Server  
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
Type=forking
#PIDFile=/var/run/mysqld.pid
ExecStart=/usr/local/mysql/support-files/mysql.server start      
ExecStop=/usr/local/mysql/support-files/mysql.server stop     
PrivateTmp=true
复制代码

 

 

第二步注册

systemctl daemon-reload

第三步查看是否注册成功

systemctl is-enabled  mysqld.service

 

第四步设置开机启动

systemctl enable mysqld.service

 

第五步启动服务

systemctl start mysqld.service

 

 

------------------------------------------------------------

ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown

或者自己写一个脚本启动停止redis也可以
ExecStart=/tmp/redisinstall.sh  start
ExecStop=/tmp/redisinstall.sh  stop

 


 

记录 systemd 的日志
journald是systemd独有的日志系统,替换了sysV init中的syslog守护进程。命令journalctl用来读取日志。

 

查看引导信息
journalctl -b命令来查看所有引导日志。
journalctl -b


即时显示引导日志
实时显示系统日志(类似tail -f)。
journalctl -f

 

查看特定服务的日志
运用journalctl来查看你只想看到的服务或可执行程序的日志。
journalctl /usr/sbin/dnsmasq

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/12155933.html