Skip to content

Instantly share code, notes, and snippets.

@dayne
Last active January 11, 2022 11:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dayne/1bc3d0ead1044e4094f8fd8951cc2c34 to your computer and use it in GitHub Desktop.
Save dayne/1bc3d0ead1044e4094f8fd8951cc2c34 to your computer and use it in GitHub Desktop.
tmux-launcher | simply on reboot tmux session launches

Simple tmux-launcher.sh for a script or command that can check to see if the session name exists yet or not before launching.

Put somewhere, like $HOME/.local/bin

Use it like this: crontab -e

@reboot $HOME/.local/bin keyword scriptname

or a command and arguments like:

@reboot $HOME/.local/bin keyword -- command -with -options and arguments here

Like so:

@reboot $HOME/.local/bin funkybot $HOME/funky/bot.sh

setup

cd ~/.local/bin
wget https://gist.githubusercontent.com/dayne/1bc3d0ead1044e4094f8fd8951cc2c34/raw/tmux-launcher.sh
chmod +x tmux-launcher.sh

Example usage - ngrok tunnel

After configuring ngrok to use a .ngrok2/ngrok.yml file you will want it to launch on reboot and may still want be able to check in on the ngrok process & manage it from console.

Then you can use it like

@reboot  $HOME/.local/bin/tmux-launcher.sh ngrok -- $HOME/.local/bin/ngrok start --all

Or if you want a script approach create a new file: $HOME/.local/bin/onboot-ngrok.sh

#!/bin/bash -l
ngrok start --all

chmod +x onboot-ngrok.sh

test it ./onboot-ngrok.sh If it works then use the tmux-launcher.sh tool:

crontab -e

@reboot $HOME/.local/bin/tmux-launcher.sh ngrok $HOME/.local/bin/onboot-ngrok.sh
#!/bin/bash
NAME=$1
SCRIPT=$2
if [ "$SCRIPT" == "--" ]; then
arr=($@)
unset arr[0] # $NAME
unset arr[1] # -- our flag
SCRIPT="${arr[@]}" # everything after -- flag
fi
tmux list-sessions | grep $NAME > /dev/null > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "a $NAME session already started"
echo "tmux attach -t $NAME"
#tmux kill-session -t $NAME
else
tmux new-session -d -s $NAME -n term
tmux send-keys -t $NAME:term "$SCRIPT" Enter
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment