about summary refs log tree commit diff
path: root/doc/trap.html
blob: 42ad4a717b37afed0f092002e2f5c72a188515f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<html>
 <head>
 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 <meta http-equiv="Content-Language" content="en" />
 <title>execline: the trap command</title>
 <meta name="Description" content="execline: the trap command" />
 <meta name="Keywords" content="execline command trap signal" />
 <!-- <link rel="stylesheet" type="text/css" href="//skarnet.org/default.css" /> -->
</head>
<body>

<p>
<a href="index.html">execline</a><br />
<a href="//skarnet.org/software/">Software</a><br />
<a href="//skarnet.org/">skarnet.org</a>
</p>

<h1> The <tt>trap</tt> program </h1>

<p>
<tt>trap</tt> traps signals and runs a variety of commands according
to the signals it catches.
</p>

<h2> Interface </h2>

<p>
 In an <a href="execlineb.html">execlineb</a> script:
</p>

<pre>
     trap [ -x ]
     {
       [ SIGTERM { <em>progsigterm...</em> } ]
       [ quit { <em>progsigquit...</em> } ]
       [ 1 { <em>progsighup</em>... } ]
       [ default { <em>progdefault</em>... } ]
       ...
     }
     <em>prog...</em>
</pre>

<ul>
 <li> <tt>trap</tt> reads a sequence of directives in a
<a href="el_semicolon.html">block</a>. It expects at least one
directive. </li>
 <li> Each directive is a keyword followed by a block. </li>
 <li> The keyword can be the special word <tt>default</tt>, a signal
name (case-insensitive, with or without the <tt>SIG</tt> prefix),
 or a signal number. The block following it is a command line to
run when the specified event occurs. </li>
 <li> <tt>trap</tt> sets traps for the various directives it reads.
A trap for <tt>SIGTERM</tt> will be triggered when the <tt>trap</tt>
program receives a SIGTERM. A <tt>default</tt>
trap will be used for any signal that is not caught by another trap. </li>
 <li> It spawns a child executing <em>prog...</em>. </li>
 <li> It sets the <tt>!</tt> environment
variable to the pid of the <em>prog...</em> process, and the <tt>SIGNAL</tt>
environment variable to the trapped signal. </li>
 <li> Whenever it catches a signal, it spawns the program described in the
corresponding directive. It will not spawn a program for the same signal
twice: if the first subprocess is still active when another instance of the
same signal arrives, this second instance is ignored. </li>
 <li> When <em>prog...</em> exits, <tt>trap</tt> exits with an
<a href="exitcodes.html">approximation</a> of the same exit code. </li>
</ul>

<h2> Options </h2>

<ul>
 <li> <tt>-x</tt>&nbsp;: forward signals. If this option is given,
any signal that <tt>trap</tt> receives and that is not explicitly
trapped will be sent to <em>prog</em>. By default, <tt>trap</tt> does
not forward any signals, and does not ignore them either - for instance a
SIGTERM, unless caught by a <tt>SIGTERM</tt> directive, will kill the
<tt>trap</tt> process (and leave <em>prog</em> running). With the
<tt>-x</tt> option, without a <tt>SIGTERM</tt> directive, a SIGTERM
will still be caught by <tt>trap</tt>, that will send it to
<em>prog</em>. Note that if a <tt>default</tt> directive is present,
this option does nothing. </li>
</ul>

<h2> Notes </h2>

<ul>
 <li> Programs defined in command line directives can start with
<code><a href="importas.html">importas</a> ! !</code> to retrieve the pid of
<em>prog</em> in <tt>$!</tt>. If they need the signal number, which
can be the case in <tt>default</tt> directives, they can for instance use
<code><a href="multisubstitute.html">multisubstitute</a> { importas ! ! importas SIGNAL SIGNAL }</code>
to get both <tt>$!</tt> and <tt>$SIGNAL</tt> substitutions. </li>
 <li> The <tt>-x</tt> option is basically a shortcut for a <code>default {
multisubstitute { importas ! ! importas SIGNAL SIGNAL } kill -$SIGNAL $! }</code> directive. </li>
 <li> <tt>trap</tt> is a standard shell builtin, with similar
functionality. It is more idiomatic, and probably more efficient,
to use that builtin in shell scripts, and to only use the
<tt>trap</tt> program in execline scripts. </li>
</ul>

</body>
</html>