1nbdkit-exitwhen-filter(1) NBDKIT nbdkit-exitwhen-filter(1)
2
3
4
6 nbdkit-exitwhen-filter - exit gracefully when an event occurs
7
9 nbdkit --filter=exitwhen PLUGIN
10 [exit-when-file-created=FILENAME]
11 [exit-when-file-deleted=FILENAME]
12 [exit-when-pipe-closed=FD]
13 [exit-when-process-exits=PID]
14 [exit-when-script=SCRIPT]
15 [exit-when-poll=SECS]
16
18 "nbdkit-exitwhen-filter" is an nbdkit filter that causes nbdkit to exit
19 gracefully when some external event occurs. Built-in events are: a
20 file being created or deleted, a pipe being closed, or a process
21 exiting. You can also define custom events using an external script or
22 command.
23
24 After the event occurs nbdkit refuses new connections, waits for all
25 current clients to disconnect, and then exits.
26
27 A similar filter is nbdkit-exitlast-filter(1). For other ways to
28 ensure that nbdkit exits when you want see nbdkit-captive(1) and
29 nbdkit-service(1).
30
32 nbdkit --filter=exitwhen memory 1G exit-when-file-created=/tmp/stop
33
34 nbdkit will run normally until something creates /tmp/stop, whereupon
35 nbdkit will refuse new connections and exit as soon as the last client
36 has disconnected. If /tmp/stop exists before nbdkit starts, it will
37 exit immediately.
38
39 nbdkit --filter=exitwhen memory 1G exit-when-process-exits=1234
40
41 nbdkit will exit gracefully when PID 1234 exits and all connections
42 close. If you want to exit when the parent process of nbdkit exits,
43 consider using the --exit-with-parent flag instead.
44
46 You can define multiple "exit-when-*" events on the command line:
47 nbdkit will exit if any of the events happens. If there are no
48 "exit-when-*" events then the filter does nothing.
49
50 exit-when-file-created=FILENAME
51 exit-when-file-deleted=FILENAME
52 Exit when the named file is created or deleted.
53
54 exit-when-pipe-closed=FD
55 The read end of a pipe(2) is passed to nbdkit in the given file
56 descriptor number. Exit when the pipe is closed. The filter does
57 not read any data from the pipe.
58
59 For an example of how to use this parameter, see:
60 https://gitlab.com/nbdkit/nbdkit/-/blob/master/tests/test-exitwhen-pipe-closed.c
61
62 exit-when-process-exits=PID
63 Exit when process ID "PID" exits.
64
65 Note there is a small race between passing the process ID to the
66 filter and the filter checking it for the first time. During this
67 window the original PID might exit and an unrelated program might
68 get the same PID, thus holding nbdkit open for longer than wanted.
69 The pipe method above is more reliable if you are able to modify
70 the other process.
71
72 exit-when-script="SCRIPT"
73 Create a custom event using the script or command "SCRIPT". The
74 "SCRIPT" can be a program, shell script or a command with optional
75 parameters. Note if using a filename here: you may need to use the
76 absolute path because nbdkit changes directory when it daemonizes.
77
78 The script should exit with code 88 if the event is detected. The
79 filter does different things depending on the exit code of the
80 script:
81
82 0 The event has not been triggered, so nbdkit continues to
83 process requests as normal.
84
85 "1-87"
86 An error is logged, but the event is not triggered and nbdkit
87 continues to process requests as normal.
88
89 88 The event has been triggered. nbdkit will refuse new
90 connections and exit gracefully as soon as all current clients
91 disconnect.
92
93 "89-"
94 Exit codes 89 and above are reserved for future use. The
95 behaviour may change in future if scripts return any of these
96 exit codes.
97
98 exit-when-poll=SECS
99 When nbdkit is serving clients this filter does not need to poll
100 because it can check for events when a client connects or
101 disconnects. However when nbdkit is idle the filter needs to poll
102 for events every "SECS" seconds and if any event happens exit
103 immediately.
104
105 The default is 60 seconds.
106
108 Compared to --exit-with-parent
109 The nbdkit server option --exit-with-parent causes nbdkit to exit when
110 the parent process exits. It seems similar to:
111
112 nbdkit --filter=exitwhen ... exit-when-process-exits=$$
113
114 ($$ is the parent PID of nbdkit).
115
116 But there are significant differences caused by the implementation.
117 --exit-with-parent is implemented using the Linux feature
118 "PR_SET_PDEATHSIG" ("PROC_PDEATHSIG_CTL" on FreeBSD). This causes a
119 signal to be sent to the server when the parent process dies. On
120 receiving the signal nbdkit closes client connections and terminates at
121 once.
122
123 On the other hand "exit-when-process-exits" monitors the other process
124 (which does not need to be the parent) and shuts down the server in a
125 different way: currently open connections are allowed to continue until
126 they close.
127
128 Neither of these methods is completely reliable in all cases: signals
129 can be lost and there is a possible (albeit very small) race when
130 passing the PID to "exit-when-process-exits". More reliable methods of
131 clean up are "exit-when-pipe-closed" or putting all of the processes
132 into a cgroup. (See nbdkit-captive(1) and nbdkit-service(1)).
133
135 $filterdir/nbdkit-exitwhen-filter.so
136 The filter.
137
138 Use "nbdkit --dump-config" to find the location of $filterdir.
139
141 "nbdkit-exitwhen-filter" first appeared in nbdkit 1.24.
142
144 nbdkit(1), nbdkit-exitlast-filter(1), nbdkit-ip-filter(1),
145 nbdkit-limit-filter(1), nbdkit-rate-filter(1), nbdkit-captive(1),
146 nbdkit-service(1), nbdkit-filter(3), nbdkit-plugin(3).
147
149 Richard W.M. Jones
150
152 Copyright (C) 2020 Red Hat Inc.
153
155 Redistribution and use in source and binary forms, with or without
156 modification, are permitted provided that the following conditions are
157 met:
158
159 • Redistributions of source code must retain the above copyright
160 notice, this list of conditions and the following disclaimer.
161
162 • Redistributions in binary form must reproduce the above copyright
163 notice, this list of conditions and the following disclaimer in the
164 documentation and/or other materials provided with the
165 distribution.
166
167 • Neither the name of Red Hat nor the names of its contributors may
168 be used to endorse or promote products derived from this software
169 without specific prior written permission.
170
171 THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND ANY
172 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
173 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
174 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR CONTRIBUTORS BE
175 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
176 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
177 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
178 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
179 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
180 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
181 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
182
183
184
185nbdkit-1.30.7 2022-07-10 nbdkit-exitwhen-filter(1)