Tmux - Check if server is up
Is there any known tmux
-function, like tmux has-session -t <session>
, but to check if the tmux server is currently running? It would be handy when writing automation-scripts. When checking if a session exists with
$ tmux has-session -t SomeSession
the user is, unless the tmux server is already running, presented with the error
failed to connect to server
Therefore, I want to check beforehand if the server is currently running. If it is, check for sessions et cetera. Is there any way to silence this without piping to /dev/null?
Solution 1:
Run
if tmux info &> /dev/null; then
echo running
else
echo not running
fi
Solution 2:
[[ -n $(pgrep tmux) ]] && echo true || echo false
If a process with “tmux” in it name is running, this will print true, otherwise it will print false.
This works because pgrep finds all process with "tmux" in their names and returns their PIDs. The "-n" basically tests whether the output of $(pgrep tmux) exists. If the output does exist (ex. "14204 23137"), then the test evaluates to true. If the output doesn't exist (or is undefined), the test evaluates to false.
If you prefer long-form:
if [[ -n $(pgrep tmux) ]]; then
echo true
else
echo false
fi
Note that what bnjmn said applies here. Namely, that this will give a false positive if a process is running other than tmux, but has "tmux" in its name.
Solution 3:
You can use ps -e | grep -q program
to check if program program
is running in a general way.
As an example in a shell :
if $(ps -e | grep -q tmux); then echo "Tmux is running."; fi