【背景】
有同事在研究mongo的服務啟動方式,討論到mysql5.7的服務管理時一起做了下面測試。
MySQL5.7是用systemd來管理service的,它的配置文件/usr/lib/systemd/system/mysqld@.service中,只定義了ExecStart啟動服務器的命令,
但沒有定義ExecStop參數,就是停止mysqld服務時執行的命令。systemd究竟是如何stop mysql服務的。
【測試步驟】
1、執行關閉服務的命令
systemctl stop mysqld@replica02.service
2、使用systemtap腳本來抓取kill進程時的信號量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
global target;
global signal;
probe begin
{
printf(
"%-6s %-12s %-5s %-6s %6s\n"
,
"FROM"
,
"COMMAND"
,
"SIG"
,
"TO"
,
"RESULT"
);
}
probe nd_syscall.kill
{
target[tid()] = uint_arg(1);
signal[tid()] = uint_arg(2);
}
probe nd_syscall.kill.
return
{
if
(target[tid()] != 0) {
printf(
"%-6d %-12s %-5d %-6d %6d\n"
, pid(), execname(),
signal[tid()], target[tid()], int_arg(1));
delete target[tid()];
delete signal[tid()];
}
}
|
3、可以看到上面的mysqld進程127531被systemd進程kill掉了,先傳15,再傳18兩個信號量
相當於執行了kill -15 127531,kill -18 127531兩個命令
4、從文檔中也找到了說明,
KillSignal參數可以指定停止進程時使用的信號,默認值為15 SIGTERM ,另外systemd會緊跟此信號再發送一個18 SIGCONT信號,以確保殺死已掛起的進程。
KillSignal=Specifies which signal to use when killing a service. This controls the signal that is sent as first step of shutting down a unit (see above), and is usually followed by SIGKILL (see above and below). For a list of valid signals, see signal(7). Defaults to SIGTERM.
Note that, right after sending the signal specified in this setting, systemd will always send SIGCONT, to ensure that even suspended tasks can be terminated cleanly.