Skip to content

Instantly share code, notes, and snippets.

@kirelagin
Last active November 2, 2023 11:05
Show Gist options
  • Save kirelagin/d4e13b027a384cb6ab65 to your computer and use it in GitHub Desktop.
Save kirelagin/d4e13b027a384cb6ab65 to your computer and use it in GitHub Desktop.
tmux magic for OpenRC
###
#
# tmux magic for OpenRC
#
# These functions do some tmux magic to let you run non-daemon programs
# (e.g. ones with ncurses interface) as daemons in tmux sessions.
# You just run `/etc/init.d/<service> start/stop` or add the service
# to a runlevel and later you can do `/etc/init.d/<service> attach` to
# see its console. Type `Ctrl+b d` to detach (in the default tmux configuration).
# We execute tmux with `-L`, so those sessions are not visible in the
# default tmux instance.
#
# Copypaste or source the following code into your init script.
# Call `tmux_magic_start` in the `start` function and
# `tmux_magic_stop` in the `stop` function.
# It is your job to set `$TMAGIC_USER`, `$TMAGIC_CMD` and `$TMAGIC_PIDFILE`
# and to ensure that the pidfile is writable.
# For an example see: <https://gist.github.com/099098c7418ddc66f3f3>.
#
#
# https://gist.github.com/d4e13b027a384cb6ab65
#
# Distributed under the terms of the GNU General Public License, v3 or later.
#
# © 2014 Kirill Elagin <kirelagin@gmail.com>
# http://kir.elagin.me/
###
tmux_magic_start() {
start-stop-daemon --start --user "${TMAGIC_USER}" \
--pidfile "${TMAGIC_PIDFILE}" \
--exec tmux -- -L _rc new-session -s "${RC_SVCNAME}" -d "echo \$\$ > \"${TMAGIC_PIDFILE}\" && exec ${TMAGIC_CMD}"
}
tmux_magic_stop() {
start-stop-daemon --stop --user "${TMAGIC_USER}" \
--pidfile "${TMAGIC_PIDFILE}"
}
attach() {
sudo -u "${TMAGIC_USER}" tmux -L _rc attach-session -t "${RC_SVCNAME}"
}
extra_started_commands="attach"
description_attach="Show the console"
@kirelagin
Copy link
Author

kirelagin commented Nov 2, 2023

@mid-kid start-stop-daemon --stop sends SIGTERM to the tmux server using its pid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment