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
}
@sts10
Copy link

sts10 commented Nov 28, 2018

Thanks for this! My BASH is ... not good. The conditionals are definitely an improvement.

But I'm pretty sure that, in order to actually stop Syncthing from syncing, I need to run tmux send-keys -t synct C-c before the kill-session call. I just tried skipping that line and just killing the session, but Syncthing had not stopped.

So I think this is best-of-both:

function se {
  if ! tmux has-session -t synct; 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
  tmux kill-session -t synct
}

@sts10
Copy link

sts10 commented Nov 28, 2018

ah, and one minor, aesthetic thing re your version of ss:

When there's no sessions/server running, I run ss I get this kind of ugly double message

> ss
no server running on /tmp/tmux-1000/default
Starting up Syncthing at http://127.0.0.1:8384/

I tried changing the conditional to add a quiet flag with -q (if tmux -q has-session -t synct; then) but that fails to suppress that first error message. But this seems to only be a cosmetic thing... think the safety of the conditional is worth it.

@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