Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to investigate what a process is doing?

I know this can be checked from /proc/PID directory,

but don't know how to,

can any one show me the way?

like image 298
omg Avatar asked May 19 '09 19:05

omg


People also ask

How do you see what a PID is doing?

A PID is automatically assigned to each process when it is created. A process is nothing but running instance of a program and each process has a unique PID on a Unix-like system. The easiest way to find out if process is running is run ps aux command and grep process name.

How do you see what a process is doing windows?

Right-click or press-and-hold on a process listed in the Details tab of your Task Manager and click or tap Properties. The Properties window opens, providing you access to useful information about the selected process.

How do you show all processes being run by a particular user?

To see only the processes owned by a specific user on Linux run: ps -u {USERNAME} Search for a Linux process by name run: pgrep -u {USERNAME} {processName} Another option to list processes by name is to run either top -U {userName} or htop -u {userName} commands.


2 Answers

If you are looking for monitoring the system calls being made by a process, look into using strace.

like image 34
Gavin H Avatar answered Nov 06 '22 19:11

Gavin H


Usually strace is the answer to this question. The simplest method is to run a command using strace directly, for example:

wichert@fog:~$ strace ls
execve("/bin/ls", ["ls"], [/* 16 vars */]) = 0
brk(0)                                  = 0x9fa8000
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
mmap2(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7f0a000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)

This does not work for already running processes such as PHP. Luckily you can also attach strace to an existing process using the -p parameter. For example:

wichert@fog:~$ strace -p 3761
Process 3761 attached - interrupt to quit
select(16, [5 7 8], NULL, [5 7 8], {0, 580000}) = 0 (Timeout)
alarm(0)                                = 62
rt_sigprocmask(SIG_BLOCK, [ALRM], [], 8) = 0
rt_sigaction(SIGALRM, {SIG_DFL}, {0x809a270, [], 0}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0

For daemons which spawn other processes you may need to use the -f parameter as well.

Besides the always useful strace you may also want to look at ltrace. ltrace is similar to strace, but it shows library calls instead of system calls. An example:

[one;~]-6> ltrace ls
__libc_start_main(0x804e5f0, 1, 0xbfdb7254, 0x8059a10, 0x8059a00 <unfinished ...>
setlocale(6, "")                                                                                 = "LC_CTYPE=en_GB.UTF-8;LC_NUMERIC="...
bindtextdomain("coreutils", "/usr/share/locale")                                                 = "/usr/share/locale"
textdomain("coreutils")                                                                          = "coreutils"
__cxa_atexit(0x8051860, 0, 0, 0xb7f65ff4, 0xbfdb71b8)                                            = 0
isatty(1)                                                                                        = 1
getenv("QUOTING_STYLE")                                                                          = NULL

Please note that you will also see a fair amount of internal libc calls as well, so the output could be more verbose than you expect.

like image 117
Wichert Akkerman Avatar answered Nov 06 '22 19:11

Wichert Akkerman