How to run nohup and write its pid file in a single bash statement
Categories:
Running nohup with PID file in a Single Bash Statement

Learn how to execute a command with nohup in the background and capture its Process ID (PID) into a file, all within a single, concise bash statement. This technique is crucial for managing long-running processes on Linux servers.
When managing long-running processes on Linux systems, nohup is an indispensable command. It allows a process to continue running in the background even after the user logs out. However, knowing the Process ID (PID) of a nohup'd process is often necessary for monitoring, stopping, or interacting with it later. This article will guide you through a robust method to execute a command with nohup and simultaneously write its PID to a file, all encapsulated within a single, elegant bash statement.
Understanding nohup and Background Processes
nohup prevents processes from being terminated when the user's session ends. It redirects stdout and stderr to nohup.out by default, unless specified otherwise. When you run a command with nohup and append & to it, the process is moved to the background. The challenge then becomes reliably capturing its PID without race conditions or complex scripting.

Conceptual flow of nohup and PID capture.
The Challenge: Capturing PID Reliably
When a command is sent to the background using &, the shell immediately prints its PID. However, capturing this PID reliably in an automated script can be tricky. Simply piping the output of nohup won't work as nohup typically doesn't send the PID to its own standard output. We need a way to capture the PID that the shell itself reports.
$! contains the PID of the most recently executed background command. This is the key to our solution.The Single-Statement Solution
The trick involves using a subshell or a command group, followed by immediately echoing the $! variable. By wrapping the nohup command in parentheses () or curly braces {}, we can ensure that $! refers to our background nohup process.
nohup bash -c 'sleep 60 & echo $! > /tmp/my_process.pid' &
Executing sleep 60 with nohup and writing its PID to /tmp/my_process.pid.
Let's break down this command:
nohup: Ensures the command continues after logout.bash -c '...': Executes the enclosed string as a command. This is crucial because it creates a new shell where$! will correctly refer to thesleepcommand.sleep 60 &: This is the actual command we want to run in the background. The&putssleep 60into the background within thebash -csubshell.echo $! > /tmp/my_process.pid: Immediately aftersleep 60is backgrounded,$! holds its PID. This PID is then redirected to the specified file.- The final
&: This puts the entirenohup bash -c '...'command into the background, allowing your current terminal session to continue immediately.
/tmp/ is generally safe for temporary PIDs, but for production systems, consider a dedicated directory like /var/run/.Verifying the Process and PID
After executing the command, you can verify that the process is running and its PID has been correctly captured.
1. Step 1
Execute the nohup command: nohup bash -c 'sleep 300 & echo $! > /tmp/my_process.pid' &
2. Step 2
Check the contents of the PID file: cat /tmp/my_process.pid
3. Step 3
Verify the process is running using ps: ps -p $(cat /tmp/my_process.pid)
4. Step 4
Kill the process using the PID from the file: kill $(cat /tmp/my_process.pid)
This method provides a clean and reliable way to manage background processes with nohup, ensuring you always have a handle on their PIDs for administrative tasks. It's a powerful technique for scripting robust system services and long-running jobs.