Skip to content

Instantly share code, notes, and snippets.

@aziis98
Last active January 19, 2022 04:56
Show Gist options
  • Select an option

  • Save aziis98/96950a4059a084d3ad5cbd1594c0f37e to your computer and use it in GitHub Desktop.

Select an option

Save aziis98/96950a4059a084d3ad5cbd1594c0f37e to your computer and use it in GitHub Desktop.
Launch commands in new detached tmux sessions.

Tmux launcher

Launch commands in a new detached tmux session. This needs tmux and sqlite3 to be installed, see launch --help for usage.

Sqlite is used for storing the command history in ~/.cache/aziis98.scripts.launch/history.db that can be run with launch past <SESSION_NAME>.

Info

Ideas

  • Extend to also store pwd of launched files to intelligently propose command to run "per folder".
#!/bin/bash
# TARGET_FILE=~/.local/bin/launch
STORAGE_DB="$HOME/.cache/aziis98.scripts.launch/history.db"
STORAGE_BUFFER="$HOME/.cache/aziis98.scripts.launch/history_buffer"
mkdir -p "$(dirname "$STORAGE_DB")"
sqlite3 "$STORAGE_DB" <<-EOF
CREATE TABLE IF NOT EXISTS history(
date TEXT,
session TEXT,
command TEXT
);
EOF
help() {
cat <<-EOF
usage: $(basename "$0") ARGS...
syntax:
<SESSION_NAME> '<COMMAND>'
Will $(basename "$0") a background session with the given command.
list, ls
Prints the list of openned sessions.
(or also "$(basename "$0") ls")
show <SESSION_NAME>
Attaches to the tmux session.
history, hist, h
Shows previously launched commands.
past <SESSION_NAME>
Launch a previously launched session from the stored command.
--help, -h
Show this help
EOF
exit 0
}
case "$1" in
--help|-h)
help
;;
list|ls)
if [ "$#" -ne 1 ]; then
echo -e "Too many parameters, see $(basename "$0") --help for usage"
exit 1
fi
tmux ls
exit 0
;;
history|hist|h)
echo -e "Launch Command History:"
sqlite3 "$STORAGE_DB" <<-EOF
.mode box
SELECT date as "Date", session as "Session", command as "Command" FROM history;
EOF
exit 0
;;
past)
if [ "$#" -ne 2 ]; then
echo -e "Incorrect number of parameters, see $(basename "$0") --help for usage"
exit 1
fi
SESSION_NAME="$2"
COMMAND="$(sqlite3 "$STORAGE_DB" <<-EOF
.headers off
.mode tabs
SELECT command FROM history WHERE session = "$SESSION_NAME" LIMIT 1;
EOF
)"
tmux new -d -s "$SESSION_NAME" "$COMMAND"
echo -e "Created tmux session \"$SESSION_NAME\" with (previous) command"
echo
echo -e " $COMMAND"
echo
echo -e "reattach with \"$(basename "$0") show $SESSION_NAME\""
exit 0
;;
show)
if [ "$#" -ne 2 ]; then
echo -e "Incorrect number of parameters, see $(basename "$0") --help for usage"
exit 1
fi
tmux attach -t "$2"
exit 0
;;
esac
if [ "$#" -ne 2 ]; then
echo -e "Too many parameters, see $(basename "$0") --help for usage"
exit 1
fi
tmux new -d -s "$1" "$2"
printf "$(date +'%Y-%m-%d %H:%M:%S')\t$1\t$2" > "$STORAGE_BUFFER"
sqlite3 "$STORAGE_DB" <<-EOF
.separator "\t"
.import $STORAGE_BUFFER history
EOF
echo -e "Created tmux session \"$1\" with command"
echo
echo -e " $2"
echo
echo -e "reattach with \"$(basename "$0") show $1\""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment