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.
Exit when the named file is created or deleted.
The read end of a pipe(2) is passed to nbdkit in the given file descriptor number. Exit when the pipe is closed. The filter does not read any data from the pipe.
For an example of how to use this parameter, see: https://gitlab.com/nbdkit/nbdkit/-/blob/master/tests/test-exitwhen-pipe-closed.c
Exit when process ID
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.
Create a custom event using the script or command
SCRIPTcan be a program, shell script or a command with optional parameters. Note if using a filename here: you may need to use the absolute path because nbdkit changes directory when it daemonizes.
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 event has not been triggered, so nbdkit continues to process requests as normal.
An error is logged, but the event is not triggered and nbdkit continues to process requests as normal.
The event has been triggered. nbdkit will refuse new connections and exit gracefully as soon as all current clients disconnect.
Exit codes 89 and above are reserved for future use. The behaviour may change in future if scripts return any of these exit codes.
When nbdkit is serving clients this filter does not need to poll because it can check for events when a client connects or disconnects. However when nbdkit is idle the filter needs to poll for events every
SECSseconds and if any event happens exit immediately.
The default is 60 seconds.
Compared to --exit-with-parent
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
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)).
nbdkit --dump-configto find the location of
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:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- Neither the name of Red Hat nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
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.
nbdkit(1), nbdkit-captive(1), nbdkit-exitlast-filter(1), nbdkit-filter(3), nbdkit-ip-filter(1), nbdkit-limit-filter(1), nbdkit-rate-filter(1), nbdkit-release-notes-1.24(1), nbdkit-service(1).