Process Management

Linux Admin – Process Management ”; Previous Next Following are the common commands used with Process Management–bg, fg, nohup, ps, pstree, top, kill, killall, free, uptime, nice. Work with Processes Quick Note − Process PID in Linux In Linux every running process is given a PID or Process ID Number. This PID is how CentOS identifies a particular process. As we have discussed, systemd is the first process started and given a PID of 1 in CentOS. Pgrep is used to get Linux PID for a given process name. [root@CentOS]# pgrep systemd 1 [root@CentOS]# As seen, the pgrep command returns the current PID of systemd. Basic CentOS Process and Job Management in CentOS When working with processes in Linux it is important to know how basic foregrounding and backgrounding processes is performed at the command line. fg − Bringsthe process to the foreground bg − Movesthe process to the background jobs − List of the current processes attached to the shell ctrl+z − Control + z key combination to sleep the current process & − Startsthe process in the background Let”s start using the shell command sleep. sleep will simply do as it is named, sleep for a defined period of time − sleep. [root@CentOS ~]$ jobs [root@CentOS ~]$ sleep 10 & [1] 12454 [root@CentOS ~]$ sleep 20 & [2] 12479 [root@CentOS ~]$ jobs [1]- Running sleep 10 & [2]+ Running sleep 20 & [cnetos@CentOS ~]$ Now, let”s bring the first job to the foreground − [root@CentOS ~]$ fg 1 sleep 10 If you are following along, you”ll notice the foreground job is stuck in your shell. Now, let”s put the process to sleep, then re-enable it in the background. Hit control+z Type: bg 1, sending the first job into the background and starting it. [root@CentOS ~]$ fg 1 sleep 20 ^Z [1]+ Stopped sleep 20 [root@CentOS ~]$ bg 1 [1]+ sleep 20 & [root@CentOS ~]$ nohup When working from a shell or terminal, it is worth noting that by default all the processes and jobs attached to the shell will terminate when the shell is closed or the user logs out. When using nohup the process will continue to run if the user logs out or closes the shell to which the process is attached. [root@CentOS]# nohup ping www.google.com & [1] 27299 nohup: ignoring input and appending output to ‘nohup.out’ [root@CentOS]# pgrep ping 27299 [root@CentOS]# kill -KILL `pgrep ping` [1]+ Killed nohup ping www.google.com [root@CentOS rdc]# cat nohup.out PING www.google.com (216.58.193.68) 56(84) bytes of data. 64 bytes from sea15s07-in-f4.1e100.net (216.58.193.68): icmp_seq = 1 ttl = 128 time = 51.6 ms 64 bytes from sea15s07-in-f4.1e100.net (216.58.193.68): icmp_seq = 2 ttl = 128 time = 54.2 ms 64 bytes from sea15s07-in-f4.1e100.net (216.58.193.68): icmp_seq = 3 ttl = 128 time = 52.7 ms ps Command The ps command is commonly used by administrators to investigate snapshots of a specific process. ps is commonly used with grep to filter out a specific process to analyze. [root@CentOS ~]$ ps axw | grep python 762 ? Ssl 0:01 /usr/bin/python -Es /usr/sbin/firewalld –nofork -nopid 1296 ? Ssl 0:00 /usr/bin/python -Es /usr/sbin/tuned -l -P 15550 pts/0 S+ 0:00 grep –color=auto python In the above command, we see all the processes using the python interpreter. Also included with the results were our grep command, looking for the string python. Following are the most common command line switches used with ps. Switch Action a Excludes constraints of only the reporting processes for the current user x Shows processes not attached to a tty or shell w Formats wide output display of the output e Shows environment after the command -e Selects all processes -o User-defined formatted output -u Shows all processes by a specific user -C Shows all processes by name or process id –sort Sorts the processes by definition To see all processes in use by the nobody user − [root@CentOS ~]$ ps -u nobody PID TTY TIME CMD 1853 ? 00:00:00 dnsmasq [root@CentOS ~]$ To see all information about the firewalld process − [root@CentOS ~]$ ps -wl -C firewalld F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 0 S 0 762 1 0 80 0 – 81786 poll_s ? 00:00:01 firewalld [root@CentOS ~]$ Let”s see which processes are consuming the most memory − [root@CentOS ~]$ ps aux –sort=-pmem | head -10 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND cnetos 6130 0.7 5.7 1344512 108364 ? Sl 02:16 0:29 /usr/bin/gnome-shell cnetos 6449 0.0 3.4 1375872 64440 ? Sl 02:16 0:00 /usr/libexec/evolution-calendar-factory root 5404 0.6 2.1 190256 39920 tty1 Ssl+ 02:15 0:27 /usr/bin/Xorg :0 -background none -noreset -audit 4 -verbose -auth /run/gdm/auth-for-gdm-iDefCt/database -seat seat0 -nolisten tcp vt1 cnetos 6296 0.0 1.7 1081944 32136 ? Sl 02:16 0:00 /usr/libexec/evolution/3.12/evolution-alarm-notify cnetos 6350 0.0 1.5 560728 29844 ? Sl 02:16 0:01 /usr/bin/prlsga cnetos 6158 0.0 1.4 1026956 28004 ? Sl 02:16 0:00 /usr/libexec/gnome-shell-calendar-server cnetos 6169 0.0 1.4 1120028 27576 ? Sl 02:16 0:00 /usr/libexec/evolution-source-registry root 762 0.0 1.4 327144 26724 ? Ssl 02:09 0:01 /usr/bin/python -Es /usr/sbin/firewalld –nofork –nopid cnetos 6026 0.0 1.4 1090832 26376 ? Sl 02:16 0:00 /usr/libexec/gnome-settings-daemon [root@CentOS ~]$ See