How to Run Programs in a New Session and Detach from Terminal in Linux Using setsid

When launching background services, long-running batch jobs, or scripts from a Linux shell, processes remain attached to the controlling terminal (TTY) and session in which they were spawned. If you close your terminal window, sever an SSH connection, or terminate the parent shell, the Linux kernel automatically broadcasts a SIGHUP (hangup) signal to all processes belonging to that session, abruptly killing your background tasks.

While commands like nohup mask the SIGHUP signal and the shell’s disown builtin removes jobs from active shell tracking, neither command alters the process’s underlying session hierarchy. The setsid utility solves this at the operating system level: it executes a program inside an entirely new session and process group, creating a true daemon process that completely severs ties with the controlling terminal.

Understanding Process Groups, Sessions, and Controlling TTYs

Every process in Linux belongs to a process group, which in turn belongs to a session. Under normal interactive execution, the terminal emulator acts as the session leader. You can inspect the process ID (PID), process group ID (PGID), session ID (SID), and controlling terminal (TTY) of any running program using ps:

ps -o pid,pgid,sid,tty,comm -C bash

When an interactive session terminates, all processes sharing that session’s SID receive SIGHUP. By invoking the setsid() system call through the command-line utility, setsid makes the target process the leader of a brand-new session with no controlling terminal, preventing signal propagation.

Running Detached Programs Using setsid

The setsid utility is included by default across all Linux distributions as part of the core util-linux package. Basic syntax requires passing your desired command and arguments:

setsid python3 -m http.server 8080

1. Forking to Prevent Terminal Blocking (-f)

By default, setsid executes the command in the foreground of the current shell unless instructed to fork. To immediately return control to your shell prompt without blocking the terminal, append the -f (or --fork) parameter:

setsid -f python3 -m http.server 8080

The parent process forks and immediately exits, leaving the child executing as a detached session leader in the background.

2. Redirecting Standard Input, Output, and Error Streams

Even when a process is detached from a session, its standard output (stdout) and standard error (stderr) file descriptors may still point to the terminal screen. To ensure clean execution and capture diagnostic output, redirect streams to a log file or /dev/null:

setsid -f /opt/scripts/backup.sh > /var/log/backup.log 2>&1 < /dev/null

Setting the Controlling Terminal (-c)

By design, setsid ensures the new session has no controlling terminal. However, certain legacy programs require an active controlling TTY to operate properly. If you need the new session to adopt the current terminal as its controlling terminal, use the -c (or --ctty) flag:

setsid -c my_application

Verifying Complete Session Detachment

To confirm that your program is running independently of your interactive terminal, query the process details:

ps -eo pid,pgid,sid,tty,stat,comm | grep python3

Look at the TTY column. When a program has been cleanly detached by setsid, the terminal column displays a question mark (?), confirming that no controlling terminal exists. You can safely close your terminal emulator or disconnect from your SSH session knowing the process will continue execution uninterrupted.

setsid vs nohup vs disown vs Terminal Multiplexers

Choosing the correct tool depends on your operational requirements:

  • setsid: Creates a completely new session and process group. Ideal for launching independent daemons from shell scripts where terminal detachment is mandatory.
  • nohup: Ignores the SIGHUP signal and automatically redirects output to nohup.out, but does not alter session ownership.
  • disown: A bash/zsh internal builtin that removes a job from the shell’s active job table; the process still shares the parent session.
  • tmux / screen: Maintains an interactive virtual terminal session across disconnections, ideal for workflows requiring interactive shell monitoring.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.