Linux setsid Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux setsid command and learn how to use it to detach a process from the current session and run it in the background. The setsid command creates a new session with the calling process as the session leader, allowing the process to run independently without being affected by signals or terminal input/output of the current session. We will start by understanding the setsid command and then demonstrate how to use it to run background processes effectively.

The lab covers the following steps:

  • Understanding the setsid command
  • Detaching a process from the current session
  • Running background processes with setsid

The setsid command is a useful tool for managing processes in Linux, and this lab provides practical examples to help you understand and apply it in your daily workflow.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicSystemCommandsGroup -.-> linux/sleep("`Execution Delaying`") linux/SystemInformationandMonitoringGroup -.-> linux/ps("`Process Displaying`") subgraph Lab Skills linux/cat -.-> lab-422913{{"`Linux setsid Command with Practical Examples`"}} linux/sleep -.-> lab-422913{{"`Linux setsid Command with Practical Examples`"}} linux/ps -.-> lab-422913{{"`Linux setsid Command with Practical Examples`"}} end

Understanding the setsid Command

In this step, we will explore the setsid command in Linux, which is used to detach a process from the current session and run it in the background.

The setsid command creates a new session with the calling process as the session leader. This means that the process will be isolated from the current session and will not be affected by signals or terminal input/output of the current session.

Let's start by running a simple command using setsid:

setsid sleep 60

This will run the sleep 60 command in a new session, and the process will continue to run even if you close the current terminal.

Example output:

[1] 1234

The output shows the process ID (PID) of the sleep command, which is running in the background.

Now, let's verify that the process is running in a separate session:

ps -o sid,pid,cmd | grep sleep

Example output:

  1234 1234 sleep 60

The ps command shows that the sleep process has a different session ID (SID) than the current session.

In the next step, we will learn how to use setsid to run background processes more effectively.

Detaching a Process from the Current Session

In this step, we will learn how to detach a process from the current session using the setsid command.

Detaching a process from the current session is useful when you want to run a long-running process in the background, without it being affected by signals or terminal input/output of the current session.

Let's start by running a simple command using setsid:

setsid bash -c 'sleep 60 && echo "Process completed"'

This will run the sleep 60 command in a new session, and the process will continue to run even if you close the current terminal. The echo "Process completed" command will be executed after the sleep command is finished.

Example output:

[1] 1234

The output shows the process ID (PID) of the sleep command, which is running in the background.

Now, let's verify that the process is running in a separate session:

ps -o sid,pid,cmd | grep sleep

Example output:

  1234 1234 sleep 60

The ps command shows that the sleep process has a different session ID (SID) than the current session.

To check the output of the detached process, we can use the ps command to find the PID of the process, and then use the cat command to read the output from the /proc/<PID>/fd/1 file (which represents the stdout of the process):

pid=$(ps -o pid,cmd | grep 'sleep 60' | awk '{print $1}')
cat /proc/$pid/fd/1

Example output:

Process completed

This shows that the echo "Process completed" command was executed after the sleep command finished.

In the next step, we will learn how to run background processes more effectively using setsid.

Running Background Processes with setsid

In this final step, we will learn how to use the setsid command to effectively run background processes in Linux.

One common use case for setsid is to run long-running processes in the background, without them being affected by the current session. This is particularly useful when you need to run a process that should continue to run even after you log out of the system.

Let's try an example:

setsid bash -c 'while true; do echo "Running in the background" >> ~/project/output.txt; sleep 5; done'

This will start a background process that writes "Running in the background" to a file named output.txt in the ~/project directory every 5 seconds.

Example output:

[1] 1234

The output shows the process ID (PID) of the background process.

Now, let's verify that the process is running in a separate session and that the output file is being generated:

ps -o sid,pid,cmd | grep 'while true'
cat ~/project/output.txt

Example output:

  1234 1234 bash -c 'while true; do echo "Running in the background" >> ~/project/output.txt; sleep 5; done'
Running in the background
Running in the background
Running in the background

The ps command shows that the process is running in a separate session, and the cat command shows the contents of the output.txt file, which is being continuously updated by the background process.

Even if you close the current terminal, the background process will continue to run and update the output.txt file. You can check the file later to see the output.

In this lab, we've learned how to use the setsid command to detach processes from the current session and run them in the background. This is a useful technique for managing long-running processes in Linux.

Summary

In this lab, we explored the setsid command in Linux, which is used to detach a process from the current session and run it in the background. We learned that setsid creates a new session with the calling process as the session leader, isolating the process from the current session and its signals or terminal input/output. We demonstrated how to use setsid to run a simple sleep command in the background, and how to verify that the process is running in a separate session. Additionally, we learned how to use setsid to detach a process from the current session, allowing long-running processes to continue running even if the current terminal is closed.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like