Skip to content

Instantly share code, notes, and snippets.

@shello
Last active February 1, 2019 04:14
Show Gist options
  • Save shello/3c7105c9391b7a3065636d09db228e22 to your computer and use it in GitHub Desktop.
Save shello/3c7105c9391b7a3065636d09db228e22 to your computer and use it in GitHub Desktop.
syncthing.sh: Syncthing + tmux control

syncthing.sh: Syncthing + tmux control

This is a simple script to control syncthing. It is possible to also source this script in 'sh'-like shells (tested in dash, bash and zsh). When used as a script it accepts a number of subcommands; if sourced, a number of syncthing_* functions are defined.

Functions / subcommands

syncthing_start / syncthing.sh start

Starts the Syncthing process silently into a background tmux session.

syncthing_open / syncthing.sh open

Open a browser window with the Syncthing web GUI; if a session has not been started beforehand, it is started before the web GUI is launched.

syncthing_stop / syncthing.sh stop

Stops the Syncthing process and the tmux session.

syncthing_inspect_tmux / syncthing.sh inspect_tmux

Opens the tmux session for inspecting the log, if necessary.

syncthing_session_started / syncthing.sh session_started

Exits with exit code 0 if a tmux session with Syncthing has been started; exit code 1 otherwise. (The output is always empty)

Credits

Original credits go to https://sts10.github.io/2018/11/27/syncthing-and-tmux.html.

Initial improvements made collaboratively at https://gist.github.com/shello/dea96c183f9e8e7fe181ef12335b44cf

License

ISC License

Copyright (c) 2019, Filipe 'shello' Rodrigues

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#!/usr/bin/env sh
if ! command -v syncthing >/dev/null; then
echo "$0: 'syncthing' is not in the PATH." >&2
return 1;
fi
_TMUX_SYNCTHING_SESSION="${_TMUX_SYNCTHING_SESSION:-syncthing}"
syncthing_session_started() {
tmux has-session -t "$_TMUX_SYNCTHING_SESSION" 2>/dev/null
}
syncthing_start() {
if syncthing_session_started; then
echo "$0: Syncthing session already started." >&2
return 1
fi
tmux new-session -d -s "$_TMUX_SYNCTHING_SESSION" "syncthing -no-browser"
}
syncthing_open() {
if ! syncthing_session_started; then
syncthing_start
fi
syncthing -browser-only
}
syncthing_inspect_tmux() {
tmux attach-session -t "$_TMUX_SYNCTHING_SESSION"
}
syncthing_stop() {
if ! syncthing_session_started; then
echo "$0: No Syncthing session to stop." >&2
return 1
fi
# Merely terminating the tmux session is not enough, as syncthing will
# receive a SIGHUP when the tmux session is terminated; syncthing (as of
# v1.0.0) will restart itself when it receives a SIGHUP; A SIGINT, issued
# by Ctrl-C ("C-c") is handled by Syncthing by exiting normally.
tmux send-keys -t "$_TMUX_SYNCTHING_SESSION" C-c
}
# Execute subcommand if executed with an argument
if [ -n "$1" ]; then
if command -v "syncthing_$1" >/dev/null; then
"syncthing_$1"
else
echo "$0: Subcommand $1 is not defined." >&2
exit 1
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment