Saturday, March 19, 2011

Killing multiple processes at once in Linux

Requirement 1:  Kill all the running processes owned by a particular user.

List all the Process IDs of user
# pgrep -u <username>         

Find Real UID of user
# id <username>

Kill all the processes owned by user with Real UID specified.
# pkill -U <Real UID>           

Example:
Killing all the processes owned by userid 'nagios'

# pgrep -u nagios

# id nagios 
   uid=510(nagios) gid=510(nagios) groups=510(nagios)

# pkill –U 510


Requirement 2:  Kill all processes that are running with some process-name or string (from ps output)

Using "For" loop:
[root@hostxyz ~]# for i in `ps -ef | grep -v grep | grep [username/some string] | awk '{print $2}'`; do kill -9 $i; echo “process id $i is killed”; done

Using "Xargs":
[root@hostxyz ~]# ps aux | grep -v grep | grep [username/some string] | awk '{print $2}' | xargs kill -9

Sample execution:  
To kill all processes that has string "jboss" in it 

[root@hostxyz ~]# for i in `ps -ef | grep -v grep | grep jboss | awk '{print $2}'`; do kill -9 $i; echo "Process id $i is killed"; done

[root@hostxyz ~]# ps aux | grep -v grep | grep jboss | awk '{print $2} | xargs kill -9


Thursday, March 10, 2011

Enabling/Disabling CPUs in Linux

We could get into situation to either disable or enable one or more CPU on a running Linux server (without reboot) and the Linux kernel does support that feature. The reason could be for improving the performance of server or for saving some computing power.


To check the list of CPUs available:


[root@hostxyz ]# cat /proc/cpuinfo


[root@hostxyz ~]# cd /sys/devices/system/cpu
[root@hostxyz cpu]# pwd
/sys/devices/system/cpu
[root@hostxyz cpu]# ls -l
total 0
drwxr-xr-x 4 root root    0 Mar  6 00:30 cpu0
drwxr-xr-x 4 root root    0 Mar  6 00:30 cpu1
drwxr-xr-x 4 root root    0 Mar  6 00:30 cpu2
drwxr-xr-x 4 root root    0 Mar  6 00:30 cpu3
drwxr-xr-x 4 root root    0 Mar  6 00:30 cpu4
drwxr-xr-x 4 root root    0 Mar  6 00:30 cpu5
drwxr-xr-x 4 root root    0 Mar  6 00:30 cpu6
drwxr-xr-x 4 root root    0 Mar  6 00:30 cpu7
-rw-r--r-- 1 root root 4096 Mar  6 00:30 sched_mc_power_savings
[root@hostxyz cpu]#


Suppose I want to disable the CPU no 5:

[root@hostxyz cpu]# cd cpu5
[root@hostxyz cpu5]# ls
cache  crash_notes  online  topology
[root@hostxyz cpu5]# cat online
1
[root@hostxyz cpu5]# echo 0 > online
[root@hostxyz cpu5]# cat /proc/cpuinfo | grep -i 'Processor'
processor       : 0
processor       : 1
processor       : 2
processor       : 3
processor       : 4                             
processor       : 6
processor       : 7
[root@hostxyz cpu5]#
[root@hostxyz cpu5]# cat /proc/interrupts
           CPU0       CPU1       CPU2       CPU3       CPU4       CPU6       CPU7
  0:  346882565          0          0          0          0          0          0    IO-APIC-edge  timer
  1:          0          0          0          0          0          0          0    IO-APIC-edge  i8042
  8:          0          0          0          0          0          0          0    IO-APIC-edge  rtc
  9:          0          0          0          0          0          0          0   IO-APIC-level  acpi
 14:          0          0          0          0          0          0          0    IO-APIC-edge  ata_piix
 15:          0          0          0          0          0          0          0    IO-APIC-edge  ata_piix
 66:         19          0          0          0          0          0          0   IO-APIC-level  ehci_hcd:usb1
 74:          0          0          0          0          0          0          0   IO-APIC-level  uhci_hcd:usb2
 82:         49          0          0          0          0          0          0   IO-APIC-level  uhci_hcd:usb3
 90:          0          0          0          0          0          0          0   IO-APIC-level  uhci_hcd:usb4
 98:       1647        431          0     380368          0         53          0   IO-APIC-level  lpfc
106:       1667         53          0     380346          0        374          0   IO-APIC-level  lpfc
122:    3359627          0          0          0          0          0          0         PCI-MSI  eth0
169:       4954       4701          0     404864          0          0          0   IO-APIC-level  ioc0
NMI:       4505       2316       2389       2332       2882       2164       2909
LOC:  339979108  339988403  339978966  339984061  339983989  339992274  339992202
ERR:          0
MIS:          0
[root@hostxyz cpu5]#


In the above /proc output, you could see the CPU5 has gone !!!!!!!!!


[root@hostxyz cpu5]# cat online
0

Enabling the CPU 5 back:

[root@hostxyz cpu5]# echo 1 > online
[root@hostxyz cpu5]# cat /proc/cpuinfo | grep -i 'Processor'
processor       : 0
processor       : 1
processor       : 2
processor       : 3
processor       : 4
processor       : 5
processor       : 6
processor       : 7
[root@hostxyz cpu5]#
[root@hostxyz cpu5]# cat /proc/interrupts
           CPU0       CPU1       CPU2       CPU3       CPU4       CPU5       CPU6       CPU7
  0:  346920869          0          0          0          0          0          0          0    IO-APIC-edge  timer
  1:          0          0          0          0          0          0          0          0    IO-APIC-edge  i8042
  8:          0          0          0          0          0          0          0          0    IO-APIC-edge  rtc
  9:          0          0          0          0          0          0          0          0   IO-APIC-level  acpi
 14:          0          0          0          0          0          0          0          0    IO-APIC-edge  ata_piix
 15:          0          0          0          0          0          0          0          0    IO-APIC-edge  ata_piix
 66:         19          0          0          0          0          0          0          0   IO-APIC-level  ehci_hcd:usb1
 74:          0          0          0          0          0          0          0          0   IO-APIC-level  uhci_hcd:usb2
 82:         49          0          0          0          0          0          0          0   IO-APIC-level  uhci_hcd:usb3
 90:          0          0          0          0          0          0          0          0   IO-APIC-level  uhci_hcd:usb4
 98:       1647        431          0     380412          0          0         53          0   IO-APIC-level  lpfc
106:       1667         53          0     380390          0          0        374          0   IO-APIC-level  lpfc
122:    3360061          0          0          0          0          0          0          0         PCI-MSI  eth0
169:       4954       4703          0     404921          0          0          0          0   IO-APIC-level  ioc0
NMI:       4505       2317       2389       2332       2883       2428       2164       2910
LOC:  340016644  340025946  340016502  340021607  340021535  339927925  340029818  340029746
ERR:          0
MIS:          0
[root@hostxyz cpu5]#

In the above /proc output, you can see that I brought back the CPU 5 back.

Wednesday, March 9, 2011

All about TIME in Linux

Below given are some useful commands pertaining to Time and Date:

[root@hostxyz ]# clock --show
Tue 08 Mar 2011 02:27:07 PM CST  -0.950788 seconds

[root@hostxyz ]# hwclock
Tue 08 Mar 2011 02:27:17 PM CST  -0.438789 seconds
[root@hostxyz ]#

Set the hardware clock by executing:
# /sbin/hwclock --systohc
Syncing the time with other or Internet Time servers:
# /usr/bin/rdate -s  <remote server ip/hostname>
Syncing the time with NTP server:
# ntpdate -u <NTP server IP/Hostname>
 
Use "time" command to find the time of execution of a command:
[root@hostxyz ]# time updatedb
real    0m13.759s
user    0m0.293s
sys     0m1.190s
[root@hostxyz ]# times updatedb
0m0.044s 0m0.029s
0m3.839s 0m1.830s
[root@hostxyz ]#
# Timezone config file
[root@hostxyz ]# cat /etc/sysconfig/clock
ZONE="America/Chicago"
UTC=true
ARC=false
[root@hostxyz ]#
If you wish the Change the Timezone to IST, follow the below steps:
# rm -rf  /etc/localtime
Change to the directory /usr/share/zoneinfo here you will find a list of time zone regions. Choose the most appropriate region, if you live in India in city Chennai, you will find a directory "Indian".
Create a symbolic link to the appropriate timezone to /etc/localtime
# ln -sf /usr/share/zoneinfo/Indian/Chennai /etc/localtime 
[root@hostxyz zoneinfo]# cat zone.tab | grep -w Indian
CC    -1210+09655       Indian/Cocos
CX    -1025+10543       Indian/Christmas
IO    -0720+07225       Indian/Chagos
KM    -1141+04316       Indian/Chennai
MG    -1855+04731       Indian/Antananarivo
MU    -2010+05730       Indian/Mauritius
MV    +0410+07330       Indian/Maldives
RE    -2052+05528       Indian/Reunion
SC    -0440+05528       Indian/Mahe
TF    -492110+0701303   Indian/Kerguelen
YT    -1247+04514       Indian/Mayotte
[root@hostxyz zoneinfo]#

Monday, March 7, 2011

Steps to upgrade openssh package


1st Method

1.  Download the latest openssh package from http://www.openssh.org/ under /usr/local.
2.  Extract the package: # tar -xvf openssh-<version>.tar.gz
3.  Take a note of current SSH installation on the server by executing the following commands:
      # which ssh
      # rpm -qa | grep ssh
      # rpm -qf  `which ssh` 
      Also take a backup of /etc/ssh folder.
4.  Go to /usr/local/openssh-<version no>/ 
5.  Compile the openssh package as follows:
     # ./configure --with-kerberos5 --with-pam --with-md5-passwords 
6. make 
7. make install 
8. Check if the system is using the newly compiled openssh package by running the 'which' and 'ssh -v' command. 
9. Manually link the older version of  'ssh' command to new version:
   # mv /usr/sbin/ssh /usr/sbin/ssh_old
   # ln -s /usr/local/bin/ssh /usr/sbin/ssh  


2nd Method (by overriding existing SSH config)
1.  Download the latest openssh package from http://www.openssh.org/ under /usr/local.
2.  Extract the package: # tar -xvf openssh-<version>.tar.gz
3.  Take a note of current SSH installation on the server by executing the following commands:
      # which ssh
      # rpm -qa | grep ssh
      # rpm -qf  `which ssh` 
      Also take a backup of /etc/ssh folder.
4.  Go to /usr/local/openssh-<version no>/ 
5.  Compile the openssh package as follows:

./configure --prefix=/usr --sysconfdir=/etc/ssh --with-ssl-dir=/opt/openssl-0.9.8e --with-pam  --with-kerberos5 --with-md5-passwords --with-tcp-wrappers

6. make 
7. make install 
8. Check if the system is using the newly compiled openssh package by running the 'which' and 'ssh -v' command. 


Saturday, March 5, 2011

Using 'date' command smartly

Here I have shown some examples for using 'date' command in various forms. This will be helpful for deriving some logic in scripting.


Current time:
[root@hostxyz ~]# date
Fri Mar  4 12:37:29 CST 2011


Output only the day of week:
[root@hostxyz ~]# date +%a
Fri


Output the Time 1 hour before:
[root@hostxyz ~]# date +%Y%m%d%H%M%S -d  "1 hour ago"
20110304113732
[root@hostxyz ~]#


Output the Time 2 hours before:
[root@hostxyz ~]# date   --date='2 hour ago' +'%b %e %H:'
Mar  4 10:
[root@hostxyz ~]#


Finding Yesterday's day and assigning to a variable DAY_1:  
DAY_1=`(date --date='1 days ago' '+&#37;a')`
echo $DAY_1


Finding day for two days ago and assigning to a variable DAY_2:
DAY_2=`(date --date='2 days ago' '+%a')`
echo $DAY_2