Bug#1108969: sysvinit-utils: add common support for oneshot services in init-d-script helper
Andrew Bower
andrew at bower.uk
Wed Sep 17 22:49:17 BST 2025
Hi Mark,
On Tue, Sep 09, 2025 at 05:23:21PM +0100, Mark Hindley wrote:
> Attached is an initial implementation. I decided hooking into init-d-script at
> the correct place was better and it sitll leaves the do_*_override functions for
> the sysadmin, if they require.
Excellent, thanks for writing this! Yes, I think this is a good approach.
> #
> # Support for oneshot services
> #
> oneshot() {
> [ "$TYPE" = oneshot ] || return
> IDS_RUNDIR=/run/init-d-script
> case $1 in
> setup) mkdir -p "$IDS_RUNDIR" ;;
> start-pre) [ -f /$IDS_RUNDIR/$NAME.oneshot ] && exit ;;
> start-post) touch /$IDS_RUNDIR/$NAME.oneshot ;;
> stop-pre) [ -f /$IDS_RUNDIR/$NAME.oneshot ] || exit ;;
> stop-post) rm -f /$IDS_RUNDIR/$NAME.oneshot ;;
> status)
> if [ -f /$IDS_RUNDIR/$NAME.oneshot ]; then
> log_success_msg "$NAME (oneshot) has been run"
> else
> log_failure_msg "$NAME (oneshot) has not been run"
> fi
> ;;
> *) echo "ERROR: invalid oneshot request: $1" >&2 ;;
> esac
> }
Why did you go for the subdirectory? The PID file paths generated
automatically are of the form /run/$NAME.pid so why not the same of
oneshot (/run/$NAME.oneshot)? Then it's the same level of directory an
administrator might look in to examine both types of service. I liked
your original suggestion in this respect!
You could also go one step closer to normal services and have
ONESHOTFILE equivalent to PIDFILE that can similarly be overridden.
> #
> # Function that starts the daemon/service
> #
>
> do_start_cmd() {
> if [ "$SETPRIV_ARGS" ]; then
> if ! PATH=/bin:/usr/bin command -v setpriv >/dev/null 2>&1; then
> echo "WARNING: setpriv not available, ignoring SETPRIV_ARGS" >&2
> unset SETPRIV_ARGS
> fi
> fi
> ${SETPRIV_ARGS:+setpriv $SETPRIV_ARGS} start-stop-daemon --start --quiet \
> ${PIDFILE:+--pidfile "$PIDFILE"} \
> ${COMMAND_NAME:+--name "$COMMAND_NAME"} \
> ${DAEMON:+--exec "$DAEMON"} $START_ARGS -- $DAEMON_ARGS
> }
Any chance we could consider the setpriv adaptation for the stop
command, too, somehow? Arguably it is overkill to be doing this at all
for one-shot services like this, to be honest, but maybe with a neat
wrapper function it would be simple.
> do_start() {
> oneshot start-pre
> if is_call_implemented do_start_prepare; then
> call do_start_prepare
> fi
> log_daemon_msg "Starting $DESC" "$NAME"
> call do_start_cmd
> retval=$?
> if [ "$retval" = 0 ]; then
> oneshot start-post
> fi
> if [ "$retval" = 1 ] && [ no != "$VERBOSE" ]; then
> log_progress_msg "is already running"
> fi
> retval=$((retval > 1))
> vlog_end_msg $retval
> if is_call_implemented do_start_cleanup; then
> call do_start_cleanup
> fi
> return $retval
> }
I'd still love it if retval could be available to the cleanup function,
somehow, but that's an aesthetic question that's not really related to
the one-shot provision.
On Mon, Sep 15, 2025 at 08:22:55AM +0100, Mark Hindley wrote:
> I think this is better to avoid an error when stopping an already stopped
> oneshot service.
Agreed - I did something like that in my adaptation, too.
My example use of your new version of the script can be seen at:
https://salsa.debian.org/pkg-security-team/acct/-/commits/experimental-oneshot-1108969
Andrew
More information about the Debian-init-diversity
mailing list