Monday, 26 August 2019

Processes and job control in Linux

Listing process and information

ps command - display process status

- e                 - display all processes
-f                   - full format listing
-u username  - owned by user username
-p PID           - information for process with process ID

Some examples:
ps -A or ps -e                - display every active process
ps -ef or ps -eF              - display a full format listing
ps -x                              - display all processes owned by you
ps -fU user1                  - display all processes owned by user1
ps -fG apache                - list all processes owned by apache group
ps -eH                            - display a process tree
ps -e --forest                  - display a process tree

pstree - display processes in a tree format
top     - interactive process viewer
htop   - interactive process viewer (have to be installed from EPEL repository)

Background and foreground processes

Start background process
command &

Use jobs command to see list of background processes.
[root@rifle ~]# vi &
[1] 3669
[root@rifle ~]# vi &
[2] 3670
[1]+  Stopped                 vi
[root@rifle ~]# vi &
[3] 3671
[2]+  Stopped                 vi
[root@rifle ~]# jobs
[1]   Stopped                 vi
[2]-  Stopped                 vi
[3]+  Stopped                 vi

Plus indicates current job.
Current job also can be referred by  %% or %+
Previous job %-
[root@rifle ~]# jobs %%
[3]+  Stopped                 vi

Ctrl-z - suspend foreground process
Ctrl-c - kill the foreground process
bg [%number] - background a suspended process, without job number backgroung last stopped or suspended job.
fg [%number] - foreground a background process, without number foreground current job.
kill                  - kill process by job number or PID
kill [sig] pid   - send a signal to a process with PID
                         by default sig is TERM (15)
kill -9 pid       - if process does not terminated send a KILL signal to process
kill -l             - show all signals

No comments: