Tuesday, February 25, 2020

Linux processes - attaching and inspecting

Inspecting processes is interesting topic for me in general. Whether with gdb or with command line tools, let's take a look how we can inspect what is going on inside a linux process: 

At one point, when you want, you can enjoy the full Ubuntu admin course.


For example in one terminal tab we can start a process such as ping,
then in another we can see its process identificator number(PID) with sudo ps -ax
and based on the information we can attach to the running process using strace: sudo strace -p PID (a nice and more verbose variant for process tracking offers: sudo strace -p12345 -s9999 -e write)

Another useful application is reptyr, which tries attach to runnig process and to transfer its output the current terminal we are using:
installation:
apt install reptyr

in order for reptyr to work you need to expand the scope of ptrace :
# echo 0 > /proc/sys/kernel/yama/ptrace_scope
then when you have the process ID you may try with the following options to attach to a process:
reptyr PID -T -L
L is to enable capturing child processes
T is for tty stealing

Keep in mind reptyr is just attaching to process and not getting its ownership (i.e. becoming its parent), so when you close the original parent terminal the captured process will halt. The solution in this case is to disown the process in question and is done in two steps:
1. the process should be listed as a task, and it is a fact that a task is associated with a particular terminal(tty). So first we run the process as a task with: bg, Ctrl+z, or &.
2. then we can run disown
Alternatively we can in first place use: nohup command name &
(
& will run the command as a child process to the current bash session. When you exit the session, all child processes will be killed.
nohup + &: when the session ends, the parent of the child process will be changed to 1 (the "init") process, thus preserving the child from being killed.
)
3. Now you can capture the process to your terminal using reptyr and even if you close the original terminal the process will not stop.

In the second example let's say you have running download in one session and it is too long, and you have to disconnect and go home. How to save the situation ?
1) Just login from another session and run the screen command.
2) From the second session: get the download PID, and use reptyr to attach it to the current session.
3) Detach screen with ctrl+a+d or just type exit
4) Next time, just re-login using ssh and make the session active (attached) with: screen -Dr

Hints on screen:
When you run the command, it creates creates new screen session/socket. Then you can use: Ctrl+a+d to detach from the current screen
to attach to already existing session use: screen -Dr
and to re-attach to already attached screen: screen -x
To delete screen session, you need to reattach and then Ctrl+a+k / or just type: exit

Congratulations!

No comments:

Subscribe To My Channel for updates