|
Question: I want to know the child shell script running process id. How do I get PID in shell scripts.
When I execute a shell script, it starts a process called sub-shell. As a child of the main shell, the shell sub-shell commands in the script as a batch run (hence the term "batch process").
In some cases, you might want to know to run a sub-shell of PID. The PID information can be used in different situations. For example, you can use the shell script PID create a unique temporary files in / tmp. Sometimes a script needs to detect all running processes, it can exclude itself from the list of processes, sub-shell.
In bash, PID storage sub-shell process in a special variable '$$' in. This variable is read-only, you can not modify it in the script. such as:
#! / Bin / bash
echo "PID of this script: $$"
The above script will get the following output:
PID of this script: 6583
In addition to $$, bash shell also export other read-only variables. For example, PPID storage sub-shell parent process ID (that is, the primary shell). UID stored execution of the script of the current user ID. such as:
#! / 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
The above output, PID each execution will change. This because each run will create a new shell. On the other hand, PPID every time as long as you are running in the same shell.
For a list of all bash built-in variables, see man pages.
$ Man bash |
|
|
|