setsid command tutorial in linux/unix with examples and use cases

Linux setsid command – run a program in a new session

Linux setsid command is usually used when you need to run scripts in the background. It is not affected by the terminal hangup signal.

syntax

setsid [options] program [arguments]

options

  • -c, –ctty
    Set the controlling terminal to the current one.
  • -f, –fork
    Always create a new process.
  • -w, –wait
    Wait for the execution of the program to end, and return the exit value of this program as the return value of setsid.

Examples & Use Cases

Running jobs (scripts) in the background

Run the ping command using the setsid command and exit the terminal.

➜  ~ setsid ping www.google.com > /dev/null 2>&1 &
[1] 4230
[1]  + 4230 done       setsid ping www.google.com > /dev/null 2>&1
➜  ~ ps -ef | grep ping | grep -v grep
ylspirit   4233      1  0 06:50 ?        00:00:00 ping www.google.com
➜  ~ exit
Connection to 192.168.159.171 closed.

Open a new terminal and use the ps command to see the ping command.

➜  ~ ps -ef | grep ping | grep -v grep
ylspirit   4233      1  0 06:50 ?        00:00:00 ping www.google.com

Add a Comment

Your email address will not be published. Required fields are marked *