Detach processes from terminal

GNU screen/tmux

The best solution is simply to use a terminal multiplexer, like GNU screen or tmux. Simply launch a session, tape your command and then detach it (Ctrl-a d for screen). Easy and clean 😉

setsid

Debian contains a binary called setsid in the util-linux package. setsid can be use to start a process and detach it from the current shell (basically it create a new shell for the ‘orphaned’ process).

setsid doesn’t redirect the standard files descriptors (stdin, stdout and stderr) so you loose any process output except if you make a stdout+stderr redirection to a file:

setsid <command> > /tmp/output.txt &2>1

nohup

nohup as the name implies, makes your command ignore SIGHUP signal. Also by default nohup redirects the standard output and error to the file nohup.out, so the program won’t fail for writing to standard output when the shell is closed. Note that nohup doesn’t remove the process from the shell’s job control and also doesn’t put it in the background. Usage:

nohup <command> > /tmp/output.txt &

disown

Last option (and the more interesting) is the built-in bash command disown. disown removes the process from the shell’s job control, but still leaves it connected to the terminal. The results is that the shell won’t send it a SIGHUP when closed, but in the meantime you still get the output. The advantage is you can disown a already running program.

Simply suspend the program using Ctrl-z then use bg to put it in background. Then detach it:

disown %n

where n is the job number (use the command job to get it).

Linux ate my RAM!

There is a saying in Linux community: “Free memory is wasted memory.” This statement seems to confuse newbies, resulting in the Linux ate all my RAM myth. Reality is that the kernel borrows unused chunk of memory for disk caching (alias “Buffers”) and file caching (alias “Cached”). This behavior improve significantly the overall performances.

Whenever an application needs more memory, borrowed chunks are “returned”. The website linuxatemyram.com explain this mechanism in more detail.

Confusion arise because people don’t read the right line when using the free command:

$ free -m
             total       used       free     shared    buffers     cached
Mem:          5863       5541        321        569        107       1203
-/+ buffers/cache:       4231       1632
Swap:         7323       3435       3888

You think this host only have 321MB of ‘free’ memory ? Wrong !
This host have 321MB of non-used memory, that true. But the total amount of memory that the kernel can “reclaim” (and therefore give for applications) is much higher.

The right answer is 1632MB.

In your scripts if you want to get the amount of ‘free’ memory, use the following one liner:

free -m | sed -n -e '3p' | grep -Po "\d+$"

Refresh ARP cache entry

ARP (Address Resolution Protocol) is the method for finding a host’s hardware address when only its IP address is known. ARP is a Link Layer protocol (Layer 2) because it only operates on the local area network.

When you migrate an IP from a VM or an hypervisor to another one, you can sometime encounter strange network problems (like no traffic received to this IP for a while). The cause of theses problems is that some network equipment (router or firewall) on the path have a ‘expired’ ARP entry in their cache.

Refresh ARP cache

For a GNU/Linux or BSD host, check the current arp table with arp -a.
Then purge the value with arp -d <hostname>

For Cisco iOS use show arp for checking arp table and clear ip arp <ip> to purge an entry.

For equipment you don’t have the hand on, the solution is a little more tricky. You need to broadcast from your VM the correct ARP ‘value’, in order to make the remote device invalidate its existing cache entry. For this use the arping command:

arping -S <ip> -B