Process tree on linux

How To Show a Processes Tree in Unix

Showing your processes in a hierarchical list is very useful for confirming the relationship between every process running on your system. Today I’d like to show you how you can get tree-like processes lists using various commands.

Showing processes tree with ptree

In Solaris, there’s quite a few commands which make the life of any system administrator much easier, they’re the process commands (p-commands). One of them which I particularly like is the ptree command which shows you a list of processes. As you run the command, you get a hierarchical list of all the processes running on your Solaris system, along with process IDs (PIDs). To me, this is a very useful command, because it shows you how exactly each process relates to others in your system. Here’s a fragment of the ptree output:

bash-3.00$ ptree 7 /lib/svc/bin/svc.startd 250 /usr/lib/saf/sac -t 300 268 /usr/lib/saf/ttymon 260 -sh 5026 -csh 9 /lib/svc/bin/svc.configd 107 /usr/lib/sysevent/syseventd 136 /usr/lib/picl/picld 140 /usr/lib/crypto/kcfd 159 /usr/sbin/nscd 227 /usr/sbin/rpcbind 234 /usr/lib/nfs/statd 235 /usr/sbin/keyserv 236 /usr/lib/netsvc/yp/ypserv -d 237 rpc.nisd_resolv -F -C 8 -p 1073741824 -t udp 241 /usr/lib/nfs/lockd 247 /usr/lib/netsvc/yp/ypbind 263 /usr/lib/utmpd 286 /usr/sadm/lib/smc/bin/smcboot 287 /usr/sadm/lib/smc/bin/smcboot 288 /usr/sadm/lib/smc/bin/smcboot

Processes tree with pstree

In most Linux distributions, you can find a pstree command, very similar to ptree. That’s how you may use it (-p is an option to show PIDs and -l uses long output format):

ubuntu$ pstree -pl init(1)─┬─NetworkManager(5427) ├─NetworkManagerD(5441) ├─acpid(5210) ├─apache2(6966)─┬─apache2(2890) │ ├─apache2(2893) │ ├─apache2(7163) │ ├─apache2(7165) │ ├─apache2(7166) │ ├─apache2(7167) │ └─apache2(7168) ├─atd(6369) ├─avahi-daemon(5658)───avahi-daemon(5659) ├─bonobo-activati(7816)───(7817) . 

Showing processes tree with ps –forest

ps command found in Linux has a –forest option, which shows you a tree structure of processes. The best in my experience is to use it like this:

ubuntu$ ps -aef --forest UID PID PPID C STIME TTY TIME CMD . 107 5473 1 0 10037 4600 0 Apr28 ? 00:00:02 /usr/sbin/hald root 5538 5473 0 5511 1288 0 Apr28 ? 00:00:00 \_ hald-runner root 5551 5538 0 6038 1284 0 Apr28 ? 00:00:01 \_ hald-addon-input: Listening on /dev/input 107 5566 5538 0 4167 992 1 Apr28 ? 00:00:00 \_ hald-addon-acpi: listening on acpid socke root 5600 5538 0 6038 1272 1 Apr28 ? 00:00:15 \_ hald-addon-storage: polling /dev/scd0 (ev root 5476 1 0 10272 2532 0 Apr28 ? 00:00:00 /usr/sbin/console-kit-daemon root 5627 1 0 12728 1176 1 Apr28 ? 00:00:00 /usr/sbin/sshd root 9151 5627 0 17536 3032 0 10:53 ? 00:00:00 \_ sshd: greys [priv] greys 9162 9151 0 17538 1892 1 10:54 ? 00:00:00 \_ sshd: [email protected]/3 greys 9168 9162 0 5231 3820 1 10:54 pts/3 00:00:00 \_ -bash greys 9584 9168 0 3802 1124 0 11:27 pts/3 00:00:00 \_ ps -aeF --forest

This output is for demonstration purpose only, and so I’ve taken the first lines of the output out because they weren’t serving the purpose of this example very well. For thins fragment of the output you can see how you get all the vital information about each process. I really like this way of running the ps command. That’s it for today! Do you know any other neat way of looking at processes tree? Let me know!

Читайте также:  Unlock bootloader xiaomi linux

See Also

Источник

Process tree on linux

NAME

pstree - display a tree of processes

SYNOPSIS

pstree [-a, --arguments] [-c, --compact] [-h, --highlight-all, -Hpid, --highlight-pid pid] [-g] --show-pgids] [-l, --long] [-n, --numeric-sort] [-N, --ns-sortns [-p, --show-pids] [-s, --show-parents] [-S, --ns-changes] [-u, --uid-changes] [-Z, --security-context] [-A, --ascii, -G, --vt100, -U, --unicode] [pid, user] pstree -V, --version

DESCRIPTION

pstree shows running processes as a tree. The tree is rooted at either pid or init if pid is omitted. If a user name is specified, all process trees rooted at processes owned by that user are shown. pstree visually merges identical branches by putting them in square brackets and prefixing them with the repetition count, e.g. init-+-getty |-getty |-getty `-getty becomes init---4*[getty] Child threads of a process are found under the parent process and are shown with the process name in curly braces, e.g. icecast2---13*[] If pstree is called as pstree.x11 then it will prompt the user at the end of the line to press return and will not return until that has happened. This is useful for when pstree is run in a xterminal. Certain kernel or mount parameters, such as the hidepid option for procfs, will hide information for some processes. In these situations pstree will attempt to build the tree without this information, showing process names as question marks.

OPTIONS

-a Show command line arguments. If the command line of a process is swapped out, that process is shown in parentheses. -a implicitly disables compaction for processes but not threads. -A Use ASCII characters to draw the tree. -c Disable compaction of identical subtrees. By default, subtrees are compacted whenever possible. -G Use VT100 line drawing characters. -h Highlight the current process and its ancestors. This is a no-op if the terminal doesn't support highlighting or if neither the current process nor any of its ancestors are in the subtree being shown. -H Like -h, but highlight the specified process instead. Unlike with -h, pstree fails when using -H if highlighting is not available. -g Show PGIDs. Process Group IDs are shown as decimal numbers in parentheses after each process name. -g implicitly disables compaction. If both PIDs and PGIDs are displayed then PIDs are shown first. -l Display long lines. By default, lines are truncated to either the COLUMNS environment variable or the display width. If neither of these methods work, the default of 132 columns is used. -n Sort processes with the same ancestor by PID instead of by name. (Numeric sort.) -N Show individual trees for each namespace of the type specified. The available types are: ipc, mnt, net, pid, user, uts. Regular users don't have access to other users' processes information, so the output will be limited. -p Show PIDs. PIDs are shown as decimal numbers in parentheses after each process name. -p implicitly disables compaction. -s Show parent processes of the specified process. -S Show namespaces transitions. Like -N, the output is limited when running as a regular user. -u Show uid transitions. Whenever the uid of a process differs from the uid of its parent, the new uid is shown in parentheses after the process name. -U Use UTF-8 (Unicode) line drawing characters. Under Linux 1.1-54 and above, UTF-8 mode is entered on the console with echo -e ' 33%8' and left with echo -e ' 33%@' -V Display version information. -Z (SELinux) Show security context for each process. This flag will only work if pstree is compilied with SELinux support.

FILES

/proc location of the proc file system

BUGS

Some character sets may be incompatible with the VT100 characters.

SEE ALSO

Источник

Читайте также:  Настройка active directory astra linux

How to view the process tree in Linux

In Linux, every process originates from the init process, with the exception of init itself. The kernel initiates the init process during system boot, which then generates and manages other processes. These processes form a hierarchical parent-child relationship, resembling a tree-like structure.

You can display this process tree in Linux, with child processes grouped under their respective parent processes, by using the ps and pstree commands in the terminal.

Steps to show process tree in Linux:

$ ps -x PID TTY STAT TIME COMMAND 1080 ? Ss 0:00 /lib/systemd/systemd --user 1081 ? S 0:00 (sd-pam) 1092 tty1 S+ 0:00 -bash 1175 ? S 0:00 sshd: user@pts/0 1176 pts/0 Ss 0:00 -bash 1424 pts/0 R+ 0:00 ps -x
$ ps -x --forest PID TTY STAT TIME COMMAND 1175 ? S 0:00 sshd: user@pts/0 1176 pts/0 Ss 0:00 \_ -bash 1436 pts/0 R+ 0:00 \_ ps -x --forest 1092 tty1 S+ 0:00 -bash 1080 ? Ss 0:00 /lib/systemd/systemd --user 1081 ? S 0:00 \_ (sd-pam)
$ sudo apt update && sudo apt install --assume-yes psmisc #Ubuntu and Debian $ sudo yum install --assumeyes pstree #CentOS and Red Hat

$ pstree systemd─┬─VGAuthService ├─accounts-daemon───2*[] ├─atd ├─cron ├─dbus-daemon ├─login───bash ├─multipathd───6*[] ├─networkd-dispat ├─packagekitd───2*[] ├─polkitd───2*[] ├─rsyslogd───3*[] ├─snapd───8*[] ├─sshd───sshd───sshd───bash───pstree ├─systemd───(sd-pam) ├─systemd-journal ├─systemd-logind ├─systemd-network ├─systemd-resolve ├─systemd-timesyn─── ├─systemd-udevd ├─unattended-upgr─── └─vmtoolsd───

Usage: pstree [-acglpsStuZ] [ -h | -H PID ] [ -n | -N type ] [ -A | -G | -U ] [ PID | USER ] pstree -V Display a tree of processes. -a, --arguments show command line arguments -A, --ascii use ASCII line drawing characters -c, --compact don't compact identical subtrees -h, --highlight-all highlight current process and its ancestors -H PID, --highlight-pid=PID highlight this process and its ancestors -g, --show-pgids show process group ids; implies -c -G, --vt100 use VT100 line drawing characters -l, --long don't truncate long lines -n, --numeric-sort sort output by PID -N type, --ns-sort=type sort by namespace type (cgroup, ipc, mnt, net, pid, user, uts) -p, --show-pids show PIDs; implies -c -s, --show-parents show parents of the selected process -S, --ns-changes show namespace transitions -t, --thread-names show full thread names -T, --hide-threads hide threads, show only processes -u, --uid-changes show uid transitions -U, --unicode use UTF-8 (Unicode) line drawing characters -V, --version display version information -Z, --security-context show SELinux security contexts PID start at this PID; default is 1 (init) USER show only trees rooted at processes of this user

Источник

Читайте также:  Запуск ssh astra linux

How to Show Process Tree in Linux

Here are multiple commands that you can use to show the process tree in the Linux command line.

So you used the killall command and it killed the parent process making your hours of work at waste?

Well, I went through the same so it is always a better idea to check the parent processes, and listing the ongoing processes in tree manner is a good idea.

Use the ps command to show the process tree

The ps command in Linux is used to find ongoing processes in Linux and it also avails you to print the exact info in a tree manner.

You can use the —tree option to show the process tree with the ps command:

use ps command in linux

Use the pstree command to show the process tree

Personally, this is what I prefer to have as even without any options, it works fine.

But your distro may not have it pre-installed and if you’re on Ubuntu-based distros, the given command should do it:

Now, you can simply use the pstree command and it should give you the following output:

use pstree command in linux

Similarly, you can use -p option to get process IDs:

show process id in pstree command

Use the tree command to show the process tree

While the tree command is mainly used to list files recursively, you can look at /proc to get the process tree.

But it requires manual installation as it does not come pre-installed and if you’re on an Ubuntu-based distro, the following command should do it:

Now, you can use the given command to show the process tree:

use tree command to show process tree

It showed 23665 directories and 435044 files which bloated my terminal window and you can limit the info using -d option as it will only show directories:

limit tree command output to directories only

And if you want to have full pathnames, you can use -f option:

get full pathnames in tree command

Use the htop utility to show the process tree

htop is similar to the top command. And in my opinion, this is arguably the most interactive way of checking ongoing processes in the terminal.

But what if I tell you that it is also capable of showing a process tree too? But before that, let’s head over to the installation part.

It does not come pre-installed and if you are on an Ubuntu-based distro, the given command should do it:

Once you are done with the installation, use the given command to start htop:

use htop in linux to show ongoing processes

Now, you just have to press F5 and the ongoing process will be shown to you in tree format:

show process tree in linux using htop

Wrapping Up

One practical example of displaying the process tree is in checking whether your system uses systemd or not.

This was my take on how you can show the process tree using various utilities but there is a lot more you can do with those utilities. More on them later.

Источник

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