April 29, 2017

Relevant Linux Features: Go Forth And Explore

Go Forth And Explore

You have just lightly scratched the surface… being fresh from a pedicure. You haven't touched any of the items below. The good news is that if you don't already know them, chances are you will not need them… at least not right away:

2>&1 Redirect STDERR and STDOUT to the same place
Metacharacter A character that, when unquoted, separates words.  One of the following:
              |  & ; ( ) < > space tab
Operators ==, !=, =~, …
control operator A token that performs a control function.  It is one of the following symbols:
              || & && ; ;; ( ) | |& <newline>
Process Status 'ps' command, e.g. 'ps -A'
Shell scripting
Compound commands && - e.g. apt-get update && apt-get install -y openssh-server
List - (list)
{ list; }
See "man bash"
xargs build and execute command lines from standard input
docker ps -aq | xargs docker rm -f
vi, sed, nawk Various editors and text processing tools

Relevant Linux Features: UNIX Domain Socket

UNIX Domain Socket

UNIX domain socket, also known as IPC (inter-process communication) socket is a data communications endpoint for exchanging data between processes executing on the same host.

Unlike a regular shell pipeline, or pipe (|), which allows communication between command and in only one direction, the UNIX domain socket is bi-directional, i.e. it enables a two-way communications channel.

Similar concepts:
  • TCP Sockets
    • enables bi-directional communication channel between two endpoints over the Internet
  • Pipes
    • one-way communication channel between commands on the local host
    • a sequence of processes chained together by their standard streams, where the standard output (stdout) of one process is fed to the next process through its standard input (stdin)
  • FIFO
    • First In First Out
    • Also known as a named pipe
    • unidirectional communication channel between two processes on the local host
    • Can be accessed by two processes, one to write data, the other to read data
    • Can be created and named by commands: mkfifo or mknod

UNIX domain socket allows processes on the same host can communicate.
All communication occurs entirely within the operating system kernel
Unix domain sockets use the file system as their address name space.
A UNIX domain socket is known by a pathname where the client and server agree on the pathname used for the communication

The /var/run/docker.sock is an implementation of the UNIX domain socket, used by Docker. The Docker daemon listens on it by default and it enables communication between the Docker daemon and containers.


April 25, 2017

Relevant Linux Features: Exit Status

Exit Status

On Unix and Linux systems, every command ends with an exit status (also known as return status or exit code). Exit status is an integer value ranging from 0 to 255.

By default, a command that ends "successfully" has an exit status of zero, 0.
A command that ends with a "failure" has a non-zero (1 - 255) exit status.

Note: Success and failure, with respect to exit status is relative. By default if a command does what it's expected to do, on exit it sets a zero, 0, exit status. E.g. If the directory /var/log/apt exists, the command ls /var/log/apt will end successfully and result in an exit status of 0. However if the argument, in this case a directory, is not accessible the ls command will "fail" and leave a non-zero exit status:

By convention, success results in an exit status of zero, however commands are generally free to decide what non-zero integer, between 1 and 255 to use. In the above example, ls uses an exit status of 2 to reflect that a directory is not accessible. And docker chooses an exit status of 125 to reflect that the image is not accessible from the repository.

Commands are free to choose which value to use to reflect success or failure. However there are some reserved value that have special meaning, defined here: http://www.tldp.org/LDP/abs/html/exitcodes.html

A command writes its exit status into the ? shell variable, accessible via $?. This variable can hold one value at a time, as such it is overwritten when the next command exits.

To read the previous command's exit status, use the command, echo $?.


0 the exit status of a command on success
1 - 255 the exit status of a command on failure
? holds the exit status of the last command executed
$? reads the exit status of the last command executed


Relevant Linux Features: Control Operator

Control Operator

A Control Operator is a token that performs a control function.  It is one of the following symbols: || & && ; ;; ( ) | |& <newline>. We will focus on only && and || control operators in this article.

On occasion you might need to group Docker commands. Let's see a few ways to do this in Linux with three of the control operators.

Control operators Description
; Semicolon - delimits commands in a sequence
Used to run multiple commands one after the other
Similar to hitting ENTER after each command:

$ docker run --rm -it debian bash -c "ls /var; sleep 1; ls /"

Run the container and execute the three commands one after the other, separated by ; (semicolon)
&& AND - runs commands conditionally, on success
has the form A && B where B is run if, and only if A succeeds, i.e. if A returns an exit status of zero.

$ apt-get update && apt-get install -y openssh-server

This runs the second command, apt-get install -y openssh-server, IF AND ONLY IF the first command, apt-get update succeeded.
|| OR - runs command conditionally, on failure
has the form A || B where B is run if, and only if A fails, i.e. if A returns a non-zero exit status

This runs the second command, IF AND ONLY IF, the first command fails. In this example, since the first command, false will always fail, i.e. return a non-zero exit status, the second command, true, runs and sets the zero exit status.