Geek on the road

open source, open knowledge

Strace to the Rescue

| Comments

Today in the IRC somebody asked which would be the best way to know if a process already exists in the system. The choice was between using test -d /proc/PID or kill -0 PID.

Both of them do the job, the question here, we want to use the best one. Suddenly I remembered an option that comes with strace and lets you query the number of syscalls for a given trace. Besides we can order based on the number of syscalls.

Liquid error: undefined method `Py_IsInitialized’ for RubyPython::Python:Module

On the other hand, the second command’s output:

Liquid error: undefined method `Py_IsInitialized’ for RubyPython::Python:Module

The column calls helps to know the number of performed syscalls. Using test -d /proc/PID gives a better performance due to a minor number of syscalls.

I really like strace, is a tool you had better know, here I got syscalls statistics, but you can trace syscalls either specific or a bunch of them, follow forked processes and much more, this is only a simple example. I hope it helps.