How to Execute Commands in a Detached Tmux Session: A Practical Guide for Developers


2 views

When working with tmux, developers often need to execute commands in sessions that are running in the background. The standard approach of attaching to a session and running commands interactively isn't always practical, especially when automating workflows or managing remote servers.

Here's the proper way to execute commands in a detached tmux session:

tmux send-keys -t foo "ls" C-m

Let's break this down:

  • send-keys: The tmux command for sending key sequences
  • -t foo: Targets the session named 'foo'
  • "ls": The command to execute
  • C-m: Represents the Enter key (carriage return)

For more complex scenarios:

Running multiple commands:

tmux send-keys -t foo "cd /var/log && ls -la" C-m

Executing a script:

tmux send-keys -t foo "./deploy.sh --production" C-m

Starting a long-running process:

tmux send-keys -t foo "nohup python3 data_processor.py &" C-m

For server administration tasks, you might combine this with SSH:

ssh user@remote-server "tmux send-keys -t maintenance 'sudo apt update && sudo apt upgrade -y' C-m"

When working with multiple panes:

tmux send-keys -t foo:0.1 "git status" C-m

Issue: Command doesn't execute properly

Solution: Ensure you're using the correct session name and proper quoting:

tmux send-keys -t foo "command with spaces" C-m

Issue: Environment variables not available

Solution: Source your profile first:

tmux send-keys -t foo "source ~/.bashrc && my_command" C-m

Many developers working with tmux encounter this specific pain point: you've created a detached session (let's call it 'monitoring') and later need to execute commands in it programmatically. The standard tmux new-session -d -s monitoring creates the session, but how do you inject commands afterwards?

The key is using tmux send-keys combined with session targeting. Here's the syntax breakdown:


# Basic form:
tmux send-keys -t session_name "command" C-m

# Practical example:
tmux send-keys -t monitoring "top" C-m

Let's walk through a real-world scenario for starting a background process:


# Create detached session
tmux new-session -d -s data_processor

# Send multiple commands (wait 2 seconds between them)
tmux send-keys -t data_processor "cd ~/projects/analytics" C-m
sleep 2
tmux send-keys -t data_processor "./start_analysis.sh --full-scan" C-m

For complex automation, consider these patterns:


# Chain commands with separators
tmux send-keys -t worker "command1 && command2 || echo 'Failed'" C-m

# Environment variable injection
tmux send-keys -t deploy "export BUILD_NUMBER=142 && ./deploy.sh" C-m

# Multi-line commands
tmux send-keys -t setup "for i in {1..5}; do" C-m \
    "  echo \"Processing \$i\"" C-m \
    "done" C-m

When commands don't execute as expected:

  1. Verify session exists: tmux has-session -t session_name
  2. Check for proper quoting in complex commands
  3. Add slight delays between consecutive commands
  4. Consider using tmux pipe-pane for output capture