Last active
November 2, 2023 11:05
-
-
Save kirelagin/d4e13b027a384cb6ab65 to your computer and use it in GitHub Desktop.
tmux magic for OpenRC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
# | |
# 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 <[email protected]> | |
# 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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How does this make sure to actually kill the tmux session when using tmux_magic_stop? As far as I can tell, "tmux start-session -d" backgrounds the daemon, instead of keeping it foregrounded.