LinuxCommandLibrary

setsid

Run program in a new session

TLDR

Run a program in a new session

$ setsid [program]
copy

Run a program in a new session discarding the resulting output and error
$ setsid [program] > /dev/null 2>&1
copy

Run a program creating a new process
$ setsid [[-f|--fork]] [program]
copy

Return the exit code of a program as the exit code of setsid when the program exits
$ setsid [[-w|--wait]] [program]
copy

Run a program in a new session setting the current terminal as the controlling terminal
$ setsid [[-c|--ctty]] [program]
copy

SYNOPSIS

setsid [options] program [arguments...]

PARAMETERS

-c, --ctty
    Attempt to attach the new process's standard input/output/error to the specified terminal. Rarely used for typical daemonization.

-f, --fork
    Fork and execute the program in the child process. The parent setsid then exits immediately. This is useful for the double-forking idiom.

-w, --wait
    Wait for the child process to exit. This option is available in newer versions (e.g., util-linux >= 2.36).

--help
    Display help information and exit.

--version
    Display version information and exit.

DESCRIPTION

The setsid command runs a program in a new session. When invoked, it makes the specified program the leader of a new session, ensuring that the process group becomes independent of the controlling terminal from which setsid was launched. This is crucial for processes designed to run in the background, such as daemon processes, which need to continue executing even after the user logs out or the terminal session closes.

By creating a new session, setsid effectively detaches the process from any controlling terminal and prevents it from receiving signals like SIGHUP (hang-up signal) that are typically sent to processes associated with a disconnecting terminal. This allows long-running tasks to execute reliably without interruption, making it a fundamental tool for daemonization and background task management in Linux and Unix-like systems. It's often used in shell scripts to ensure a process's longevity.

CAVEATS

setsid itself does not automatically redirect standard I/O (stdin, stdout, stderr). For robust daemonization, you typically need to manually redirect these to /dev/null or specific log files.

If the new session leader (the program executed by setsid) subsequently opens a terminal, it can become its controlling terminal, potentially negating the detachment purpose unless further precautions are taken.

DOUBLE-FORKING IDIOM

For robust daemonization, setsid is often combined with a double-forking technique. The first fork creates a child that calls setsid, and then this child forks again. The grand-child process then becomes the actual daemon. This ensures that the intermediate child (the session leader) exits, allowing the grand-child to be reparented by init (or `systemd` etc.) and to be completely detached from the original controlling terminal, making it immune to terminal-related signals. The setsid --fork option simplifies this by effectively performing the first fork internally.

IMMUNITY TO SIGHUP

One of the primary reasons to use setsid is to prevent processes from receiving the SIGHUP signal. This signal is traditionally sent to processes in a session when their controlling terminal disconnects. By creating a new session and detaching from the terminal, processes started with setsid are no longer part of the original session and therefore do not receive SIGHUP upon terminal closure, allowing them to continue running unimpeded.

HISTORY

The concept of sessions and process groups, along with the `setsid()` system call, is a fundamental part of POSIX and Unix-like operating systems design. It enables the robust management of processes, particularly background services and daemons. The setsid utility provides a convenient shell-level wrapper around the `setsid()` system call, making it accessible for scripting and direct command-line use. Its utility has been consistent since the early days of Unix for managing long-running, terminal-independent processes.

SEE ALSO

nohup(1), disown(1), screen(1), tmux(1), fork(2), setsid(2)

Copied to clipboard