Last active
March 27, 2021 01:31
-
-
Save truemedian/a864ec7f0dd5c379c64cd6797221295d to your computer and use it in GitHub Desktop.
Creates a tmux session with an automatically restarting command. stderr is automatically written to the tmux session AND the stderr log.
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
#!/usr/bin/env bash | |
RUN_SESSION_NAME="discord" | |
RUN_COMMAND="./luvit bot" | |
RUN_ERRLOG="./stderr.log" | |
if [ -z $TMUX ]; then | |
if ! command -v tmux >/dev/null; then | |
echo "fatal: tmux not installed" | |
exit 1 | |
fi | |
tmux attach-session -t $RUN_SESSION_NAME 2>/dev/null && exit 0 | |
tmux new-session -s $RUN_SESSION_NAME $0 || exit 1 | |
exit 0 | |
fi | |
if [ "$(tmux display-message -p '#S')" == "$RUN_SESSION_NAME" ]; then | |
while true; do | |
echo "[$(date)] [Running] $RUN_COMMAND" >> "$RUN_ERRLOG" | |
$RUN_COMMAND 2> >(tee -a "$RUN_ERRLOG" >&2) | |
code=$? | |
echo "[Exit] exited with code=$code" >> "$RUN_ERRLOG" | |
echo "" >> "$RUN_ERRLOG" | |
if [ $code == 254 ]; then | |
exit 0 | |
elif [ $code != 0 ]; then | |
sleep 5s | |
fi | |
done | |
else | |
echo "fatal: nesting tmux sessions is dangerous, please use this command outside of tmux." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment