Showing posts with label process. Show all posts
Showing posts with label process. Show all posts

Tuesday, June 8, 2021

Putting a currently running process in the background

Sometimes a process seems like hanging, the terminal is irresponsive, you know it is running, but somehow you need to logout and need the process to continue running in the background.

This calls for a "kill" command with SIGSTOP and SIGCONT signals.

1. First, find the pid of the process

$ ps -ef | grep myprocess

1234

2. Once you have the pid, issue a kill with SIGSTOP signal to stop the process, assuming the process id is 1234.

$ sudo kill -SIGSTOP 1234

3. Issue a kill with SIGCONT to continue the process back in the background

$ sudo kill -SIGCONT 1234

4. You can check the backgrounded process using jobs command

$ jobs -l

[1]+ 1234 Stopped (signal)        myprocess

5. To get the process back to the foreground, just use fg command. %1 referring to the jobs number when you use the jobs command.

$ fg %1


You can refer to kill and signal man pages for more information.

Thursday, January 15, 2015

How to kill whole process group (parent + child process)

This is very easy, and all this while I have been using ps with grep and awk, just to get the parent and child process PID, and feed it to kill command to kill the whole lot of them. Now no more, that is why reading the man page is very beneficial ;). To kill the whole group process, please see below example.

Let's say I want to kill teamviewer, and it's child processes:

check what is teamviewer and its children's PID:


$ pstree -Gap 31458

teamviewerd,31458 -f

├─{teamviewerd},31460

├─{teamviewerd},31461

├─{teamviewerd},31462

├─{teamviewerd},31463

├─{teamviewerd},31464

├─{teamviewerd},31465

├─{teamviewerd},31466

├─{teamviewerd},31549

└─{teamviewerd},24892

run kill to the PID of parent, and put - sign in front of the PID, to signal the whole group killing:
$ kill -TERM -31458

no more PID 31458, with the children processes
$ pstree -Gap 31458

Tuesday, November 12, 2013

How to capture top output to a readable format

For this case, we use batch mode in top. From top's manpage:
Starts  top in 'Batch mode', which could be useful for sending output from top to other programs or to a file.  In this mode, top will not accept input and runs until the iterations limit you've set with the '-n' command-line option or until killed.

To use it is very easy, you must provide -b for batch mode, and -n for number of iteration that you desire. You can pipe it to less, tail, head or pipe it to a file, according to your need.

foo@thorium:~$ top -b -n 1 | head

top - 10:47:15 up 3 days, 23:52,  1 user,  load average: 2.01, 0.99, 0.57

Tasks: 140 total,   2 running, 136 sleeping,   0 stopped,   2 zombie

Cpu(s): 13.2%us,  4.9%sy,  0.0%ni, 81.6%id,  0.3%wa,  0.0%hi,  0.0%si,  0.0%st

Mem:   1025608k total,   865376k used,   160232k free,    80332k buffers

Swap:   522236k total,    66468k used,   455768k free,   273996k cached


  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                              
1192 root      20   0  122m  65m  10m R 13.8  6.6 559:31.01 Xorg                                                                                                  
19333 foo   20   0  234m  61m  13m S  9.8  6.2 626:59.69 nxplayer.bin                                                                                            
19440 foo   20   0  175m  14m 9612 S  7.9  1.5   0:44.78 lxterminal  

The above command will list 10 lines of top output in batch mode, and being set to iterate only once.



Thursday, May 17, 2012

Show memory map of a process

How to show memory map of a process? or how to check how much memory a process is consuming?
Use pmap.

How to use pmap:

  1. get a pid of a process you are interested in checking, in this example apache:
    [mainuser@serverone ~]$ ps -eaf | grep http 
    root      1759  4493  0 04:02 ?        00:00:00 /usr/sbin/httpd
    apache    1760  4493  0 04:02 ?        00:00:25 /usr/sbin/httpd
    apache    1761  4493  0 04:02 ?        00:00:23 /usr/sbin/httpd
    apache    1762  4493  0 04:02 ?        00:00:21 /usr/sbin/httpd
    apache    1763  4493  0 04:02 ?        00:00:18 /usr/sbin/httpd


  2. run the pmap command against the PID number:
    [mainuser@serverone ~]$ sudo pmap 1760 | tail 
    97406000     28K r--s-  /usr/lib/gconv/gconv-modules.cache
    97463000     16K rw---    [ anon ]
    97467000 524288K rw-s-  /tmp/apc.tE1RRo (deleted)
    b7467000  11096K r----  /usr/local/zend/lib/libicudata.so.38
    b7f3d000      4K rw---  /usr/local/zend/lib/libicudata.so.38
    b7f3e000     64K rw-s-  /dev/zero (deleted)
    b7f4e000    504K rw-s-  /dev/zero (deleted)
    b7fcc000     32K rw---    [ anon ]
    bfd74000    144K rwx--    [ stack ]
    total  2742152K
     
  3. The process, which is apache is consuming about 2.7GB of memory
That's all folks :)