Transcript
Page 1: Command Line Tools Every PHP Developer Should Know About

Command Line Tools Every PHP Developer Should Know About

By Andrew Kandels

Page 2: Command Line Tools Every PHP Developer Should Know About

File Operations

lsof List open files (think of “ls open files”)

tail -f Output appended data as the file grows

Filter output with egrep: tail –f /log/file | egrep “ip-address”

Page 3: Command Line Tools Every PHP Developer Should Know About

straceTraces the name of every system call, its arguments and return value.

Trick for strace’ing web requests:

strace –o /tmp/webtraffic.log –f /etc/init.d/apache2 start &tail –f /tmp/webtraffic.log | egrep “No such file”

Page 4: Command Line Tools Every PHP Developer Should Know About

Bash Command History

/etc/profile Improvements

export HISTCONTROL=erasedupsexport HISTSIZE=10000shopt -s histappend

!! Run last command!string Run last command that matches string!$ Last argument from last command (bang-bling)CTRL-R Search/auto-complete command history$? Exit code from last command (0 = win)

Page 5: Command Line Tools Every PHP Developer Should Know About

Get Some Stats

Step #1: Is there a problem? Get the CPU load.

1m 5m 15m

mpstat CPU statsiostat File I/O statstop/htop Process/various other statscat /proc/loadavg Load averagesnetstat Network statsfree -m Free memory/swap stats

netstat –tlp Get the listening TCP sockets (what ports are open)

Page 6: Command Line Tools Every PHP Developer Should Know About

screen

Recoverable screen manager that emulates a VT100/ANSI terminal.

CTRL-a <command>

c Create new window“ List windows

If you lose your connection, use screen –list to list previous screen sessions, and screen –r # to recover a session:

Page 7: Command Line Tools Every PHP Developer Should Know About

processes

ps –aux List all processespstree –p List processes in a tree (parent/child)htop Interactively list all processes (with tree option)apachetop View Apache connections interactivelymytop View MySQL queries interactivelyiotop View I/O operations interactively

Page 8: Command Line Tools Every PHP Developer Should Know About

There’s a Command For That

strings <file> List readable strings in a binary (or any) file

file <file> What kind of file is it?

stat <file> atime, ctime, mtime, size, type, inode

dig <url> Resolve a domain name into an IP address (and measure)

arp –a View the local ARP cache

cat /proc/cpuinfo How many CPUs do I have?

free –m How much memory am I using?

df –h How much disk space am I using?

du –ch /path How much space is a folder using?

echo $((2*4)) Quick math

Page 9: Command Line Tools Every PHP Developer Should Know About

awk

Pattern scanning and text processing language (it’s like pixie dust in vim)

NR = Number or Records read so far NF = Number of Fields in current record FS = the Field Separator RS = the Record Separator BEGIN = a pattern that's only true before processing any input END = a pattern that's only true after processing all input.

sedStream editor to replace text in file(s), delete lines and so much more.

In-place replace 2010 copyrights with 2011 in all PHP files:

find . –type f –name “*.php” –exec sed -I’’ ‘s/Copyright 2010/Copyright 2011/g’ \;

Page 10: Command Line Tools Every PHP Developer Should Know About

httperf

http://www.hpl.hp.com/research/linux/httperf/

Stress test your server by simulating web traffic.

Beat the crap out of your server:

for x in {1..10}; do httperf --hog –server=domain.com –wsess=250,5,10 --burst-length=5 --rate 100 --timeout 5 --uri /path/to/file echo “Run #$x… “ sleep 1done

250 – Number of clients100 – Requests per second

Test load (server side):

while [ true ]; do uptime; sleep 5; done