Tuesday, November 27, 2012

How to send a process to the background

Sending a process to the background in Linux is quite easy. All you need is bg, fg, &, and ctrl+Z ( ^Z ).
For this example I will use a simple bash script test.sh I put together to print “Test” every 5 seconds.
#!/bin/bash
#This script will print "Test" every 5 seconds
#
while [ true ]
do
echo "Test at `date`"
sleep 5
done
#End

Now let’s see how it’s done.
[user@abubu root]$./test.sh &
This starts test.sh and sends it to the background. You will be back at shell but should see the “Test” message every 5 seconds.
[user@abubu root]$jobs
[1]+ Running ./test.sh &

The jobs command will print all the background processes. Each process is represented by a number to it’s left. For example, tesh.sh is represented by 1.
[user@abubu root]$fg 1
The fg command will send the test.sh process to the foreground and return control to the shell.
[user@abubu root]$ ./test.sh (hit ctrl+Z (^Z) now)
Test at Tue Jun 3 15:11:38 MYT 2008
[1]+ Stopped ./test.sh

The test.sh process is temporarily suspended.
[user@abubu root]$bg 1
The bg command will send test.sh to the background.
[user@abubu root]jobs
[1]+ Running ./test.sh &

The jobs command will print all the background processes. Each process will be represented by a number to it’s left. tesh.sh is represented by 1.
[user@abubu root]$fg 1
The fg command will send the test.sh process to the foreground and return control to the shell.
That’s it.

How to add route in Linux

To view the current routing table run “route -n
[root@klmppswdr01p ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.41.42.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
10.41.41.0 10.41.42.8 255.255.255.0 UG 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
0.0.0.0 10.41.42.1 0.0.0.0 UG 0 0 0 eth0

To add a route refer to the command below.
"route add -net 10.41.41.0 netmask 255.255.255.0 gw 10.41.42.8"
To delete a route refer to the command below.
"route del -net 10.41.41.0 netmask 255.255.255.0 gw 10.41.42.8"
The routing information above is not persistent across reboots. After a reboot, the routing information will be lost and you need to add them in again.
To make the routing information persistent, add the “route add” line as seen above into the /etc/rc.local file.
Sample /etc/rc.local file.
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
route add -net 10.41.41.0 netmask 255.255.255.0 gw 10.41.42.8

How To Execute Linux commands from Windows

Most of the time, users are having a Windows Machine on their desk or laptop. Normally, we want to perform a full scale data retrieval from our Linux servers in the DC, where we don’t have a trusted Linux server to manage it….the answer to it is use “PLINK” utility.
Plink comes together with the Putty…
A simple example of usage is:
C:> plink USERNAME@SERVERNAME ‘YOUR-LINUX-COMMAND’
If you have a dozen of servers…then you probably want to write a batch script in Windows to loop through a list of servers and mention the list of commands juz like what i did…..
Here a typical windows batch script:
@echo off
for / f “tokens=*” %%A in (your-server-list.txt) ( C:pathtoplink.exe user@server -w YOUR-PASSWORD -m linuxcommandscript > YOUR_OUTPUT_FILE.txt)
There you go, i did this for my sar report data collection for root cause analysis and infrastructure load analysis….keying in a password wif every darn login is impractical and yet you dont want to generate a security key for the servers.

How to reset the root password for MySQL

It happens, you set a super complicated password for your MySQL root account and 2 months down the road forget what it was.
Here’s how you’d fix that.
1. Stop your current MySQL database if it is running
root@abubu# service mysqld stop
2. Start MySQL in safe mode and bypass reading the privilege table.
root@abubu# mysqld_safe --skip-grant-tables
3. Reset your root password MySQL console. If it goes well you will not need to key in a password.
root@abubu# mysql -u root mysql
mysql> update user set Password=PASSWORD('new-password');
mysql> flush privileges;
mysql exit;

4. Kill the MySQL process and restart MySQL normally.

How to build a local DNS caching server

Being in Malaysia we are gifted with superior Internet speeds. NOT!!
Services like openDNS are awesome but the lag between us and them often results in sluggish performance anyways.
One way to improve performance is to use local DNS servers. I don’t use Streamyx’s DNS servers because they SUCK!!. TIME’s DNS servers are ok but I still prefer openDNS.
To improve performance, I put together a local DNS caching-only server that forwards to openDNS. Now I have openDNS with lighting fast response.
Let’s walk though the steps to get your own local DNS caching-only server setup. I’m using openSUSE 11 so the steps might vary depending on your distro.
1. Install BIND
pandora:~ # zypper in bind
2. Edit /etc/named.conf
pandora:~ # vi /etc/named..conf
Uncomment the forwarders section. Update the default values with the values below.
forwarders { 208.67.222.222; 208.67.220.220; };

forward only;

Add the line ” forward only; ” This will tell BIND to only forward to the forwarders and not the ROOT servers.
3. Start the service.
To have the service start automatically run ” chkconfig named on
pandora:~ # service named start
4. Let’s make sure your caching server is running fine.
pandora:~ # nslookup google.com localhost
Server:         localhost
Address:        127.0.0.1#53

Non-authoritative answer:
Name:   google.com
Address: 64.233.167.99
Name:   google.com
Address: 72.14.207.99
Name:   google.com
Address: 64.233.187.99


pandora:~ # nslookup yahoo.com localhost
Server:         localhost
Address:        127.0.0.1#53

Non-authoritative answer:
Name:   yahoo.com
Address: 68.180.206.184
Name:   yahoo.com
Address: 206.190.60.37

5. Update your /etc/resolv.conf file.
This will tell your system to use the local DNS server which we just setup instead of the external ones.
Add the lines below to the file.
nameserver 127.0.0.1
nameserver 127.0.0.2

That’s it. You now have local DNS caching. Enjoy!!

My /etc/named.conf file. Only the lines I changed.
#forwarders { 192.0.2.1; 192.0.2.2; };
forwarders { 208.67.222.222; 208.67.220.220; };

# Enable the next entry to prefer usage of the name server declared in
# the forwarders section.
#forward first;
forward only;

VLC media player 0.9.2

VLC media player 0.9.2
VLC media player 0.9.2
The best media player in my book, VLC has a new version out. Like WL it’s my player of choice on both my Linux and Windows machines.
Read the changelog while you download the installer.

How to find your Ubuntu version

2 easy ways find out what version of Ubuntu you’re running.
First option,
cat the /etc/issue file.
danny@family-desktop:/etc$ cat /etc/issue
Ubuntu 8.04.1 n l

Second option
cat the /etc/lsb-release file.
danny@family-desktop:/etc$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=8.04
DISTRIB_CODENAME=hardy
DISTRIB_DESCRIPTION="Ubuntu 8.04.1"

or
run the lsb_release command with the “-a” switch.
danny@family-desktop:/etc$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 8.04.1
Release:        8.04
Codename:       hardy