In this blogpost I'll show you some examples where strace can be useful for you. Most of it will be with PHP code but they're easy enough anyone can understand them.
Installing strace
Strace isn't installed by default on most distributions. To install, do a simple yum install strace when on CentOS/Red Hat or apt-get install strace on Debian/Ubuntu systems.
Getting output from strace
You can use strace in two different ways. You can attach it to an already running process or you can use it to start a custom application or script and follow all system calls from the very beginning.
In short, here's how it goes. If you want to start your application and troubleshoot it from beginning to end, you do this:
~# strace -f $command
~# strace -f php -q somefile.php
Or you want to attach to a running process, use this:
~# strace -f -p $pid
~# strace -f -p 8151
The -f parameter tells strace to follow any children or processes that are spawned/forked from the application.
Standard usage of strace
By default, strace will show you -all- system calls that your application or script is performing. That can get pretty overwhelming, but it's a good place to start. Take for instance the following simple script called 'test1.php'.
<?php
/* Simple buggy script */
for ($i = 0; $i < 5; $i++)
sleep(1);
?>
Read more: # Defining Chaos In Order
QR: