Skip to content

Commit 201acb7

Browse files
authored
feat(claude-code): use tmux instead of screen (#432)
1 parent 8c2ea35 commit 201acb7

File tree

2 files changed

+60
-14
lines changed

2 files changed

+60
-14
lines changed

Diff for: claude-code/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Run the [Claude Code](https://docs.anthropic.com/en/docs/agents-and-tools/claude
1414
```tf
1515
module "claude-code" {
1616
source = "registry.coder.com/modules/claude-code/coder"
17-
version = "1.0.31"
17+
version = "1.1.0"
1818
agent_id = coder_agent.example.id
1919
folder = "/home/coder"
2020
install_claude_code = true
@@ -25,7 +25,7 @@ module "claude-code" {
2525
### Prerequisites
2626

2727
- Node.js and npm must be installed in your workspace to install Claude Code
28-
- `screen` must be installed in your workspace to run Claude Code in the background
28+
- Either `screen` or `tmux` must be installed in your workspace to run Claude Code in the background
2929
- You must add the [Coder Login](https://registry.coder.com/modules/coder-login) module to your template
3030

3131
The `codercom/oss-dogfood:latest` container image can be used for testing on container-based workspaces.
@@ -43,7 +43,7 @@ The `codercom/oss-dogfood:latest` container image can be used for testing on con
4343
> Join our [Discord channel](https://discord.gg/coder) or
4444
> [contact us](https://coder.com/contact) to get help or share feedback.
4545
46-
Your workspace must have `screen` installed to use this.
46+
Your workspace must have either `screen` or `tmux` installed to use this.
4747

4848
```tf
4949
variable "anthropic_api_key" {
@@ -83,14 +83,14 @@ resource "coder_agent" "main" {
8383
module "claude-code" {
8484
count = data.coder_workspace.me.start_count
8585
source = "registry.coder.com/modules/claude-code/coder"
86-
version = "1.0.31"
86+
version = "1.1.0"
8787
agent_id = coder_agent.example.id
8888
folder = "/home/coder"
8989
install_claude_code = true
9090
claude_code_version = "0.2.57"
9191
9292
# Enable experimental features
93-
experiment_use_screen = true
93+
experiment_use_screen = true # Or use experiment_use_tmux = true to use tmux instead
9494
experiment_report_tasks = true
9595
}
9696
```
@@ -102,7 +102,7 @@ Run Claude Code as a standalone app in your workspace. This will install Claude
102102
```tf
103103
module "claude-code" {
104104
source = "registry.coder.com/modules/claude-code/coder"
105-
version = "1.0.31"
105+
version = "1.1.0"
106106
agent_id = coder_agent.example.id
107107
folder = "/home/coder"
108108
install_claude_code = true

Diff for: claude-code/main.tf

+54-8
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ variable "experiment_use_screen" {
5454
default = false
5555
}
5656

57+
variable "experiment_use_tmux" {
58+
type = bool
59+
description = "Whether to use tmux instead of screen for running Claude Code in the background."
60+
default = false
61+
}
62+
5763
variable "experiment_report_tasks" {
5864
type = bool
5965
description = "Whether to enable task reporting."
@@ -122,6 +128,39 @@ resource "coder_script" "claude_code" {
122128
coder exp mcp configure claude-code ${var.folder}
123129
fi
124130
131+
# Handle terminal multiplexer selection (tmux or screen)
132+
if [ "${var.experiment_use_tmux}" = "true" ] && [ "${var.experiment_use_screen}" = "true" ]; then
133+
echo "Error: Both experiment_use_tmux and experiment_use_screen cannot be true simultaneously."
134+
echo "Please set only one of them to true."
135+
exit 1
136+
fi
137+
138+
# Run with tmux if enabled
139+
if [ "${var.experiment_use_tmux}" = "true" ]; then
140+
echo "Running Claude Code in the background with tmux..."
141+
142+
# Check if tmux is installed
143+
if ! command_exists tmux; then
144+
echo "Error: tmux is not installed. Please install tmux manually."
145+
exit 1
146+
fi
147+
148+
touch "$HOME/.claude-code.log"
149+
150+
export LANG=en_US.UTF-8
151+
export LC_ALL=en_US.UTF-8
152+
153+
# Create a new tmux session in detached mode
154+
tmux new-session -d -s claude-code -c ${var.folder} "claude"
155+
156+
# Send the prompt to the tmux session if needed
157+
if [ -n "$CODER_MCP_CLAUDE_TASK_PROMPT" ]; then
158+
tmux send-keys -t claude-code "$CODER_MCP_CLAUDE_TASK_PROMPT"
159+
sleep 5
160+
tmux send-keys -t claude-code Enter
161+
fi
162+
fi
163+
125164
# Run with screen if enabled
126165
if [ "${var.experiment_use_screen}" = "true" ]; then
127166
echo "Running Claude Code in the background..."
@@ -182,20 +221,27 @@ resource "coder_app" "claude_code" {
182221
#!/bin/bash
183222
set -e
184223
185-
if [ "${var.experiment_use_screen}" = "true" ]; then
224+
export LANG=en_US.UTF-8
225+
export LC_ALL=en_US.UTF-8
226+
227+
if [ "${var.experiment_use_tmux}" = "true" ]; then
228+
if tmux has-session -t claude-code 2>/dev/null; then
229+
echo "Attaching to existing Claude Code tmux session." | tee -a "$HOME/.claude-code.log"
230+
tmux attach-session -t claude-code
231+
else
232+
echo "Starting a new Claude Code tmux session." | tee -a "$HOME/.claude-code.log"
233+
tmux new-session -s claude-code -c ${var.folder} "claude --dangerously-skip-permissions | tee -a \"$HOME/.claude-code.log\"; exec bash"
234+
fi
235+
elif [ "${var.experiment_use_screen}" = "true" ]; then
186236
if screen -list | grep -q "claude-code"; then
187-
export LANG=en_US.UTF-8
188-
export LC_ALL=en_US.UTF-8
189-
echo "Attaching to existing Claude Code session." | tee -a "$HOME/.claude-code.log"
237+
echo "Attaching to existing Claude Code screen session." | tee -a "$HOME/.claude-code.log"
190238
screen -xRR claude-code
191239
else
192-
echo "Starting a new Claude Code session." | tee -a "$HOME/.claude-code.log"
193-
screen -S claude-code bash -c 'export LANG=en_US.UTF-8; export LC_ALL=en_US.UTF-8; claude --dangerously-skip-permissions | tee -a "$HOME/.claude-code.log"; exec bash'
240+
echo "Starting a new Claude Code screen session." | tee -a "$HOME/.claude-code.log"
241+
screen -S claude-code bash -c 'claude --dangerously-skip-permissions | tee -a "$HOME/.claude-code.log"; exec bash'
194242
fi
195243
else
196244
cd ${var.folder}
197-
export LANG=en_US.UTF-8
198-
export LC_ALL=en_US.UTF-8
199245
claude
200246
fi
201247
EOT

0 commit comments

Comments
 (0)