A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Docker basics

# Download an image and spin up a container, run it, connect to it

docker pull [name pf image] # download an image

docker run -d -p 80:80 --name pintail-whoami pintailai/pintail-whoami:0.0.1 # download + run

docker run --rm -v /path/on/machine:/app/out image-name:stable params to app

# List running containers

docker ps # show running containers

docker ps | cut -c-$(tput cols) # show running containers without wrapping to the next line

docker ps -q # show just the IDs of the running containers

docker ps -q | head -1 # show ID of most recently started container (how to sort)

# Run a shell on a container

docker run -i -t [Container ID] /bin/bash

# Connect to a shell on an already-runnning docker container.
# (Shell: Use /bin/bash for ubuntu or /bin/ash for alpine)

docker exec -it [Container ID] [shell]

docker exec -it `docker ps -q | head -1` /bin/bash # run shell on the most recently started container

# Copy a file off

docker cp <container>:<src-path> <local-dest-path>

# Delete all stopped containers and images

docker system prune -a

# List all images

docker image ls

Useful bash flags for scripts verbose trace expand error exit

Useful flags:
set -v # "verbose" - echo commands
set -x # "xtrace" - like verbose but expands commands
set -e # "errexit" - abort script at first error
set -u # "nounset" - strict mode: undefined variable causes an exit

Long format:
set -o verbose

Bash "strict mode":
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

Fix home and end keys in remote shell

$ od -c

<home key pressed>   ^[[7~
<end key pressed>    ^[[8~

Then put in /etc/inputrc or ~/.inputrc:

"\e[7~": beginning-of-line
"\e[8~": end-of-line

An actual example:

"\e[1~": beginning-of-line
"\e[4~": end-of-line
"\e[3~": delete-char

(source)

sudo doesn't list files properly

Problem:

You can list a directory using sudo:
$ sudo ls /var/log/apache/
access_log  error_log

But you can't list files with a wildcard using sudo:
$ sudo ls /var/log/apache/*_log
ls: cannot access /var/log/apache/*_log: No such file or directory

Solution:

Ensure parameter expansion happens in the shell run by sudo, not in the user's shell:
$ sudo bash -c 'ls /var/log/apache/*_log'

(source)

To get to the page denoted by auditd(8), type
man 8 auditd


Email output from the crontab

method 1 - for all commands

Add this as the first line of crontab:

MAILTO=address1@host.com,address2@host.co.uk

method 2 - for specific commands

Add this line anywhere near the top:

NOTIFY_LIST="address1@host.com,address2@host.co.uk"

Use this for the command:

00 09 * * * run_command | mail -s "command has run" $NOTIFY_LIST

How to change the colours of directories and files

To change the colour of directories from dark blue to light blue (cyan):

    export LS_COLORS=$(echo $LS_COLORS|sed 's/di=01;34/di=01;36/')

See also

    man dircolors

and

    dircolors --print-database



...for a list of the colours.

Bash arithmetic/command expansion

Command expansion, I use a lot
echo something $( date +%s )

Backticks also work for command expansion
echo something `date +%s`
...but do they work any differently to $( )?

Conditionals, like an if statement, use square brackets
if [ "$end_hour" = "24" ]; then ....; fi

Arithmetic, like 1 + 2, uses this:
i=$(( i + 1 ))
let i+=1
See http://www.softpanorama.org/Scripting/Shellorama/arithmetic_expressions.shtml


Temporarily suspend a process

At times, you may find it necessary to temporarily suspend a process, and then resume its execution at a later time. The following two commands will suspend a process, and the resume it, respectively:

# kill -STOP 945
# kill -CONT 945

Bash getopts

Use named command line parameters in Bash scripts.

GNU screen


Type 'screen' after you log into a server.

Or type byobu for an enhancement to GNU screen!
"Allows users to quickly switch their .screenrc to any of the available profiles"
See also http://launchpad.net/byobu
byobu version 3.5
Screen version 4.00.03jw4 (FAU) 2-May-06


Byobu features:
  • Visible at bottom of screen at all times:
    • All screen tabs
    • Server welcome message
    • Uptime
    • Load
    • Hardware spec
    • [configurable to display anything you want]
  • Tab names show special characters to provide more info:
    • - (dash) means that window will be switched to with Ctrl-A, A
    • @ (at) means that tab has seen activity since you last saw it

Screen keyboard commands:
  • Ctrl-A, c -- to create a new terminal session
  • Ctrl-A, Ctrl-A -- to alt-tab
  • Ctrl-A, " -- to list the terminals
  • Ctrl-A, A -- to rename the terminal
  • Ctrl-A M -- (monitor for activity) Toggles monitoring of the current window
  • Ctrl-A _ -- (monitor for silence) Toggle
Best screen feature:
  1. Log into the server.
  2. Type screen to start a session.
  3. Work as normal.
  4. When finished, just close/kill the terminal program, don't log off.
  5. Later, on any (other) PC, log into the server again.
  6. Type screen -D -R to retrieve your session, just as you left it.

I don't know of a way to change the order of the tabs.

Combine STDOUT and STDERR

It's supposed to be done by adding 2>&1 to the end of the command, but that doesn't work for certain commands like time.
Instead make a script combine.sh containing this:
$@ 2>&1

Call it like this:
./combine.sh command_to_capture with parameters 2>&1 > outputfile