BASH Programming

How to Get the PID of a Shell Script

In Linux and Unix-based operating systems, the process ID (PID) is a unique identifier assigned to each running process, including shell scripts. Obtaining the PID of a shell script can be useful for monitoring, troubleshooting, and other administrative tasks. There are different ways to get the PID of a shell script and this article will discuss three methods to get the PID of a shell script.

How to Get the PID of a Shell Script

There are three ways to the process ID of a shell script but one should remember that the script should be running so here are those three ways:

How to Get the PID Using $$ Variable

One of the simplest ways to get the PID of a shell script is by using the built-in $$ variable. The $$ variable stores the PID of the current process, which in this case is the shell script. Here’s how to use the $$ variable to get the PID of a shell script, all you need to do is to just add the below given line on the script:

echo "PID of this script is:" $$

Graphical user interface, text Description automatically generated

When you run the script its process ID will be displayed as in the image below:

How to Get the PID Using ps Command

The ps command is a versatile tool for displaying information about running processes in Linux or Unix-based operating systems. Here’s how to use the ps command to get the PID of a shell script:

ps -ef | grep <script name>

The ps command lists all running processes, and the grep command searches for the process containing the shell script name. The awk command extracts the second column (which contains the PID) from the output of the grep command, here I have used the above syntax to get the process of a running script file:

ps -ef | grep bashfile.sh

How to Get the PID Using ps aux and awk Command

ps aux | grep <script-name> | grep -v grep | awk '{print $2}'

ps aux: This command lists all the running processes on the system. The a option shows all the processes for all users, the u option provides detailed information about each process.

grep <script-name>: This command searches for the process with the given script name in the output of the ps aux command.

grep -v grep: This command filters out the process with the name “grep” itself, which could otherwise appear in the output if the script name matches the “grep” keyword.

awk ‘{print $2}’: This command extracts the second field from the output of the previous command, which is the PID of the process. Awk is a programming language used for text processing and manipulation and in this case, it is used to extract the second field of the output, which contains the PID of the process:

ps aux | grep bashfile.sh | grep -v grep | awk '{print $2}'

Conclusion

Getting the PID of a shell script in Linux or Unix-based operating systems is a simple process that can be achieved using various methods. The $$ variable, ps command, and ps aux command are all useful tools for obtaining the PID of a shell script. These methods are useful for monitoring, troubleshooting, and other administrative tasks. However, different Linux distributions and versions may have varying commands and options available, which can cause some methods to not work on some systems.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.