9

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?

4 Answers 4

14

Run

if tmux info &> /dev/null; then 
  echo running
else
  echo not running
fi
1
  • The else clause of the above code will be triggered even when a Tmux server is running, if that server does not have any running sessions. :'( – This will for example be the case if you started Tmux with tmux -D & before running the above code.
    – zrajm
    Jan 24 at 11:46
3
[[ -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.

1
  • This will fail for example if another user is logged in and running tmux. As a minimal precaution you'd want to add -u"$USER" to your pgrep command.
    – zrajm
    Jan 24 at 11:49
1

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
3
  • Help me clarify this. You're treating the command $(ps -e | grep -q tmux) as a boolean since most shell commands return true/false, depending on the exit code?
    – krystah
    Jan 6, 2014 at 15:57
  • @krystah Yes exactly. grep exit code is success only if at least one match is found, and switch -q suppress it's output.
    – Levans
    Jan 7, 2014 at 6:49
  • YMMV. The problem with this is that there may be other processes that have the string program in them. For example, the process running grep -q program will actually cause a match (at least on my system) or an open text file like vim program-not-running.txt. So, this does not work for me at all because it always returns true if tmux is running or not. Buyer beware.
    – bnjmn
    May 26, 2016 at 14:28
0

There are problems with the other answers, but it seems like whether or not Tmux is running can be reliably tested with:

if tmux run 2>/dev/null; then
    echo "Tmux running"
else
    echo "Tmux not running"
fi

When run when Tmux isn't running tmux run will output the following to standard error (which is why redirecting stderr to /dev/null is necessary):

no server running on /tmp/tmux-1000/default

I originally tried tmux run-shell true (and the equivalent tmux run : if you want to be terse) and both commands work quite well, but leaving out the shell command to be run altogether (as above) also work (and might be more efficient since no shell command need to run.

The problem with tmux info

This returns false when there is a Tmux server running which does not have any sessions in it. (E.g. if you started a server using tmux -D &.)

The problem with pgrep tmux

This will return true if any user has a process called tmux running – though this might be improved using pgrep -u"$USER" tmux there's no real attempt to check anything beyond the name of the process.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .