Get parent pid linux

Getting parent PID with shell?

I want to have a cron job that will set the priority of some services, however, the parent runs as root and the rest as another user. However, the parent will respawn processes with it’s priority ID so the cron job would have to run way more then it should. Is there a way to do this to set priority’s? Example of what I want to set higher:

1 S root 13826 1 0 81 0 - 3289 rt_sig 00:33 ? 00:00:00 nginx: master process /usr/local/sbin/nginx -c /etc/nginx/nginx.conf 5 S root 15474 1 0 75 0 - 3848 - Apr22 ? 00:01:37 /usr/local/apache/bin/httpd -k start -DSSL 5 S nobody 19511 13826 0 75 0 - 3297 - 13:06 ? 00:00:10 nginx: worker process 5 S nobody 19512 13826 0 78 0 - 3361 - 13:06 ? 00:00:11 nginx: worker process 5 S nobody 19513 13826 0 75 0 - 3681 - 13:06 ? 00:00:09 nginx: worker process 5 S nobody 19514 13826 0 78 0 - 3297 - 13:06 ? 00:00:07 nginx: worker process 5 S root 19521 15474 0 77 0 - 3561 - 13:06 ? 00:00:00 /usr/local/apache/bin/httpd -k start -DSSL 5 S nobody 19522 15474 0 78 0 - 3848 431083 13:06 ? 00:00:00 /usr/local/apache/bin/httpd -k start -DSSL 5 S nobody 19523 15474 0 75 0 - 3952 semtim 13:06 ? 00:00:00 /usr/local/apache/bin/httpd -k start -DSSL 5 S nobody 19524 15474 0 75 0 - 3951 semtim 13:06 ? 00:00:00 /usr/local/apache/bin/httpd -k start -DSSL 5 S nobody 19525 15474 0 75 0 - 3949 semtim 13:06 ? 00:00:00 /usr/local/apache/bin/httpd -k start -DSSL 5 S nobody 19526 15474 0 78 0 - 3947 - 13:06 ? 00:00:00 /usr/local/apache/bin/httpd -k start -DSSL 5 S nobody 19527 15474 0 78 0 - 3949 semtim 13:06 ? 00:00:00 /usr/local/apache/bin/httpd -k start -DSSL 

Источник

Get the Parent PID of a Child Process in Linux

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

1. Overview

In Linux, a process is a task that our machine is currently working on. The initial process is referred to as the parent process while any other process originating from a parent is known as a child process. Additionally, a process is identifiable through its process ID (PID). Therefore, it’s possible to get the parent process ID (PPID) of a child process.

Читайте также:  Acer nitro 5 драйвер linux

In this tutorial, we’ll focus ps command and learn how to get the parent process ID of a child process. The command contains a variety of options that we’ll discuss.

2. Using the ps Command

ps is a commonly used command for checking PIDs. It stands for “process status”. We’ll use it to display information related to the currently running processes.

First, we’ll pass the -e option to the ps command to list all the running processes:

$ ps -e PID TTY TIME CMD 1 ? 00:00:03 systemd 2 ? 00:00:00 kthreadd . 2987 ? 00:00:00 chrome 3010 ? 00:08:06 Xwayland

At this point, we get the process ID (PID), terminal line (TTY), run time (TIME), and command (CMD).

Next, we’ll filter out a process from the output above by its process name by using the pgrep command. pgrep lists the currently running processes according to the search criteria supplied to it.

Let’s pass the -l option to pgrep to list the process name as well as the process ID:

$ pgrep -l chrome 2961 chrome 2974 chrome_crashpad 

In this example, we passed the process name “chrome” as the search criteria and got its process ID.

At this point, we can use the process ID of the process we got above to get the parent process ID:

In this case, after passing the parent ID to the command above, we get its parent process ID which is 2129.

3. Conclusion

In this article, we discussed an easy and efficient way to get the parent process ID in Linux using the ps and pgrep commands.

We also discussed some of the options these commands provide. Both these commands contain the –help option if we need additional information.

Источник

How do I get the parent process ID of a given child process?

How to get parent PID from a given child’s PID? I know I can manually check it under /proc , I wonder if there is a smart/better way to achieve this in Ubuntu. Note the parent may or may not be killed. Thanks

4 Answers 4

How to get a parent PID (PPID) from a child’s process ID (PID) using the command-line

  • e.g. ps -o ppid= 2072 returns 2061 , which you can easily use in a script etc. ps -o ppid= -C foo gives the PPID of process with command foo . You can also use the old fashioned ps | grep : ps -eo ppid,comm | grep ‘[f]oo’ .
  • Fuller explanation: ps -f 2072 returns
UID PID PPID C STIME TTY STAT TIME CMD izx 2072 2061 0 07:16 ? S 0:00 /usr/lib/pulseaudio/pulse/gconf-helper
init(1)───pulseaudio(2061)───gconf-helper(2072)

Do you know of a way to make pstree show invocations (command + arguments) without splitting lines? My installation automatically splits lines if I add arguments, even if the result could easily fit on one line. The man page appears to say as much, but doesn’t give a reason. My workaround involves extracting PID’s from pstree and using ps -o args . on the results, but that has a bit of a smell.

Just something that surprised me: The space after ppid= but before the pid seems to be necessary. I got different results if I omitted it.

Using only shell variable to get parent PID :

if you need the command from this parent pid:

if you need the full command line (with all options):

Explanation

  • $PPID is defined by the shell, it’s the PID of the parent process
  • in /proc/ , you have some dirs with the PID of each processes. Then, if you cat /proc/$PPID/comm , you echo the command name of the PID

Check man proc

This is the best answer. procfs is a kernel API. execute a command and parse the output is a nonsense.

This is also potentially the most portable, PPID is defined as part of the POSIX standard so any POSIX compliant shell should be setting this appropriately. See pubs.opengroup.org/onlinepubs/9699919799/utilities/…

Using pstree by command name

Using pstree you can search by the child process name and get the Process ID (PID) along with the parents, grandparents and any children of the child process:

$ pstree -hp | grep sleep |-cron(763)---cron(795)---sh(839)---display-auto-br(841)---sleep(8414) 

In this case sleep is the child command and it’s PID is 8414 . It’s parent ID is 841 and is called display-auto-brightness . The grandparent is a shell ( sh ) with a process ID of 839 . The great-grandparent is cron with a process ID of 795 . The great-great-grandparent is also cron with a process ID of 763 .

If you want to search by Process ID of sleep instead of name you can use:

$ pstree -hp | grep 14653 |-cron(763)---cron(795)---sh(839)---display-auto-br(841)---sleep(14653) 

Notice the sleep process ID changed to 14653 . The parent (PID 841) sleeps for 1 minute, wakes up for a split second and then starts a new sleep command which gets a new process ID. This is another reason why searching for sleep is easier than searching by process ID.

To see a nested chain all the way back to boot process use the PID instead of name:

$ pstree -aps 8541 systemd,1 splash fastboot kaslr └─cron,763 -f └─cron,795 -f └─sh,839 -c /usr/local/bin/display-auto-brightness └─display-auto-br,841 /usr/local/bin/display-auto-brightness └─sleep,8541 60 

Note: Another minute has passed and the sleep command gets a new PID (8541).

Источник

How to find the Parent Process ID in Linux

In this quick tutorial, you are going to learn how to find the Parent Process ID in Linux from the command-line.

You will also learn how to find the children of a given Parent Process ID using the ps command.

We will also cover how to output a complete process tree of a parent process using the pstree command. A very helpful thing when you want to identify all of the children of a given parent, their IDs and names, which you may need when killing a process.

Parent Process ID Environment Variable

The parent process ID of your current context is exposed as an environment variable. To see the value you can echo it out.

For example, if you are SSH ‘d into a Linux server, the value of the $PPID environment variable will the process ID of the SSH process.

The environment variable only outputs the process ID. To see what the parent process is we can use the ps command with the $PPID environment variable.

Find the Parent Process ID of a Running Process

To determine the parent process of a specific process, we use the ps command.

The output only contain the parent process ID itself. Using the output from the ps command we can determine the name of the process.

Listing Child Processes of a Parent Process ID

With the parent process ID we can lookup all of the child process using of the parent using the pgrep command.

For a typical SSH parent the output will list the following processes

30797 -bash 30798 /usr/lib/openssh/sftp-server

Display Process Tree

Another useful utility is the pstree command. This command will output a tree of children processes of a parent process ID. For example, to display a tree of child processes from the current parent process, you would run the following command.

sshd─┬─bash───pstree └─sftp-server

From the example above, we can see that the sshd process is our current Parent Process ID (PPID), and that it has two child processes. The child process are bash and sftp-server .

We can also see that the bash process has its own child, which is the pstree command we just executed.

The pstree command can also output the process ID of each process in your tree. To output the process IDs you use the -p flag. Here’s an example of a process tree for an Apache webserver.

apache2(2029)─┬─apache2(9790) ├─apache2(9791) ├─apache2(9792) ├─apache2(9793) ├─apache2(9794) ├─apache2(9795) ├─apache2(9796) ├─apache2(9797) ├─apache2(9798) ├─apache2(9799) ├─apache2(9800) ├─apache2(9801) ├─apache2(9802) ├─apache2(9803) ├─apache2(9804) ├─apache2(9805) ├─apache2(9806) ├─apache2(9807) ├─apache2(9808) ├─apache2(9809) ├─apache2(9836) ├─apache2(9944) ├─apache2(15517) ├─apache2(21980) └─apache2(29107)

Источник

Оцените статью
Adblock
detector