about summary refs log tree commit diff
path: root/doc/trap.html
blob: 4a5a66b66d654c9cd6cf335e1938e4af1a4ff7e3 (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
<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="http://skarnet.org/default.css" /> -->
</head>
<body>

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

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

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

<h2> Interface </h2>

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

<pre>
     trap [ -x ] [ -t <em>millisecs</em> ]
     {
       [ timeout { <em>progtimeout...</em> } ]
       [ SIGTERM { <em>progsigterm...</em> } ]
       [ quit { <em>progsigquit...</em> } ]
       [ 1 { <em>progsighup</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>timeout</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 trap for <tt>timeout</tt> will be triggered when <em>millisecs</em>
milliseconds elapse without a signal being caught. </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. </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 be still be caught by <tt>trap</tt>, that will send it to
<em>prog</em>. </li>
 <li> <tt>-t&nbsp;<em>millisecs</em></tt>&nbsp;: if a <tt>timeout</tt>
directive exists, the corresponding <em>progtimeout...</em> will be run
every time <em>millisecs</em> milliseconds elapse without <tt>trap</tt>
receiving a signal. By default, <em>millisecs</em> is 0, which means
infinite (i.e. a <tt>timeout</tt> directive will never trigger). </li>
</ul>

<h2> Notes </h2>

<ul>
 <li> Programs defined in command line directives can start with
<tt><a href="import.html">import</a> !</tt> to retrieve the pid of
<em>prog</em> in <tt>${!}</tt> </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>