I've been asked to give a talk on the command line interface for a mixed audience of Data Scientists and Engineers. Since the world is becoming ever more container based, I'm focussing on Linux.
Containers
Sometimes you need to diagnose things within the container. Jump into the container with:
docker exec -it CONTAINER_ID /bin/bash
To get the basic diagnostic tools mentioned below, you'll generally need to execute:
apt-get update # you need to run this first
apt-get install net-tools # gives you netstat
apt-get install iputils-ping # gives you ping
apt-get install procps # gives you ps
apt-get install lsof
Note these installations will all be gone next time you fire up the image as the underlying image does not change.
You can find out which module your favourite command belongs to by running something like this:
$ dpkg -S /bin/netstat
net-tools: /bin/netstat
Formatting
You can use regex expressions in grep with the -P switch. For example, let's search for lines that are strictly composed of two 5-letter words seperated by a space:
$ echo hello world | grep -P "\w{5}\s\w{5}$"
hello world
$ echo hello wordle | grep -P "\w{5}\s\w{5}$"
$
You can extract elements from a string with an arbitrary delimiter with awk. For example, this takes the first and sixth elements from a line of CSV:
$ echo this,is,a,line,of,csv | awk -F',' '{print $1 " " $6}'
this csv
$
To prettify output, use column like this:
$ echo "hello, world. I love you!
goodbye cruelest world! So sad" | column -t
hello, world. I love you!
goodbye cruelest world! So sad
$
To see what your filewall is dropping (useful when you've misconfigured a node), run:
$ sudo iptables -L -n -v -x
To see current network connections, run:
$ netstat -nap
You might want to pipe that to grep LISTEN to see what processes are listening and on which port. Very useful if something already has control of port 8080.
For threads, you can see what's going on by accessing the /proc directory. While threads are easy to see in Java (jstack), Python is a little more opaque, not least because the Global Interpretter Lock (GIL) only really allows one physical thread of execution (even if Python can allow logical threads). To utilise more processors, you must start a heavyweight thread (see "multiprocessing" here). Anyway, find the process ID you're interested in and run something like:
$ sudo cat /proc/YOUR_PROCESS_ID/stack
[<0>] do_wait+0x1cb/0x230
[<0>] kernel_wait4+0x89/0x130
[<0>] __do_sys_wait4+0x95/0xa0
[<0>] __x64_sys_wait4+0x1e/0x20
[<0>] do_syscall_64+0x57/0x190
[<0>] entry_SYSCALL_64_after_hwframe+0x44/0xa9
If you want to pin work to certain cores, use something like taskset. For example, if I wanted to run COMMAND on all but one of my 16 cores, I run:
taskset 0xFFFE COMMAND
This is very useful if some data munging is so intense it's bringing my system down. Using this, at least one thread is left for the OS.
Finally, vmstat gives you lots of information about the health of the box such as blocks being read/written from/to disk (bo/bi), the number of processes runnable (not necessarily running) and the number blcoked (r/b) and the number of context switches per second (cs)
No comments:
Post a Comment