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