1nbdkit-exitwhen-filter(1)           NBDKIT           nbdkit-exitwhen-filter(1)
2
3
4

NAME

6       nbdkit-exitwhen-filter - exit gracefully when an event occurs
7

SYNOPSIS

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

DESCRIPTION

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

EXAMPLES

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

PARAMETERS

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       exit-when-process-exits=PID
60           Exit when process ID "PID" exits.
61
62           Note there is a small race between passing the process ID to the
63           filter and the filter checking it for the first time.  During this
64           window the original PID might exit and an unrelated program might
65           get the same PID, thus holding nbdkit open for longer than wanted.
66           The pipe method above is more reliable if you are able to modify
67           the other process.
68
69       exit-when-script="SCRIPT"
70           Create a custom event using the script or command "SCRIPT".  The
71           "SCRIPT" can be a program, shell script or a command with optional
72           parameters.  Note if using a filename here: you may need to use the
73           absolute path because nbdkit changes directory when it daemonizes.
74
75           The script should exit with code 88 if the event is detected.  The
76           filter does different things depending on the exit code of the
77           script:
78
79           0   The event has not been triggered, so nbdkit continues to
80               process requests as normal.
81
82           "1-87"
83               An error is logged, but the event is not triggered and nbdkit
84               continues to process requests as normal.
85
86           88  The event has been triggered.  nbdkit will refuse new
87               connections and exit gracefully as soon as all current clients
88               disconnect.
89
90           "89-"
91               Exit codes 89 and above are reserved for future use.  The
92               behaviour may change in future if scripts return any of these
93               exit codes.
94
95       exit-when-poll=SECS
96           When nbdkit is serving clients this filter does not need to poll
97           because it can check for events when a client connects or
98           disconnects.  However when nbdkit is idle the filter needs to poll
99           for events every "SECS" seconds and if any event happens exit
100           immediately.
101
102           The default is 60 seconds.
103

NOTES

105   Compared to --exit-with-parent
106       The nbdkit server option --exit-with-parent causes nbdkit to exit when
107       the parent process exits.  It seems similar to:
108
109        nbdkit --filter=exitwhen ... exit-when-process-exits=$$
110
111       ($$ is the parent PID of nbdkit).
112
113       But there are significant differences caused by the implementation.
114       --exit-with-parent is implemented using the Linux feature
115       "PR_SET_PDEATHSIG" ("PROC_PDEATHSIG_CTL" on FreeBSD).  This causes a
116       signal to be sent to the server when the parent process dies.  On
117       receiving the signal nbdkit closes client connections and terminates at
118       once.
119
120       On the other hand "exit-when-process-exits" monitors the other process
121       (which does not need to be the parent) and shuts down the server in a
122       different way: currently open connections are allowed to continue until
123       they close.
124
125       Neither of these methods is completely reliable in all cases: signals
126       can be lost and there is a possible (albeit very small) race when
127       passing the PID to "exit-when-process-exits".  More reliable methods of
128       clean up are "exit-when-pipe-closed" or putting all of the processes
129       into a cgroup.  (See nbdkit-captive(1) and nbdkit-service(1)).
130

FILES

132       $filterdir/nbdkit-exitwhen-filter.so
133           The filter.
134
135           Use "nbdkit --dump-config" to find the location of $filterdir.
136

VERSION

138       "nbdkit-exitwhen-filter" first appeared in nbdkit 1.24.
139

SEE ALSO

141       nbdkit(1), nbdkit-exitlast-filter(1), nbdkit-ip-filter(1),
142       nbdkit-limit-filter(1), nbdkit-rate-filter(1), nbdkit-captive(1),
143       nbdkit-service(1), nbdkit-filter(3), nbdkit-plugin(3).
144

AUTHORS

146       Richard W.M. Jones
147
149       Copyright (C) 2020 Red Hat Inc.
150

LICENSE

152       Redistribution and use in source and binary forms, with or without
153       modification, are permitted provided that the following conditions are
154       met:
155
156       ·   Redistributions of source code must retain the above copyright
157           notice, this list of conditions and the following disclaimer.
158
159       ·   Redistributions in binary form must reproduce the above copyright
160           notice, this list of conditions and the following disclaimer in the
161           documentation and/or other materials provided with the
162           distribution.
163
164       ·   Neither the name of Red Hat nor the names of its contributors may
165           be used to endorse or promote products derived from this software
166           without specific prior written permission.
167
168       THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND ANY
169       EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
170       IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
171       PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR CONTRIBUTORS BE
172       LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
173       CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
174       SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
175       BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
176       WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
177       OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
178       ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
179
180
181
182nbdkit-1.24.2                     2021-03-02         nbdkit-exitwhen-filter(1)
Impressum