How to Run a Command in GNU Screen Session and Detach Without Termination


1 views

Many developers encounter this issue when trying to launch persistent background processes using GNU Screen. The default behavior:

screen -S session_name command_to_run

Terminates the screen session immediately after command execution, which defeats the purpose of using screen for background process management.

Screen terminates because it's designed to mirror the lifecycle of the executed command. When the command completes, screen assumes its job is done.

Method 1: Using -dm for Detached Mode

The most robust approach combines three flags:

screen -dmS session_name sh -c 'command_to_run; exec bash'

Breakdown:

  • -d -m: Starts in detached mode
  • -S: Names the session
  • sh -c: Creates a subshell to run commands
  • exec bash: Maintains session after command completion

Method 2: Complex Command Example

For running multiple commands:

screen -dmS data_processor sh -c \
'python3 process_data.py; \
echo "Processing complete" >> log.txt; \
exec bash'

Persistent Environment

To maintain specific environment variables:

screen -dmS env_session bash -c \
'export PATH=/custom/path:$PATH; \
./start_service.sh; \
exec bash'

Logging Output

Capture both stdout and stderr:

screen -dmS logging_session -L -Logfile session.log \
sh -c './script.sh 2>&1 | tee -a combined.log; exec bash'

Check running screens:

screen -ls

Reattach to verify:

screen -r session_name

When attempting to launch a command within a new screen session using screen -S session_name command, many administrators encounter the frustrating behavior where Screen terminates immediately after command execution. This happens because:


# Problematic command:
screen -S mySession /path/to/script.sh
# Result: "[screen is terminating]" 

The correct approach requires using Screen's built-in -dm (detached mode) flag combined with command execution:


# Working solution:
screen -S session_name -dm /path/to/command

Here are three real-world scenarios with complete command syntax:


# 1. Running a Python script:
screen -S data_processor -dm python3 /scripts/data_analysis.py

# 2. Starting a long-running server process:
screen -S node_server -dm node /app/server.js

# 3. Monitoring command with continuous output:
screen -S log_watcher -dm tail -f /var/log/syslog

After launching your detached session, verify its status with:


screen -ls
# Sample output:
# There is a screen on:
#    12345.data_processor    (Detached)

For more complex scenarios, consider these patterns:


# 1. With environment variables:
screen -S env_test -dm bash -c 'export VAR=value && /script/using_var.sh'

# 2. Multiple commands in sequence:
screen -S multi_cmd -dm bash -c 'command1 && command2'

# 3. Redirecting output to file:
screen -S logging -dm bash -c '/command/to/run > output.log 2>&1'

If you encounter problems:

  • Ensure Screen is properly installed (which screen)
  • Verify executable permissions on your command/script
  • Check system resource limits (ulimit -a)
  • Examine system logs for errors (dmesg | tail)