Centos 7 和Centos 6 对比 命令管理这块做了很大的改变。

今天就聊聊 systemctl 命令。之前大家都是使用 service ,service 服务 指令 现在要换成 systemctl 指令 服务。

以前添加自启动 chkconfig 服务 on/off 现在要换成 systemctl 开启/关闭 服务

来介绍一下指令操作。

列出所有服务(包括启用的和禁用的)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
\# systemctl list-unit-files --type=service
UNIT FILE STATE
arp-ethers.service disabled
auditd.service enabled
[email protected] disabled
blk-availability.service disabled
brandbot.service static
collectd.service disabled
console-getty.service disabled
console-shell.service disabled
cpupower.service disabled
crond.service enabled
dbus-org.Fedoraproject.FirewallD1.service enabled
....

分析启动时各个进程花费的时间

1
2
3
4
5
6
7
8
9
10
11
12
\# systemd-analyze blame
8.565s mariadb.service
7.991s webmin.service
6.095s postfix.service
4.311s httpd.service
3.926s firewalld.service
3.780s kdump.service
3.238s tuned.service
1.712s network.service
1.394s lvm2-monitor.service
1.126s systemd-logind.service
....

列出所有失败单元

1
2
3
4
5
6
7
8
\# systemctl --failed
UNIT LOAD ACTIVE SUB DESCRIPTION
kdump.service loaded failed failed Crash recovery kernel arming
LOAD =Reflects whether the unit definition was properly loaded.
ACTIVE =The high-level unit activation state, i.e. generalization of SUB.
SUB =The low-level unit activation state, values depend on unit type.
1 loaded units listed.Pass--all to see loaded but inactive units, too.
To show all installed unit files use'systemctl list-unit-files'.

列出自启动服务

#systemctl list-dependencies

Linux中如何启动、重启、停止、重载服务以及检查服务(如 httpd.service)状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
\# systemctl start httpd.service
\# systemctl restart httpd.service
\# systemctl stop httpd.service
\# systemctl reload httpd.service
\# systemctl status httpd.service
httpd.service -TheApache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
Active: active (running) since Tue2015-04-2817:21:30 IST;6s ago
Process:2876ExecStop=/bin/kill -WINCH ${MAINPID}(code=exited, status=0/SUCCESS)
Main PID:2881(httpd)
Status:"Processing requests..."
CGroup:/system.slice/httpd.service
├─2881/usr/sbin/httpd -DFOREGROUND
├─2884/usr/sbin/httpd -DFOREGROUND
├─2885/usr/sbin/httpd -DFOREGROUND
├─2886/usr/sbin/httpd -DFOREGROUND
├─2887/usr/sbin/httpd -DFOREGROUND
└─2888/usr/sbin/httpd -DFOREGROUND
Apr2817:21:30 tecmint systemd\[1\]:StartingTheApache HTTP Server...
Apr2817:21:30 tecmint httpd\[2881\]: AH00558: httpd:Couldnot reliably determine the server's fully q...ssage
Apr 28 17:21:30 tecmint systemd\[1\]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

注意:当我们使用systemctl的start,restart,stop和reload命令时,我们不会从终端获取到任何输出内容,只有status命令可以打印输出。

检查某个单元或服务是否运行

1
2
3
4
5
6
7
8
9
10
\# systemctl status firewalld.service
firewalld.service - firewalld -dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled)
Active: active (running) since Tue2015-04-2816:27:55 IST;34min ago
Main PID:549(firewalld)
CGroup:/system.slice/firewalld.service
└─549/usr/bin/python -Es/usr/sbin/firewalld --nofork --nopid
Apr2816:27:51 tecmint systemd\[1\]:Starting firewalld -dynamic firewall daemon...
Apr2816:27:55 tecmint systemd\[1\]:Started firewalld -dynamic firewall daemon.
`

检查某个单元(如 cron.service)是否启用

1
2
\# systemctl is-enabled crond.service
enabled

如何激活服务并在启动时启用或禁用服务(即系统启动时自动启动服务)

1
2
3
\# systemctl is-active httpd.service
\# systemctl enable httpd.service
\# systemctl disable httpd.service

如何屏蔽(让它不能启动)或显示服务(如 httpd.service)

1
2
3
4
\# systemctl mask httpd.service
ln -s '/dev/null''/etc/systemd/system/httpd.service'
\# systemctl unmask httpd.service
rm '/etc/systemd/system/httpd.service'

使用systemctl命令杀死服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
\# systemctl kill httpd
\# systemctl status httpd
httpd.service -TheApache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
Active: failed (Result:exit-code) since Tue2015-04-2818:01:42 IST;28min ago
Main PID:2881(code=exited, status=0/SUCCESS)
Status:"Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
Apr2817:37:29 tecmint systemd\[1\]: httpd.service:Got notification message from PID 2881, but recepti...bled.
Apr2817:37:29 tecmint systemd\[1\]: httpd.service:Got notification message from PID 2881, but recepti...bled.
Apr2817:37:39 tecmint systemd\[1\]: httpd.service:Got notification message from PID 2881, but recepti...bled.
Apr2817:37:39 tecmint systemd\[1\]: httpd.service:Got notification message from PID 2881, but recepti...bled.
Apr2817:37:49 tecmint systemd\[1\]: httpd.service:Got notification message from PID 2881, but recepti...bled.
Apr2817:37:49 tecmint systemd\[1\]: httpd.service:Got notification message from PID 2881, but recepti...bled.
Apr2817:37:59 tecmint systemd\[1\]: httpd.service:Got notification message from PID 2881, but recepti...bled.
Apr2817:37:59 tecmint systemd\[1\]: httpd.service:Got notification message from PID 2881, but recepti...bled.
Apr2818:01:42 tecmint systemd\[1\]: httpd.service: control process exited, code=exited status=226
Apr2818:01:42 tecmint systemd\[1\]:Unit httpd.service entered failed state.
Hint:Some lines were ellipsized,use-l to show in full.

将某个服务(httpd.service)的CPU分配份额限制为2000 CPUShares/

1
# systemctl set-property httpd.service CPUShares=2000

检查某个服务的所有配置细节

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
\# systemctl show httpd
Id=httpd.service
Names=httpd.service
Requires=basic.target
Wants=system.slice
WantedBy=multi-user.target
Conflicts=shutdown.target
Before=shutdown.target multi-user.target
After=network.target remote-fs.target nss-lookup.target systemd-journald.socket basic.target system.slice
Description=TheApache HTTP Server
LoadState=loaded
ActiveState=active
SubState=running
FragmentPath=/usr/lib/systemd/system/httpd.service
....

获取某个服务(httpd)的依赖性列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
\# systemctl list-dependencies httpd.service
httpd.service
├─system.slice
└─basic.target
├─firewalld.service
├─microcode.service
├─rhel-autorelabel-mark.service
├─rhel-autorelabel.service
├─rhel-configure.service
├─rhel-dmesg.service
├─rhel-loadmodules.service
├─paths.target
├─slices.target
│├─-.slice
│└─system.slice
├─sockets.target
│├─dbus.socket
....

启动系统救援模式

1
2
3
\# systemctl rescue
Broadcast message from [email protected] on pts/0(Wed2015-04-2911:31:18 IST):
The system is going down to rescue mode NOW!

33. 进入紧急模式

1
2
3
4
\# systemctl emergency
Welcome to emergency mode!After logging in, type "journalctl -xb" to view
system logs,"systemctl reboot" to reboot,"systemctl default" to try again
to boot intodefault mode.

34. 列出当前使用的运行等级

1
2
\# systemctl get-default
multi-user.target