nbdkit-exitwhen-filter(1) | NBDKIT | nbdkit-exitwhen-filter(1) |
nbdkit-exitwhen-filter - exit gracefully when an event occurs
nbdkit --filter=exitwhen PLUGIN [exit-when-file-created=FILENAME] [exit-when-file-deleted=FILENAME] [exit-when-pipe-closed=FD] [exit-when-process-exits=PID] [exit-when-script=SCRIPT] [exit-when-poll=SECS]
"nbdkit-exitwhen-filter" is an nbdkit filter that causes nbdkit to exit gracefully when some external event occurs. Built-in events are: a file being created or deleted, a pipe being closed, or a process exiting. You can also define custom events using an external script or command.
After the event occurs nbdkit refuses new connections, waits for all current clients to disconnect, and then exits.
A similar filter is nbdkit-exitlast-filter(1). For other ways to ensure that nbdkit exits when you want see nbdkit-captive(1) and nbdkit-service(1).
nbdkit --filter=exitwhen memory 1G exit-when-file-created=/tmp/stop
nbdkit will run normally until something creates /tmp/stop, whereupon nbdkit will refuse new connections and exit as soon as the last client has disconnected. If /tmp/stop exists before nbdkit starts, it will exit immediately.
nbdkit --filter=exitwhen memory 1G exit-when-process-exits=1234
nbdkit will exit gracefully when PID 1234 exits and all connections close. If you want to exit when the parent process of nbdkit exits, consider using the --exit-with-parent flag instead.
You can define multiple "exit-when-*" events on the command line: nbdkit will exit if any of the events happens. If there are no "exit-when-*" events then the filter does nothing.
For an example of how to use this parameter, see: https://gitlab.com/nbdkit/nbdkit/-/blob/master/tests/test-exitwhen-pipe-closed.c
Note there is a small race between passing the process ID to the filter and the filter checking it for the first time. During this window the original PID might exit and an unrelated program might get the same PID, thus holding nbdkit open for longer than wanted. The pipe method above is more reliable if you are able to modify the other process.
The script should exit with code 88 if the event is detected. The filter does different things depending on the exit code of the script:
The default is 60 seconds.
The nbdkit server option --exit-with-parent causes nbdkit to exit when the parent process exits. It seems similar to:
nbdkit --filter=exitwhen ... exit-when-process-exits=$$
($$ is the parent PID of nbdkit).
But there are significant differences caused by the implementation. --exit-with-parent is implemented using the Linux feature "PR_SET_PDEATHSIG" ("PROC_PDEATHSIG_CTL" on FreeBSD). This causes a signal to be sent to the server when the parent process dies. On receiving the signal nbdkit closes client connections and terminates at once.
On the other hand "exit-when-process-exits" monitors the other process (which does not need to be the parent) and shuts down the server in a different way: currently open connections are allowed to continue until they close.
Neither of these methods is completely reliable in all cases: signals can be lost and there is a possible (albeit very small) race when passing the PID to "exit-when-process-exits". More reliable methods of clean up are "exit-when-pipe-closed" or putting all of the processes into a cgroup. (See nbdkit-captive(1) and nbdkit-service(1)).
Use "nbdkit --dump-config" to find the location of $filterdir.
"nbdkit-exitwhen-filter" first appeared in nbdkit 1.24.
nbdkit(1), nbdkit-exitlast-filter(1), nbdkit-ip-filter(1), nbdkit-limit-filter(1), nbdkit-rate-filter(1), nbdkit-captive(1), nbdkit-service(1), nbdkit-filter(3), nbdkit-plugin(3).
Richard W.M. Jones
Copyright (C) 2020 Red Hat Inc.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2023-01-04 | nbdkit-1.32.5 |