<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[[实践OK]Linux SIGTERM 捕获]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Unix/LinuxC技术]]></category>
<pubDate>Thu, 18 May 2023 08:11:30 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	背景：容器中用docker stop&nbsp;&nbsp;4d393592c6ec ，sameersbn/redmine:5.0.5 的容器会产生如下日志，docker logs 4d393592c6e&nbsp;&nbsp;#docker stop 4d393592c6ec&nbsp;&nbsp;会触发下面的退出,而不是臆想的主动退出了，是手工执行出现docker 退出。<br/><br/>2023-05-18 07:52:16,943 WARN received SIGTERM indicating exit request<br/>2023-05-18 07:52:16,943 INFO waiting for unicorn, cron, nginx to die<br/>2023-05-18 07:52:16,945 INFO stopped: nginx (exit status 0)<br/>2023-05-18 07:52:16,946 INFO stopped: cron (terminated by SIGTERM)<br/>2023-05-18 07:52:17,947 INFO stopped: unicorn (exit status 0)<br/><br/>是一个SIGTERM信号，kill -l 发现15) SIGTERM，编号15，才知道是我自己停止的，而不是长时间运行出现自动退出。这个很重要，因为容器的稳定性相当重要。<br/>下面就以这段C代码进行模拟容器的退出并发SIGTERM信号，如下：<br/>test.c<br/><textarea name="code" class="php" rows="15" cols="100">
#include &lt;stdio.h&gt;
#include &lt;signal.h&gt;

void handler(void);

int main(int argc, char ** argv)
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;sigset( SIGTERM, handler );//signal( SIGTERM, handler );
&nbsp;&nbsp;&nbsp;&nbsp;printf( &quot;Process_pid=[%d]&#92;n&quot;, getpid() );
&nbsp;&nbsp;&nbsp;&nbsp;while(1);
&nbsp;&nbsp;&nbsp;&nbsp;return 0;
&#125;

void handler(void)
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Get a SIGTERM signal!&#92;n&quot;); 
&#125;
</textarea><br/><br/>send.c 用于发送SIGTERM 信号给test<br/><textarea name="code" class="php" rows="15" cols="100">
#include &lt;stdio.h&gt;
#include &lt;signal.h&gt;

int main( int argc, char ** argv )
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;if( argc != 2 )
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf( &quot;Usage: ./send process_pid&#92;n&quot; );
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return -1;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;printf( &quot;You will send a signal to the process=[%s]!&#92;n&quot;, argv[1] );
&nbsp;&nbsp;&nbsp;&nbsp;kill( atoi(argv[1]), SIGTERM );
&nbsp;&nbsp;&nbsp;&nbsp;printf( &quot;Send over!&#92;n&quot; );
&nbsp;&nbsp;&nbsp;&nbsp;return 0;
&#125;
</textarea><br/><br/>SIGTERM ： 程序结束信号，与SIGKILL不同的是该信号可以被阻塞和处理。通常用来要求程序自己正常退出。Shell命令kill 默认产生这个信号。<br/>gcc&nbsp;&nbsp;test.c -o test<br/>gcc&nbsp;&nbsp;send.c -o send<br/>运行程序:<br/>./test<br/><br/>./send 12345&nbsp;&nbsp;//12345是test的pid<br/><br/><textarea name="code" class="php" rows="15" cols="100">
[root@release2_server_bj_yz_10_10_0_248:/usr/local/src/SIGTERM]
#./test
Process_pid=[13843]


ps -ef&#124;grep test
root&nbsp;&nbsp;&nbsp;&nbsp; 13843 13734 98 16:04 pts/0&nbsp;&nbsp;&nbsp;&nbsp;00:00:55 ./test

./send 13843&nbsp;&nbsp;&nbsp;&nbsp; #进程PID编号
#./send 13843
You will send a signal to the process=[13843]!
Send over!


test进程输出：
[root@release2_server_bj_yz_10_10_0_248:/usr/local/src/SIGTERM]
#./test
Process_pid=[13843]
Get a SIGTERM signal!

</textarea><br/><br/>终端下输入：<br/>kill 12345<br/>也会给test发送SIGTERM 信号。<br/>or kill -15 12345<br/>kill -SIGTERM 12345<br/><br/><br/><br/>来自：<a href="https://blog.csdn.net/a379039233/article/details/80715461" target="_blank">https://blog.csdn.net/a379039233/article/details/80715461</a><br/>More signal：<br/>#kill -l<br/> 1) SIGHUP&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2) SIGINT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3) SIGQUIT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4) SIGILL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5) SIGTRAP<br/> 6) SIGABRT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7) SIGBUS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 8) SIGFPE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9) SIGKILL&nbsp;&nbsp;&nbsp;&nbsp; 10) SIGUSR1<br/>11) SIGSEGV&nbsp;&nbsp;&nbsp;&nbsp; 12) SIGUSR2&nbsp;&nbsp;&nbsp;&nbsp; 13) SIGPIPE&nbsp;&nbsp;&nbsp;&nbsp; 14) SIGALRM&nbsp;&nbsp;&nbsp;&nbsp; 15) SIGTERM<br/>16) SIGSTKFLT&nbsp;&nbsp; 17) SIGCHLD&nbsp;&nbsp;&nbsp;&nbsp; 18) SIGCONT&nbsp;&nbsp;&nbsp;&nbsp; 19) SIGSTOP&nbsp;&nbsp;&nbsp;&nbsp; 20) SIGTSTP<br/>21) SIGTTIN&nbsp;&nbsp;&nbsp;&nbsp; 22) SIGTTOU&nbsp;&nbsp;&nbsp;&nbsp; 23) SIGURG&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;24) SIGXCPU&nbsp;&nbsp;&nbsp;&nbsp; 25) SIGXFSZ<br/>26) SIGVTALRM&nbsp;&nbsp; 27) SIGPROF&nbsp;&nbsp;&nbsp;&nbsp; 28) SIGWINCH&nbsp;&nbsp;&nbsp;&nbsp;29) SIGIO&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 30) SIGPWR<br/>31) SIGSYS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;34) SIGRTMIN&nbsp;&nbsp;&nbsp;&nbsp;35) SIGRTMIN+1&nbsp;&nbsp;36) SIGRTMIN+2&nbsp;&nbsp;37) SIGRTMIN+3<br/>38) SIGRTMIN+4&nbsp;&nbsp;39) SIGRTMIN+5&nbsp;&nbsp;40) SIGRTMIN+6&nbsp;&nbsp;41) SIGRTMIN+7&nbsp;&nbsp;42) SIGRTMIN+8<br/>43) SIGRTMIN+9&nbsp;&nbsp;44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13<br/>48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12<br/>53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9&nbsp;&nbsp;56) SIGRTMAX-8&nbsp;&nbsp;57) SIGRTMAX-7<br/>58) SIGRTMAX-6&nbsp;&nbsp;59) SIGRTMAX-5&nbsp;&nbsp;60) SIGRTMAX-4&nbsp;&nbsp;61) SIGRTMAX-3&nbsp;&nbsp;62) SIGRTMAX-2<br/>63) SIGRTMAX-1&nbsp;&nbsp;64) SIGRTMAX<br/>
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [实践OK]Linux SIGTERM 捕获]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>