Repository: ray-project/ray
Issue category: Bug Report

Issue:
[Ray Clusters] `ray exec ... --stop --tmux ...` doesn't work with both `--stop` and `--tmux` specified
### What happened + What you expected to happen

When running `ray exec ...` with both `--stop` and `--tmux` flags, the `sudo shutdown -h now` command gets incorrectly left outside the tmux command and thus the machine is immediately shut down without the actual command finishing inside tmux.

For example, consider the following command:
```
ray exec \
  --verbose \
  --start \
  --stop \
  --tmux \
  --no-config-cache \
  ./cluster-config.yml \
  'echo "start" && sleep 10 && echo "done"'
```
This results in (as printed out by the command runner):
> Running `tmux new -d bash -c 'echo "start" && sleep 10 && echo "done"; ray stop; ray teardown ~/ray_bootstrap_config.yaml --yes --workers-only; exec bash'; sudo shutdown -h now`

The first part `tmux new -d bash -c '...'` returns immediately and thus the `sudo shutdown -h now` gets executed immediately before the command inside tmux finishes. I would expect the shutdown command to run only after the actual command.

### Versions / Dependencies

```
$ ray --version
ray, version 2.37.0
```

### Reproduction script

`cluster-config.yml`:
```yml
auth:
  ssh_user: ubuntu

cluster_name: minimal

provider:
  type: gcp
  region: us-east1
  availability_zone: us-east1-b
  project_id: [project-id]  # Globally unique project id
```

Command:
```
ray exec \
  --verbose \
  --start \
  --stop \
  --tmux \
  --no-config-cache \
  ./cluster-config.yml \
  'echo "start" && sleep 10 && echo "done"'
```

### Issue Severity

Medium: It is a significant difficulty but I can work around it.


Hints:
@hartikainen do you want to create a PR to fix it? We are happy to review the PR.

Patch:
diff --git a/python/ray/autoscaler/_private/commands.py b/python/ray/autoscaler/_private/commands.py
index 3c03738854f7..9a9b9d91cc2f 100644
--- a/python/ray/autoscaler/_private/commands.py
+++ b/python/ray/autoscaler/_private/commands.py
@@ -1153,16 +1153,15 @@ def exec_cluster(
         },
         docker_config=config.get("docker"),
     )
-    shutdown_after_run = False
     if cmd and stop:
         cmd = "; ".join(
             [
                 cmd,
                 "ray stop",
                 "ray teardown ~/ray_bootstrap_config.yaml --yes --workers-only",
+                "sudo shutdown -h now",
             ]
         )
-        shutdown_after_run = True
 
     result = _exec(
         updater,
@@ -1172,7 +1171,7 @@ def exec_cluster(
         port_forward=port_forward,
         with_output=with_output,
         run_env=run_env,
-        shutdown_after_run=shutdown_after_run,
+        shutdown_after_run=False,
         extra_screen_args=extra_screen_args,
     )
     if tmux or screen:


Test patch: