Search Ali's Blog

Thursday, September 30, 2010

Run as daemon in Linux

In Linux, when a command is executed from a terminal window, it will be child of that window's PID. For example, see below how xlogo is parent of the terminal:
# open terminal window:
# echo $$
  4830
# xlogo &
  4956
# ps -l | grep xlogo
  0 S 1049 4956 4830 0 75 0 - 9979 - pts/11 00:00:00 xlogo

As a result, whenever you close the terminal (parent) window, the xlogo will terminate.

If you wish to run your application as daemon, use the command NOHUP(1), this will make the running application immune to hangups. For example, see how xlogo is child of PID 1 (the init process):
# open terminal window:
# echo $$
  4830
# nohup xlogo &
  5013
# close the terminal window
# ps -l|grep xlogo
  0 S 1049 5013 1 0 75 0 - 9979 - pts/11 00:00:00 xlogo


Tuesday, September 28, 2010

SSH Host key verification failed

If you keep getting this annoying SSH failure:


I used to edit known_hosts file and remove the record that coresponds to the remote host IP,
a simpler way is to run the command:
# ssh-keygen -R REMOTE_IP

Linux Sign Generator

I came across this nice image generator of Linux penguine holding a sign with custom text, give it a try.

I recommend using 20pt pixels font with black color, like the one I used in this sed post.









You can also edit the link directly and just replace the text, for example, to generate a sign with above setting and the test "Linux Sign Generator" then the link would be:
http://www.images-graphics-pics.com/signs/signs/linux/?allow=&text=Linux+Sign+Generator&fontsize=20&font=pixels&color=black&move=20

Note: the character + used for spaces

Sed by Example

As you probably know, sed is a very powerful tool in Linux for filtering and transforming text.
I was looking for some commands with regular expression to process some configuration files in Linux, and I found this link that teaches sed by example, it's informative and easy to understand.







Note: If you read Arabic, sorry for the picture, no offence (I still find it funny) If you do not read Arabic, never mind.

Monday, September 20, 2010

Check multiple machines availability

Usually, administrators check machines liveness by sending ICMP echo request (aka ping request).

I found this tool very handy to check the liveness of multiple machines very quickly from command line. The tool is called fping, and it can be installed on Linux using this tar ball.




Example:
Define your machines IPs configuration file:
# cat machines.ips
10.20.1.19
10.20.1.20
10.20.1.21
10.20.1.22
10.20.1.23
10.20.1.24
10.20.1.25
10.20.1.26
10.20.1.27
10.20.1.31
10.20.1.32


Now, run fping to check the machines availability:
# fping -f machines.ips -r 1 -t 100
10.20.1.19 is alive
10.20.1.20 is alive
10.20.1.21 is alive
10.20.1.22 is alive
10.20.1.23 is alive
10.20.1.24 is alive
10.20.1.26 is alive
10.20.1.27 is alive
10.20.1.31 is alive
10.20.1.32 is alive
10.20.1.25 is unreachable

Script Header Generator

If you write a lot of scripts, you are aware of the importance of the scripts headers.
General, script header define the script language, usage, description, author..

I wrote this script, that generates a script header based on the template below, the script tries to identify the script language and usage automatically, in addition to some other fields, variables that need to be set manually are marked with "TBD" such as script description.

Feel free to change the AUTHOR and AUTHOR_EMAIL in the script to your own strings.
I found this scripts very useful to fix the header of hundreds of scripts I have that didn't include the appropriate header. Share and Enjoy.


Example, to generate the header of the script test.py, run:
$ ./create_header.py test.py

#!/usr/bin/env python

#===========================================
#
# FILE: test.py
# USAGE: ./test.py [--help|--version]
# DESCRIPTION: Tests how fast you can type A-Z
# REQUIREMENTS: python, finger
# BUGS: N/A
# NOTES: Dummy File
# AUTHOR: Ali Ayoub
# EMAIL: ali@ali.ali
# COMPANY: LIUB, Ltd.
# CREATED: 09.19.2010-19:05:39
# REVISION: 1.2
#===========================================

Tip:
Python Rocks.

The ARP Flux Problem

If you spent a lot of time trying to understand why a network interface in Linux fakes ARP replies on behalf of another interface, you may be facing the ARP flux effect! Yes, this is how it's called, and yes, you spend many hours debugging problems due to this weird behavior.

Surprisingly, this is a known behavior of Liux ARP module, when a a machine has multiple network interfaces, as stated in O'Reilly Book: Understanding Linux Network Internals:



The solution of the flux problem is through altering the system sysctl parameters, few websites (see references below) suggest how to make the suitable changes.

To make this process easier, you can use this script to set the right values into the procfs files.
To make the changes permanent (kept among reboots): the following can be added to your /etc/sysctl.conf file:
sysctl -w net.ipv4.conf.all.arp_ignore=1
sysctl -w net.ipv4.conf.all.arp_announce=2

Tip:
To flush the ARP table in Linux, use this helper script.

References:
http://wiki.openvz.org/Multiple_network_interfaces_and_ARP_flux
http://www.inlab.de/balanceng/faq.html
http://linux-ip.net/html/ether-arp.html