ch10 processes

One of the main tasks of an operating system is to run processes on behalf of its users, services,
and applications. These are tracked in a process table inside the kernel, which keeps track of the
current state of each process in the system, and the system scheduler decides when each process
will be assigned to a CPU, and when it is taken off the CPU again

ubuntu2@ubuntu2:~/Documents$ ps -eaf
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Oct14 ?        00:00:04 /sbin/init
root         2     0  0 Oct14 ?        00:00:00 [kthreadd]
root         3     2  0 Oct14 ?        00:00:21 [ksoftirqd/0]


ps -eaf - is a common command to list all current processes
ps aux - is a common way to list all current processes on a BSD system

GNU/ Linux straddles both traditions, and the GNU implementation of ps accepts System V and
BSD options, as well as its own GNU options, such as --user. System V is probably the most
widely used format

C is a rough fi gure to represent the percentage of CPU time the process is responsible for consuming.

STIME is the time (or date) that the process was started. If associated with a terminal,
the terminal is reported under TTY

TIME is the amount of CPU time that the process has used, and CMD is the full name of the executable

The -F flag gives more detail; these columns are all described in the ps man page, but this adds the
SZ, RSS, and PSR fields. 

$ ps -F
UID        PID  PPID  C    SZ   RSS PSR STIME TTY          TIME CMD
ubuntu2   6867  6858  0  2010  5628   0 Oct15 pts/2    00:00:03 bash
ubuntu2  12349  6867  0  1308  2356   0 12:44 pts/2    00:00:00 ps -F

SZ is the number of pages (usually 4KB on x86) of the whole process; 
RSS is the amount of physical RAM (not including swapped-out data) the process holds. 
PSR is the ID of the current CPU that the process is running on.

 ps -Fp 6867
UID        PID  PPID  C    SZ   RSS PSR STIME TTY          TIME CMD
ubuntu2   6867  6858  0  2010  5628   0 Oct15 pts/2    00:00:03 bash

ps -fu - user

kill -9 `pgrep -x apache2`

Number Signal            Meaning
0            0                    Caught on exit from shell
1            SIGHUP        Clean, tidy up; reread configuration files and continue
2            SIGINT         Interrupt
3            SIGQUIT      Quit
6            SIGABRT     Abort
9            SIGKILL      Kill the process immediately
14          SIGALRM    Alarm clock
15          SIGTERM    Terminate cleanly

When shutting down a machine, an OS will normally call any shutdown scripts
registered, send a SIGTERM to any remaining processes, and finally send a
SIGKILL to any processes that are still running

searching for processes that include “ii” in their names
$ pgrep -l ii
8402 iimerge
8421 iigcc
8376 iimerge

$ pidof apache2
2510 2509 2508 2507 2502 1536 1535 1534 1533 1532 1495
$ pgrep -d’,’ apache2
1495,1532,1533,1534,1535,1536,2502,2507,2508,2509,2510
$

KILLALL
First, a word of warning: You should never consider running killall without the -e (exact match) option


killall -u www by itself will kill
every process run by the www user — again, exactly the same as pgrep.

killall -1 -u www sends a SIGHUP signal to the www processes,
telling them to restart

ps -eaf | egrep “(apache|gnome-terminal)”

The /proc pseudo-filesystem

The kernel’s process table, and the state of the processes in it, are available in the /proc pseudofilesystem,
identified by their PIDs. If you want information about PID 28741, then /proc/28741/
contains the relevant information. There is also a special symbolic link, called /proc/self: To any
process that refers to /proc/self, it will appear as a symbolic link to the running process. This is
not always easy to spot:

$ echo $$
2168
$ ls -ld /proc/self /proc/$$
dr-xr-xr-x 7 steve steve 0 Nov 12 16:06 /proc/2168
lrwxrwxrwx 1 root root 64 Nov 12 15:56 /proc/self -> 2171

The shell has a PID of 2168, and the shell passes the value of $$ to ls. In the ls program, /proc/self is /proc/2171 because ls is running as PID 2171. So these two are not the same number.

I/O Redirection

ls -l /proc/self/fd shows the fi les open by the ls
command itself; after these standard 0, 1, and 2, ls has also opened the directory /proc/5820/fd
to list it (5820 being the PID of ls itself), with a fi le descriptor of 3.
$ ls -l /proc/self/fd
total 0
lrwx------ 1 steve steve 64 Jan 27 21:34 0 -> /dev/pts/1
lrwx------ 1 steve steve 64 Jan 27 21:34 1 -> /dev/pts/1
lrwx------ 1 steve steve 64 Jan 27 21:34 2 -> /dev/pts/1
lr-x------ 1 steve steve 64 Jan 27 21:34 3 -> /proc/5820/fd
$
Because everything is a file, even the concepts of input and output are also files


ls -l /proc/self/fd > pro
ubuntu2@ubuntu2:~/Documents/linuxlessons$ cat pro
total 0
lrwx------ 1 ubuntu2 ubuntu2 64 Oct 18 09:42 0 -> /dev/pts/2
l-wx------ 1 ubuntu2 ubuntu2 64 Oct 18 09:42 1 -> /home/ubuntu2/Documents/linuxlessons/pro
lrwx------ 1 ubuntu2 ubuntu2 64 Oct 18 09:42 2 -> /dev/pts/2
lr-x------ 1 ubuntu2 ubuntu2 64 Oct 18 09:42 3 -> /proc/12930/fd
ubuntu2@ubuntu2:~/Documents/linuxlessons$ ls -l /proc/self/fd > pro
ubuntu2@ubuntu2:~/Documents/linuxlessons$ cat pro
total 0
lrwx------ 1 ubuntu2 ubuntu2 64 Oct 18 09:42 0 -> /dev/pts/2
l-wx------ 1 ubuntu2 ubuntu2 64 Oct 18 09:42 1 -> /home/ubuntu2/Documents/linuxlessons/pro
lrwx------ 1 ubuntu2 ubuntu2 64 Oct 18 09:42 2 -> /dev/pts/2
lr-x------ 1 ubuntu2 ubuntu2 64 Oct 18 09:42 3 -> /proc/12933/fd
ubuntu2@ubuntu2:~/Documents/linuxlessons$ 

----
$ ls -l /proc/self/fd /nosuchfile < /etc/hosts \
> > /tmp/ls-output.txt 2> /tmp/ls-err.txt
$ cat /tmp/ls-output.txt
/proc/self/fd:
total 0
lr-x------ 1 steve steve 64 Jan 27 22:52 0 -> /etc/hosts
l-wx------ 1 steve steve 64 Jan 27 22:52 1 -> /tmp/ls-output.txt
l-wx------ 1 steve steve 64 Jan 27 22:52 2 -> /tmp/ls-err.txt
lr-x------ 1 steve steve 64 Jan 27 22:52 3 -> /proc/2623/fd
$ cat /tmp/ls-err.txt
ls: cannot access /nosuchfile: No such file or directory


$ ls -l /proc/self/fd < pro 1>pro 2>pro
ubuntu2@ubuntu2:~/Documents/linuxlessons$ 
NO OUTPUT TO SCREEN

cat pro
total 0
lr-x------ 1 ubuntu2 ubuntu2 64 Oct 18 09:47 0 -> /home/ubuntu2/Documents/linuxlessons/pro
l-wx------ 1 ubuntu2 ubuntu2 64 Oct 18 09:47 1 -> /home/ubuntu2/Documents/linuxlessons/pro
l-wx------ 1 ubuntu2 ubuntu2 64 Oct 18 09:47 2 -> /home/ubuntu2/Documents/linuxlessons/pro
lr-x------ 1 ubuntu2 ubuntu2 64 Oct 18 09:47 3 -> /proc/12968/fd
ubuntu2@ubuntu2:~/Documents/linuxlessons$ 


Symbolic links normally have 777 permissions

EXEC
The exec builtin calls the underlying exec(3) system call. It has two main purposes. The fi rst is
the way in which the system call is most commonly used — to replace the currently running process
with a different process. That is, the shell itself will be replaced by a different program, be it another
shell, or any other program. When the exec’d program terminates, control is not returned to the
calling shell. The second use is to cause redirection to happen as a byproduct of the exec call.

------------------
When you run exec 3> /tmp/testing, a new file descriptor is created, pointing to /tmp/testing.
The file /tmp/testing is also created, and opened for writing.

$ ls -l /proc/$$/fd
total 0
lrwx------ 1 steve steve 64 Jan 31 11:56 0 -> /dev/pts/1
lrwx------ 1 steve steve 64 Jan 31 11:56 1 -> /dev/pts/1
lrwx------ 1 steve steve 64 Jan 31 11:56 2 -> /dev/pts/1
lrwx------ 1 steve steve 64 Jan 31 11:58 255 -> /dev/pts/1
l-wx------ 1 steve steve 64 Jan 31 11:56 3 -> /tmp/testing

$ echo hello >&3
$ cat /tmp/testing
hello

-----------------
A pipe connects two processes together,
generally attaching the standard output of one process to the standard input of another
------------------
It waits until all of the caller’s children have returned and then passes control back to the calling shell.
------------------
When running a long background process like the downloads in the previous section, it can be useful
to ensure that the job will not be terminated if the user logs off his or her session, or is logged
out because a network link has gone down. The nohup command runs processes in a wrapper,
which protects them from receiving signals that would otherwise cause them to terminate.
----------------
/proc/version is a read-only listing of the kernel version, including build details of how it was
compiled.
------------------
SysRq
If enabled, when the user presses the “magic” combination Ctrl+Alt+SysRq, along with one other
key to specify what the kernel is to do, the kernel can perform some of the most basic tasks available
to it — synchronize the filesystems, report on memory usage, or even reboot the system.
----------------------
Common SysRq Commands
Key Purpose
c Crash the system.
m Show the memory of the system.
h Show help.
r Set the console display to Raw mode.
s Synchronize all filesystems.
i Send a KILL signal to all processes (except init).
u Unmount all filesystems.
b Reboot the machine.
e Send a TERM signal to all processes (except init).

To safely reboot a hung system, this will set the console to raw mode, sync the filesystems, send a TERM to all processes, unmount all filesystems, and reboot the machine.

/proc/meminfo provides a fairly detailed overview of the current status of memory and the virtual
memory system.

The read-only file /proc/cpuinfo displays information about the processor(s) installed in the system.

/sys is a pseudo-filesystem very closely related to /proc; there is even some overlap, as /proc has
a /sys subdirectory that contains similar items

processes are one part of that
and the process table has been exposed in the /proc pseudo-filesystem for a long time

Process control includes managing the file descriptors of a process, which can be redirected in a
variety of ways to achieve different things.


Ctrl+R reverse search for entered command and repeat it again
-----
Execute a specific command from history
history | more
!4
-----
The /etc/skel directory contains files and directories that are automatically copied over to a new user's home directory when such user is created by the useradd program.
 
ls -a /etc/skel/
.  ..  .bash_logout  .bash_profile  .bashrc
-----
To view individual CPUs in the top command press 1
-----
$ps axuf print a process tree
 
$ps U oracle – view processes owned by a particular user
 
Display type of filesystem
$df –Tha
 
lsof shows open files in the system including network connection, devices and directories
 
view open files by particular user
$lsof –u username
 
If you like to view all the users who are using  a particular file use
$lsof /bin/vi
 
$vmstat displays memory, swap, IO, system and cpu performance info
 
Which program has initiated a specific network connection
$netstat –tap
 
Display RWA network statistics (packets discarded, received)
$netstat --statistics --raw
 
Nice command
Possible nice value range: -20 to 20.
A process that has a nice value of -20 is very high priority
 
$ps axl – display the nice value of all running process
$ps axl | grep pi$
 
Note: only root user can set a negative nice value
 
$nice --10 ./pi
 
$renice 16 –p 13245
 

Comments

Popular posts from this blog

HAproxy logging

teamcity Automatic Agent Start under Linux

NFS mount add in fstab _netdev instead of default | firewall-cmd --list-all