<!DOCTYPE HTML> <html lang="ko" > <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <title>kill | Introduction</title> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <meta name="description" content=""> <meta name="generator" content="GitBook 2.6.7"> <meta name="HandheldFriendly" content="true"/> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png"> <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon"> <link rel="stylesheet" href="gitbook/style.css"> <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css"> <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-search/search.css"> <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css"> <link rel="stylesheet" href="./styles/website.css"> <link rel="next" href="./trap.html" /> <link rel="prev" href="./signals_and_traps.html" /> </head> <body> <div class="book" data-level="12.1" data-chapter-title="kill" data-filepath="kill.md" data-basepath="." data-innerlanguage=""> <div class="book-summary"> <nav role="navigation"> <ul class="summary"> <li class="chapter " data-level="0" data-path="index.html"> <a href="./index.html"> <i class="fa fa-check"></i> Introduction </a> </li> <li class="chapter " data-level="1" data-path="bug_reports.html"> <a href="./bug_reports.html"> <i class="fa fa-check"></i> <b>1.</b> Bug reports </a> </li> <li class="chapter " data-level="2" data-path="basics.html"> <a href="./basics.html"> <i class="fa fa-check"></i> <b>2.</b> Basics </a> <ul class="articles"> <li class="chapter " data-level="2.1" data-path="quotes.html"> <a href="./quotes.html"> <i class="fa fa-check"></i> <b>2.1.</b> Quotes </a> <ul class="articles"> <li class="chapter " data-level="2.1.1" data-path="escape_sequences.html"> <a href="./escape_sequences.html"> <i class="fa fa-check"></i> <b>2.1.1.</b> Escape Sequences </a> </li> </ul> </li> <li class="chapter " data-level="2.2" data-path="variables.html"> <a href="./variables.html"> <i class="fa fa-check"></i> <b>2.2.</b> Variables </a> </li> <li class="chapter " data-level="2.3" data-path="functions.html"> <a href="./functions.html"> <i class="fa fa-check"></i> <b>2.3.</b> Functions </a> </li> <li class="chapter " data-level="2.4" data-path="exit_status.html"> <a href="./exit_status.html"> <i class="fa fa-check"></i> <b>2.4.</b> Exit Status </a> </li> <li class="chapter " data-level="2.5" data-path="pattern_matching.html"> <a href="./pattern_matching.html"> <i class="fa fa-check"></i> <b>2.5.</b> Pattern Matching </a> </li> <li class="chapter " data-level="2.6" data-path="shebang.html"> <a href="./shebang.html"> <i class="fa fa-check"></i> <b>2.6.</b> #! </a> </li> <li class="chapter " data-level="2.7" data-path="interactive_non-interactive.html"> <a href="./interactive_non-interactive.html"> <i class="fa fa-check"></i> <b>2.7.</b> Interactive vs Non-Interactive Shell </a> </li> <li class="chapter " data-level="2.8" data-path="login_non-login.html"> <a href="./login_non-login.html"> <i class="fa fa-check"></i> <b>2.8.</b> Login vs Non-Login Shell </a> </li> <li class="chapter " data-level="2.9" data-path="etc.html"> <a href="./etc.html"> <i class="fa fa-check"></i> <b>2.9.</b> etc </a> </li> </ul> </li> <li class="chapter " data-level="3" data-path="arrays.html"> <a href="./arrays.html"> <i class="fa fa-check"></i> <b>3.</b> Arrays </a> </li> <li class="chapter " data-level="4" data-path="test.html"> <a href="./test.html"> <i class="fa fa-check"></i> <b>4.</b> Test </a> <ul class="articles"> <li class="chapter " data-level="4.1" data-path="test_operators.html"> <a href="./test_operators.html"> <i class="fa fa-check"></i> <b>4.1.</b> Operators </a> </li> </ul> </li> <li class="chapter " data-level="5" data-path="subshells.html"> <a href="./subshells.html"> <i class="fa fa-check"></i> <b>5.</b> Subshells </a> <ul class="articles"> <li class="chapter " data-level="5.1" data-path="process_creation.html"> <a href="./process_creation.html"> <i class="fa fa-check"></i> <b>5.1.</b> Process Creation </a> </li> </ul> </li> <li class="chapter " data-level="6" data-path="commands.html"> <a href="./commands.html"> <i class="fa fa-check"></i> <b>6.</b> Commands </a> <ul class="articles"> <li class="chapter " data-level="6.1" data-path="builtin_commands.html"> <a href="./builtin_commands.html"> <i class="fa fa-check"></i> <b>6.1.</b> Builtin Commands </a> <ul class="articles"> <li class="chapter " data-level="6.1.1" data-path="read.html"> <a href="./read.html"> <i class="fa fa-check"></i> <b>6.1.1.</b> read </a> </li> <li class="chapter " data-level="6.1.2" data-path="printf.html"> <a href="./printf.html"> <i class="fa fa-check"></i> <b>6.1.2.</b> printf </a> </li> <li class="chapter " data-level="6.1.3" data-path="eval.html"> <a href="./eval.html"> <i class="fa fa-check"></i> <b>6.1.3.</b> eval </a> </li> <li class="chapter " data-level="6.1.4" data-path="getopts.html"> <a href="./getopts.html"> <i class="fa fa-check"></i> <b>6.1.4.</b> getopts </a> </li> </ul> </li> <li class="chapter " data-level="6.2" data-path="keyword_commands.html"> <a href="./keyword_commands.html"> <i class="fa fa-check"></i> <b>6.2.</b> Keyword Commands </a> </li> <li class="chapter " data-level="6.3" data-path="compound_commands.html"> <a href="./compound_commands.html"> <i class="fa fa-check"></i> <b>6.3.</b> Compound Commands </a> </li> <li class="chapter " data-level="6.4" data-path="expr.html"> <a href="./expr.html"> <i class="fa fa-check"></i> <b>6.4.</b> expr </a> </li> <li class="chapter " data-level="6.5" data-path="grep.html"> <a href="./grep.html"> <i class="fa fa-check"></i> <b>6.5.</b> grep </a> </li> <li class="chapter " data-level="6.6" data-path="find.html"> <a href="./find.html"> <i class="fa fa-check"></i> <b>6.6.</b> find </a> </li> </ul> </li> <li class="chapter " data-level="7" data-path="special_expressions.html"> <a href="./special_expressions.html"> <i class="fa fa-check"></i> <b>7.</b> Special Expressions </a> </li> <li class="chapter " data-level="8" data-path="metacharacters.html"> <a href="./metacharacters.html"> <i class="fa fa-check"></i> <b>8.</b> Shell Metacharacters </a> <ul class="articles"> <li class="chapter " data-level="8.1" data-path="precedence.html"> <a href="./precedence.html"> <i class="fa fa-check"></i> <b>8.1.</b> Precedence </a> </li> </ul> </li> <li class="chapter " data-level="9" data-path="expansions_and_substitutions.html"> <a href="./expansions_and_substitutions.html"> <i class="fa fa-check"></i> <b>9.</b> Expansions and Substitutions </a> <ul class="articles"> <li class="chapter " data-level="9.1" data-path="exp_and_sub/brace_expansion.html"> <a href="./exp_and_sub/brace_expansion.html"> <i class="fa fa-check"></i> <b>9.1.</b> Brace Expansion </a> </li> <li class="chapter " data-level="9.2" data-path="exp_and_sub/tilde_expansion.html"> <a href="./exp_and_sub/tilde_expansion.html"> <i class="fa fa-check"></i> <b>9.2.</b> Tilde Expansion </a> </li> <li class="chapter " data-level="9.3" data-path="exp_and_sub/parameter_expansion.html"> <a href="./exp_and_sub/parameter_expansion.html"> <i class="fa fa-check"></i> <b>9.3.</b> Parameter Expansion </a> </li> <li class="chapter " data-level="9.4" data-path="exp_and_sub/arithmetic_expansion.html"> <a href="./exp_and_sub/arithmetic_expansion.html"> <i class="fa fa-check"></i> <b>9.4.</b> Arithmetic Expansion </a> </li> <li class="chapter " data-level="9.5" data-path="exp_and_sub/command_substitution.html"> <a href="./exp_and_sub/command_substitution.html"> <i class="fa fa-check"></i> <b>9.5.</b> Command Substitution </a> </li> <li class="chapter " data-level="9.6" data-path="exp_and_sub/process_substitution.html"> <a href="./exp_and_sub/process_substitution.html"> <i class="fa fa-check"></i> <b>9.6.</b> Process Substitution </a> </li> <li class="chapter " data-level="9.7" data-path="exp_and_sub/word_splitting.html"> <a href="./exp_and_sub/word_splitting.html"> <i class="fa fa-check"></i> <b>9.7.</b> Word Splitting </a> </li> <li class="chapter " data-level="9.8" data-path="exp_and_sub/filename_expansion.html"> <a href="./exp_and_sub/filename_expansion.html"> <i class="fa fa-check"></i> <b>9.8.</b> Filename Expansion </a> </li> </ul> </li> <li class="chapter " data-level="10" data-path="redirections.html"> <a href="./redirections.html"> <i class="fa fa-check"></i> <b>10.</b> Redirections </a> <ul class="articles"> <li class="chapter " data-level="10.1" data-path="file_descriptors.html"> <a href="./file_descriptors.html"> <i class="fa fa-check"></i> <b>10.1.</b> File Descriptors </a> </li> <li class="chapter " data-level="10.2" data-path="here_document.html"> <a href="./here_document.html"> <i class="fa fa-check"></i> <b>10.2.</b> << , <<< </a> </li> <li class="chapter " data-level="10.3" data-path="pipe.html"> <a href="./pipe.html"> <i class="fa fa-check"></i> <b>10.3.</b> Pipe </a> </li> <li class="chapter " data-level="10.4" data-path="named_pipe.html"> <a href="./named_pipe.html"> <i class="fa fa-check"></i> <b>10.4.</b> Named Pipe </a> </li> <li class="chapter " data-level="10.5" data-path="buffering.html"> <a href="./buffering.html"> <i class="fa fa-check"></i> <b>10.5.</b> Buffering </a> </li> </ul> </li> <li class="chapter " data-level="11" data-path="job_control.html"> <a href="./job_control.html"> <i class="fa fa-check"></i> <b>11.</b> Job Control </a> <ul class="articles"> <li class="chapter " data-level="11.1" data-path="session_and_process-group.html"> <a href="./session_and_process-group.html"> <i class="fa fa-check"></i> <b>11.1.</b> Session and Process Group </a> </li> <li class="chapter " data-level="11.2" data-path="process_state_codes.html"> <a href="./process_state_codes.html"> <i class="fa fa-check"></i> <b>11.2.</b> Process State Codes </a> </li> <li class="chapter " data-level="11.3" data-path="tty.html"> <a href="./tty.html"> <i class="fa fa-check"></i> <b>11.3.</b> TTY </a> </li> <li class="chapter " data-level="11.4" data-path="mutual_exclusion.html"> <a href="./mutual_exclusion.html"> <i class="fa fa-check"></i> <b>11.4.</b> Mutual Exclusion </a> </li> </ul> </li> <li class="chapter " data-level="12" data-path="signals_and_traps.html"> <a href="./signals_and_traps.html"> <i class="fa fa-check"></i> <b>12.</b> Signals and Traps </a> <ul class="articles"> <li class="chapter active" data-level="12.1" data-path="kill.html"> <a href="./kill.html"> <i class="fa fa-check"></i> <b>12.1.</b> kill </a> </li> <li class="chapter " data-level="12.2" data-path="trap.html"> <a href="./trap.html"> <i class="fa fa-check"></i> <b>12.2.</b> trap </a> </li> <li class="chapter " data-level="12.3" data-path="signals_table.html"> <a href="./signals_table.html"> <i class="fa fa-check"></i> <b>12.3.</b> Signals Table </a> </li> </ul> </li> <li class="chapter " data-level="13" data-path="shell_options.html"> <a href="./shell_options.html"> <i class="fa fa-check"></i> <b>13.</b> Shell Options </a> </li> <li class="chapter " data-level="14" data-path="shell_variables.html"> <a href="./shell_variables.html"> <i class="fa fa-check"></i> <b>14.</b> Shell Variables </a> <ul class="articles"> <li class="chapter " data-level="14.1" data-path="positional_parameters.html"> <a href="./positional_parameters.html"> <i class="fa fa-check"></i> <b>14.1.</b> Positional Parameters </a> </li> <li class="chapter " data-level="14.2" data-path="special_parameters.html"> <a href="./special_parameters.html"> <i class="fa fa-check"></i> <b>14.2.</b> Special Parameters </a> </li> </ul> </li> <li class="chapter " data-level="15" data-path="command_aliases.html"> <a href="./command_aliases.html"> <i class="fa fa-check"></i> <b>15.</b> Command Aliases </a> </li> <li class="chapter " data-level="16" data-path="command_history.html"> <a href="./command_history.html"> <i class="fa fa-check"></i> <b>16.</b> Command History </a> </li> <li class="chapter " data-level="17" data-path="command_completion.html"> <a href="./command_completion.html"> <i class="fa fa-check"></i> <b>17.</b> Command Completion </a> </li> <li class="chapter " data-level="18" data-path="readline.html"> <a href="./readline.html"> <i class="fa fa-check"></i> <b>18.</b> Readline </a> </li> <li class="chapter " data-level="19" data-path="debugging.html"> <a href="./debugging.html"> <i class="fa fa-check"></i> <b>19.</b> Debugging </a> </li> <li class="chapter " data-level="20" data-path="sh.html"> <a href="./sh.html"> <i class="fa fa-check"></i> <b>20.</b> sh </a> </li> <li class="chapter " data-level="21" data-path="colors_and_prompt.html"> <a href="./colors_and_prompt.html"> <i class="fa fa-check"></i> <b>21.</b> Colors and Prompt </a> </li> <li class="chapter " data-level="22" data-path="tips.html"> <a href="./tips.html"> <i class="fa fa-check"></i> <b>22.</b> Tips </a> </li> <li class="chapter " data-level="23" data-path="recommand_sites.html"> <a href="./recommand_sites.html"> <i class="fa fa-check"></i> <b>23.</b> Recommand Sites </a> </li> <li class="chapter " data-level="24" data-path="closing.html"> <a href="./closing.html"> <i class="fa fa-check"></i> <b>24.</b> 맺음말 </a> </li> <li class="chapter " data-level="25" data-path="supplement.html"> <a href="./supplement.html"> <i class="fa fa-check"></i> <b>25.</b> 별책부록 </a> </li> <li class="divider"></li> <li> <a href="https://www.gitbook.com" target="blank" class="gitbook-link"> GitBook에 게시 </a> </li> </ul> </nav> </div> <div class="book-body"> <div class="body-inner"> <div class="book-header" role="navigation"> <!-- Actions Left --> <!-- Title --> <h1> <i class="fa fa-circle-o-notch fa-spin"></i> <a href="./" >Introduction</a> </h1> </div> <div class="page-wrapper" tabindex="-1" role="main"> <div class="page-inner"> <section class="normal" id="section-"> <h1 id="kill">kill</h1> <blockquote> <p>kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]</p> </blockquote> <p><a href="signals_table.html">Signals table</a> 을 보면 알 수 있겠지만 대부분의 신호들은 치명적인 오류를 나타내고 default action 으로 프로세스가 종료됩니다. 그러므로 왜 신호를 보내는 명령 이름이 kill 인지 짐작을 할 수 있습니다. kill 명령으로 신호를 보낼때 신호값을 주지 않으면 기본값 또한 TERM 신호가 됩니다.</p> <p>다음은 kill 명령 사용 예입니다. 신호명 앞에 SIG 를 제외하고 사용할 수 있고 소문자로 쓸 수도 있습니다. 또한 pid 대신에 jobspec 을 사용할 수 있습니다.</p> <pre><code class="lang-bash"><span class="hljs-built_in">kill</span> -TERM <span class="hljs-number">4287</span> <span class="hljs-built_in">kill</span> -SIGTERM <span class="hljs-number">4287</span> <span class="hljs-built_in">kill</span> <span class="hljs-operator">-s</span> TERM <span class="hljs-number">4287</span> <span class="hljs-built_in">kill</span> <span class="hljs-operator">-s</span> SIGTERM <span class="hljs-number">4287</span> <span class="hljs-built_in">kill</span> -<span class="hljs-number">15</span> <span class="hljs-number">4287</span> <span class="hljs-built_in">kill</span> -n <span class="hljs-number">15</span> <span class="hljs-number">4287</span> <span class="hljs-built_in">kill</span> <span class="hljs-number">4287</span> <span class="hljs-comment"># 신호값을 주지 않으면 기본값은 TERM 신호가 된다.</span> </code></pre> <h3 id="kill-신호값-pgid">kill 신호값 -pgid</h3> <p>kill 명령을 이용해 process group 에 신호를 보낼 수 있습니다.<br>이때는 pgid 앞에 <code>-</code> 문자를 붙여 사용합니다.</p> <pre><code class="lang-bash"><span class="hljs-built_in">kill</span> -TERM -<span class="hljs-number">4287</span> <span class="hljs-comment"># 다음과 같은 경우는 -4287 이 pgid 가 아니라 신호값을 나타내는 인수로</span> <span class="hljs-comment"># 인식되는 것을 방지하기 위해 -- 를 붙입니다.</span> <span class="hljs-comment"># 신호값을 주지 않았으므로 기본값인 TERM 신호가 됩니다.</span> <span class="hljs-built_in">kill</span> -- -<span class="hljs-number">4287</span> </code></pre> <h3 id="kill-0-pid">kill -0 pid</h3> <p><code>kill -0</code> 은 실질적으로 신호를 보내지 않지만 프로세스가 아직 살아있는지, 살아있다면 신호를 보낼수있는 권한이 되는지 테스트 하는데 사용합니다. </p> <pre><code class="lang-bash">$ <span class="hljs-built_in">kill</span> -<span class="hljs-number">0</span> <span class="hljs-number">32630</span>; <span class="hljs-built_in">echo</span> $? <span class="hljs-comment"># process 가 살아있고 신호를 보낼 수 있는 권한이 됨</span> <span class="hljs-number">0</span> $ <span class="hljs-built_in">kill</span> -<span class="hljs-number">0</span> <span class="hljs-number">32631</span>; <span class="hljs-built_in">echo</span> $? <span class="hljs-comment"># process 가 존재하지 않음</span> bash: <span class="hljs-built_in">kill</span>: (<span class="hljs-number">32631</span>) - No such process <span class="hljs-number">1</span> $ <span class="hljs-built_in">kill</span> -<span class="hljs-number">0</span> <span class="hljs-number">1</span>; <span class="hljs-built_in">echo</span> $? <span class="hljs-comment"># process 가 존재하나 신호를 보낼 수 있는 권한이 안됨</span> bash: <span class="hljs-built_in">kill</span>: (<span class="hljs-number">1</span>) - Operation not permitted <span class="hljs-number">1</span> </code></pre> <h3 id="kill-신호값-0">kill 신호값 0</h3> <p>스크립트 실행 중에 특정 상황에서 자기 자신을 종료해야 될 때가 있습니다. 그런데 background 프로세스가 생성되어 실행 중이거나 스크립트 내에서 a.sh -> b.sh -> c.sh 순서로 호출하여 실행중에 있을경우 단순히 exit 명령을 사용하거나 <code>kill -TERM $$</code> 을 하는 것으로는 c.sh 만 종료가 되고 a.sh, b.sh 와 background 프로세스는 실행을 계속하게 됩니다.</p> <p>이때 사용할수 있는 것이 <code>kill -TERM 0</code> 입니다. 여기서 <code>0</code> 은 프로세스 자신의 process group id 를 나타내는데 kill 명령은 shell 의 builtin 명령이므로 shell 과 같은 pgid 를 갖게 됩니다. 따라서 <code>kill -TERM 0</code> 은 c.sh 의 pgid 에 신호를 보내게 되고 결과적으로 같은 pgid 에 속하는 프로세스들이 모두 종료하게 됩니다.</p> <pre><code class="lang-bash"><span class="hljs-comment"># 스크립트 자신의 pgid 에 TERM 신호 보내기</span> <span class="hljs-built_in">kill</span> -TERM <span class="hljs-number">0</span> <span class="hljs-built_in">kill</span> <span class="hljs-number">0</span> ---------------------------------- <span class="hljs-comment"># 스크립트 자신의 pgid 에 INT 신호 보내기</span> <span class="hljs-built_in">kill</span> -INT <span class="hljs-number">0</span> </code></pre> </section> </div> </div> </div> <a href="./signals_and_traps.html" class="navigation navigation-prev " aria-label="Previous page: Signals and Traps"><i class="fa fa-angle-left"></i></a> <a href="./trap.html" class="navigation navigation-next " aria-label="Next page: trap"><i class="fa fa-angle-right"></i></a> </div> </div> <script src="gitbook/app.js"></script> <script src="gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script> <script src="gitbook/plugins/gitbook-plugin-search/search.js"></script> <script src="gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script> <script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script> <script> require(["gitbook"], function(gitbook) { var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}}; gitbook.start(config); }); </script> </body> </html>