How to get the process ID (PID) in a script

Source: Internet
Author: User

I want to know the process ID of the running script shell. How do I get the PID in the shell script.

When I execute a shell script, it launches a process called a child shell. As a child of the main shell, the child shell runs the command in the shell script as a batch (hence the "batch process").

In some cases, you might want to know the PID of a child shell that is running. This PID information can be used in different situations. For example, you can use the PID of a shell script to create a unique temporary file in/tmp. Sometimes the script needs to detect all the running processes, and it can exclude its own child shell from the list of processes.

In bash, the PID of the child shell process is stored in a special variable ' $$ '. This variable is read-only and you can not modify it in the script. Like what:

#!/bin/bash

echo "PID of this script: $$"

The above script will get the following output:

PID of this script:6583

In addition to $$, the bash shell also exports other read-only variables. For example, PPID stores the ID of the child Shell parent process (that is, the primary shell). The UID stores the current user ID that executes the script. Like what:

#!/bin/bash

echo "PID of this script: $$"

echo "PPID of this script: $PPID"

echo "UID of this script: $UID"

The output is:

PID of this script:6686

PPID of this script:4656

UID of this script:1000

In the above output, the PID will change every time it executes. This will create a new shell for each run. On the other hand, Ppid is always the same as long as you run in the same shell.

For all bash built-in variable lists, refer to man page.

$ Man Bash

Well, the tutorial is here, I hope to bring you help!

Free pick up Brother Lian IT Education Original Linux Operations Engineer video/Detailed Linux tutorials, details of the website customer service: http://www.lampbrother.net/linux/

or hooking up with Q2430675018.

Welcome to the Linux Communication Group 478068715


How to get the process ID (PID) in a script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.