• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • VCS
  • Interview Questions
  • Database
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

setsid Command Examples in Linux

by Deepika

setsid is a command-line utility in Unix-like operating systems that is used to run a program in a new session. A session is a collection of processes that share a common controlling terminal. By running a program in a new session, setsid detaches it from the controlling terminal of the current session, making it immune to hangups, signals, or other events that would otherwise be sent to the terminal.

In Unix-like systems, every process is associated with a process group, which is a collection of processes that share the same process ID. A process group leader is a process that is responsible for controlling the other processes in the group. If the calling process is not a process group leader, setsid creates a new session and sets the calling process as the session leader. The new session is not controlled by the current terminal by default.

The setsid command can be used to start a new process in the background, without any association with the current terminal. For example, the command setsid my_program & will start my_program in a new session and detach it from the current terminal, allowing it to continue running even after the terminal session has ended.

In addition to detaching a process from a controlling terminal, setsid can also be used to create a daemon process. A daemon process is a background process that runs continuously, without any interactive input or output. By using setsid to create a new session for the daemon process, it can be run independently of any user sessions or terminal sessions, ensuring that it continues to run even if the user logs out.

setsid Command Examples

1. Run a program in a new session:

# setsid program

2. Run a program in a new session discarding the resulting output and error:

# setsid program > /dev/null 2>&1

3. Run a program creating a new process:

# setsid --fork program

4. Return the exit code of a program as the exit code of setsid when the program exits:

# setsid --wait program

5. Run a program in a new session setting the current terminal as the controlling terminal:

# setsid --ctty program

Summary

Overall, setsid is a useful tool for running background processes or creating daemon processes in Unix-like systems. By creating a new session for the process, it detaches it from the current terminal and allows it to continue running independently of any user sessions or terminal sessions.

Filed Under: Linux

Some more articles you might also be interested in …

  1. Linux OS Service ‘kdump’
  2. stress: command not found
  3. mt Command Examples in Linux
  4. rpm: error while loading shared libraries: invalid ELF header
  5. Linux / UNIX : How to find files which has SUID/SGID set
  6. berks Command Examples (Chef cookbook dependency manager)
  7. How to troubleshoot NFS transfer latency issues using “nfsiostat” in CentOS / RHEL
  8. lvresize command examples in Linux
  9. CentOS / RHEL 6,7 : How to enable or disable XDMCP service (GDM)
  10. CentOS / RHEL 7 : Lock User Account After N Number of Incorrect Login Attempts

You May Also Like

Primary Sidebar

Recent Posts

  • glab Command Examples
  • “glab repo” Command Examples
  • “glab release” Command Examples
  • “glab pipeline” Command Examples

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright