How to get process ID of background process?

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for How to get process ID of background process?

🤔 How to get process ID of background process?

We all know the frustration of starting a background process in our shell script and not being able to track or control it effectively.

So, how do we get the process ID (PID) of a background process from our shell script? 🤷‍♂️

The Issue

Let's take a look at the context: you start a background process in your shell script, and you want to be able to kill this process when your script finishes.

The problem arises when trying to retrieve the PID of the background process. You might think that the variable $! would give you the PID of the background process, but unfortunately, that's not the case. 😫

The Solution

But fear not! There are actually a couple of simple solutions to this problem. Let's dive in and check them out:

1. Using pgrep

One way to get the PID of a background process is by using the pgrep command. This command allows you to search for a process using various criteria, including the process name.

Here's an example of how you can use pgrep to retrieve the PID of your background process:

#!/bin/bash

# Start your background process
your_background_process &

# Get the PID of the background process
pid=$(pgrep -f "your_background_process")

echo "PID: $pid"

In the example above, pgrep -f "your_background_process" searches for a process whose name matches "your_background_process" and returns its PID. Make sure to replace "your_background_process" with the actual name of your background process.

2. Using a Temporary File

Another approach is to write the PID of the background process to a temporary file. This method allows you to easily access the PID at any point in your script.

Here's how you can implement this solution:

#!/bin/bash

# Start your background process
your_background_process &

# Write the PID of the background process to a temporary file
echo $! > bg_process.pid

# Retrieve the PID from the temporary file
pid=$(cat bg_process.pid)

echo "PID: $pid"

In this example, we create a temporary file called bg_process.pid and write the PID of our background process to it ($!). Later on, we can retrieve the PID from the file using cat bg_process.pid.

Call-to-Action

Now that you have two easy methods to get the PID of your background process, go ahead and try them out! 💪

If you found this guide helpful, make sure to share it with your friends and colleagues who might be facing the same issue. And if you have any other tips or tricks, feel free to leave a comment below and keep the conversation going! 🙌

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my