Skip to content

Instantly share code, notes, and snippets.

@shello
Last active November 28, 2018 02:00
Show Gist options
  • Save shello/dea96c183f9e8e7fe181ef12335b44cf to your computer and use it in GitHub Desktop.
Save shello/dea96c183f9e8e7fe181ef12335b44cf to your computer and use it in GitHub Desktop.
# Via: https://sts10.github.io/2018/11/27/syncthing-and-tmux.html
# Changes to `ss` and `se`:
# - Start session with command on `new-session`: avoids creating a shell
# session just for `syncthing`; also use only `kill-session` instead of
# sending keys.
# - Test if the sessions exist (or don't exist) before creating/killing them.
# in ~/.bash_profile or ~/.bashrc
function ss {
if tmux has-session -t synct 2>/dev/null; then
echo "Syncthing session already started." >&2
return 1
fi
echo "Starting up Syncthing at http://127.0.0.1:8384/"
tmux new-session -d -s synct "syncthing -no-browser"
}
function se {
if ! tmux has-session -t synct 2>/dev/null; then
echo "No Syncthing session to end." >&2
return 1
fi
echo "Stopping Syncthing and killing the tmux session"
tmux send-keys -t synct C-c
}
@shello
Copy link
Author

shello commented Nov 28, 2018

Shell script skills only improve with experience, so keep at it :)

Regarding syncthing remaining executing after the kill-session, that's very interesting. I wonder if the signal tmux kill-session is sending (I was unable to find documentation for that quickly) is being ignored by syncthing. Ctrl-C sends a SIGINT, so I'm assuming kill-session sends another signal (maybe SIGTERM?).
If that is the case, then the se function you wrote probably doesn't need the kill-session command: by sending the Ctrl-C the syncthing process exits, and the session should end right away.

@shello
Copy link
Author

shello commented Nov 28, 2018

Regarding the error message: you can redirect that output (which tmux writes to stderr, file descriptor 2) to /dev/null with the shell redirection 2>/dev/null. I made this change, along with your se fix, to the gist!

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