UnixServerAdmin

Server Administration & Management

How to limit CPU usage of any process in Linux

You can use cpulimit program that attempts to limit the cpu usage of a process. Limits are expressed in percentage and not in cpu time. cpulimit does not act on the nice value or other scheduling priority stuff, but on the real cpu usage. Also, it is able to adapt itself to the overall system load, dynamically and quickly.

Step-1 Install cpulimit

Type the following commands to install latest stable release:

# cd /tmp
# wget  http://downloads.sourceforge.net/cpulimit/cpulimit-1.1.tar.gz
# tar -zxvf cpulimit-1.1.tar.gz
# cd cpulimit-1.1
# make
# cp cpulimit /usr/local/sbin/
# rm -rf cpulimit*

Step-2 How do I use cpulimit?

To limit CPU usage of the process called firefox to 30%, enter:
# cpulimit -e firefox -l 30

To limit CPU usage of the process to 30% by using its PID, enter:
# cpulimit -p 1313 -l 30

To find out PID of the process use any of the following:
# ps aux | less
# ps aux | grep firefox
# pgrep -u vivek php-cgi
# pgrep lighttpd

You can also use absolute path name of the executable, enter:
# cpulimit -P /opt/firefox/firebox -l 30

Where,

-p : Process PID.
-e : Process name.
-l : percentage of CPU allowed from 0 to 100.
-P: absolute path name of the executable program file.

Root vs Normal User Account :–> cpulimit should run at least with the same user running the controlled process. But it is much better if you run cpulimit as root, in order to have a higher priority and a more precise control.

A Note About SMP (Multicore / MultiCpu) Systems :–> If your machine has one processor you can limit the percentage from 0% to 100%, which means that if you set for example 50%, your process cannot use more than 500 ms of cpu time for each second. But if your machine has four processors, percentage may vary from 0% to 400%, so setting the limit to 200% means to use no more than half of the available power. In any case, the percentage is the same of what you see when you run top.

July 13, 2015 Posted by | Tips & Tricks, Unix/Linux | , , | 1 Comment

How to read or view utmp, wtmp and btmp files in Linux

In Linux operating systems everything is logged some where. Most of the system logs are logged in to /var/log folder. This folder contains logs related to different services and applications. In this folder we have some files such as utmp, wtmp and btmp. These files contains all the details about login’s and logout’s which are from local as well as from remote systems and system status such as uptime etc.

utmp will give you complete picture of users logins at which terminals, logouts, system events and current status of the system, system boot time (used by uptime) etc.

wtmp gives historical data of utmp.

btmp records only failed login attempts.

# last ( Provide how logged in, when they logged in and when they logged out etc info on the screen.)

# last -f /var/log/wtmp (To open wtmp file and view its content use blow command)

# last -f /var/run/utmp (To see still logged in users view utmp file use last command)

# last -f /var/log/btmp (To view btmp file use same command)

August 10, 2014 Posted by | Tips & Tricks, Unix/Linux | , , , , | Leave a comment

Difference between /dev/null and /dev/zero files

/dev/zero and /dev/null are two pseudo files which are useful for creating empty files. Many people consider there is no difference or puzzled with what could be the difference between two files. There is considerable difference when writing data using these hardware files.

/dev/zero: This file is used to create a file with no data but with required size(A file with all zero’s). In other words this will create a data file with all zeros in the file which will give the size to a file. If you see the strace command on /dev/zero, it’s continuous zeros which it will write to a file. So we conclude that /dev/zero is a file use full for creating a file with some required size without any meaning to the data.

/dev/null: This is one more Pseudo file which is useful in many places like redirecting unwanted output/error etc to this file. This file acts as a black hole(Which eat up everything and do not show any output). So whenever you feed some data to this file, you can not retrieve the data which is fed to it. This file even useful for creating files with zero size.

July 30, 2014 Posted by | Tips & Tricks, Unix/Linux | , , , | Leave a comment

Why swap is double the size of the RAM in Linux

It has a meaning and for that we should know memory hierarchy. We have different levels of memory which is useful for processing your data. They are as follows.

  • Processor/CPU registers(Bits in size)
  • L1 Cache(kbs in size)
  • L2 Cache(MBs in size)
  • L3 cache(100s of MBs in size)
  • RAM(GB’s in size)

suppose take one application which is in running state. The application data is moved to RAM for faster accessing and some of its data is moved to L3 cache for processing frequently used data and we move more frequently used app data to L2 and then L1. And we move data which is right now processed by your processor/CPU to registers which is present in processor. If you observe the memory size will gradually decrease when you go up in the ladder more frequently used data is kept at higher levels and made it self available for processing. So if suppose you want to load a bigger program which can consume all the ram/L3/L2/L1 cache its better to move already running process to some location which we can access later to process it once again. But moving this data to HDD is not preferred as it is bit slow and other solution is to increase the RAM size which bit cost. So people came with a solution called SWAP partition which can solve both these limitations.

We create swap partition to cache all the data in registers, L1, L2, L3 caches and RAM.

Then why we create two times the RAM :–>  The reason behind it is that if you club all the above memory locations, the total size will be between 1.5 times to 2 times the RAM. This is the reason behind the thumb rule to create swap size 2 times the RAM

If I create swap size more than two times the RAM, What will happen –>  As we said earlier swap is used to move data from different memories and their total size is always 1.5 times to 2 times maximum so it’s of no use if you create swap size more than 2 times the RAM. In other words, all the space more than 2 times the RAM in swap is no use.

I have RAM size of 128GB and my RAM usage never fills up, do I have to create swap partition –> No, not at all required. As mention earlier swap is a temporary space to store all your memories data for future processing, if your RAM is free then there is no use of swap partition.

July 20, 2014 Posted by | Tips & Tricks, Unix/Linux | , , , | Leave a comment

Why we can create only up to 4 primary partitions

Actually in basic hard disk, we can create 4 partition(either primary or extended), we can create

  • 4 primary maximum or
  • 3 primary + 1 extended or
  • 2 primary + 1 extended or
  • 1 primary + 1 extended

not more then that? why and what is the reason?

The reason is because of a limitation of the MBR (Master Boot Record- the first sector of the hard disk.) The MBR is only 512bytes of size, it is needed to store the primary boot loader, and the partition table. Typically, the area reserved for partition table is only 64 bytes. And the partition table entry for one partition is 16 bytes. So, 16×4=64. The space is over. so we cant create more than this.

July 10, 2014 Posted by | Tips & Tricks, Unix/Linux | , , , | Leave a comment

Difference between NAS & SAN

S.No. NAS – Network Attached Storage SAN – Storage Area Network
1 It can be mapped as network drives to share on that server. It can be used as a ‘disk in disk’ and volume management utilities.
2 It provides storage and a file system. It provide Block based storage.
3 Protocols –> NFS, CIFS & SMB Protocols –> SCSI, Fibre Channel, iSCSI, ATA over Ethernet & HyperSCSI
4 For small & medium business For large organizations
5 Storage Capacity –> up to few TB Storage Capacity –> Many TB
6 Multiple NAS Devices Single SAN & Multiple high performance disk arrays
7 Specialized knowledge and training is not required to configure and maintain NAS. Specialized knowledge and training is required to configure and maintain SANs.
8 NAS & SAN are not mutually exclusive. A hybrid of SAN and NAS can offer file and block level protocols from the same system to support NAS and SAN.

June 30, 2014 Posted by | SAN, Tips & Tricks, Unix/Linux | , , , | Leave a comment

How to find UUID of devices in Linux

This trick is for those who deal with many removable devices, with every device changing its name after you restart your systm. The solution is to mount devices by using their UUIDs (Universally Unique Identifier)

# blkid

/dev/sda1: UUID=”0bd49e6e-6692-410e-8408-5353905f5c42″ TYPE=”ext4″
/dev/sda2: UUID=”cea4d6d2-d624-4193-9576-67ec27b15796″ TYPE=”ext4″
/dev/sda3: UUID=”3b00985c-cf8d-4b87-be92-74186d0a2dcb” TYPE=”ext4″
/dev/sda5: UUID=”79b4e73d-d40d-428b-86a8-930506d86d8d” TYPE=”swap”

Which will show the UUIDs of devices, Now you can make an entry in the “/etc/fstab” file to avoid any problems.
This tip can also be applied in case of SAN storage, where every Logical Unit Number (LUN) needs to be mounted.

June 20, 2014 Posted by | Tips & Tricks, Unix/Linux | , , | Leave a comment

How to find Hardware information on Live (running) Servers & Systems

dmidecode is a tool for dumping a computer DMI (some say SMBIOS) table contents in a human-readable format. This table contains a description of the system hardware components, as well as other useful pieces of information such as serial numbers  and  BIOS  revision.  You can retrieve this information without having to probe for the actual hardware.  While this is a good point in terms of report speed and safeness, this also makes the presented information possibly unreliable.

The  DMI  table  does not  only describe what the system is currently made of, it also can report the possible evolutions (such as the fastest supported CPU or the maximal amount of memory supported).

SMBIOS stands for System Management BIOS, while DMI stands for Desktop Management Interface. Both standards are  tightly related and developed by the DMTF (Desktop Management Task Force).

# yum install dmidecode

# dmidecode –type bios [About BIOS Details]

# dmidecode –type system [About System Information]

# dmidecode –type baseboard [About NIC & Storage Card Information]

# dmidecode –type chassis [About Chassis Information]

# dmidecode –type processor [About Processor Information]

# dmidecode –type memory [About Memory Information]

# dmidecode –type cache [About Cache Information]

# dmidecode –type connector [About USB Information]

# dmidecode –type slot [About Slot Information]

June 10, 2014 Posted by | Tips & Tricks, Unix/Linux | , , | Leave a comment

How to kill Multiple Processes In Linux

There are many cases where you want to kill multiple processes that match a certain pattern in their command line strings. For example, suppose you want to kill all processes that are running commands with keyword “javaxyz” in their arguments.

# ps aux | grep javaxyz

Here is a single command that will kill all processes at once that are matched with grep.

# kill -9 `ps aux | grep javaxyz | grep -v grep | awk ‘{print $2}’`

The command line inside a pair of backtick characters (i.e., ps aux …. ‘{print $2}’) will print out a list of process IDs that are matched with grep. The result is then used by the outer command kill. The “grep -v grep” is to exclude a self match (i.e., grep command itself) from a list of matched processes.

One caveat with this command is that when you are running it in a shell script, make sure to use bash, not sh.

If you are running the following script with sh: you will get “kill: Illegal number:” error. The command line inside a pair of backticks is returning a multi-line response, and it appears that sh is not able to handle it. But bash can. So the following script should be okay.

#!/bin/bash
kill -9 `ps aux | grep javaxyz | grep -v grep | awk ‘{print $2}’`

May 30, 2014 Posted by | Security, Tips & Tricks, Unix/Linux | , , , | Leave a comment

Commands Line Shortcuts

Here are following some tips to speed up our work.

# cmd1 ; cmd2

The above command will run cmd1 and then excute cmd2

# cmd1 && cmd2

This will execute cmd2, if cmd1 is successful.

# cmd1 || cmd2

The above sequence will run cmd2 if cmd1 is not successful.

May 20, 2014 Posted by | Tips & Tricks, Unix/Linux | , | Leave a comment

How to Fix rpmdb: unable to join the environment

This post will probably help you to fix an error occurring when you try to upgrade some package like this:

rpmdb: unable to join the environment
error: db4 error(11) from dbenv->open: Resource temporarily unavailable
error: cannot open Packages index using db3 – Resource temporarily unavailable (11)
error: cannot open Packages database in /var/lib/rpm
warning: /root/webmin-1.360-1.noarch.rpm: V3 DSA signature: NOKEY, key ID 11f63c51
rpmdb: unable to join the environment
error: db4 error(11) from dbenv->open: Resource temporarily unavailable
error: cannot open Packages database in /var/lib/rpm
rpmdb: unable to join the environment
error: db4 error(11) from dbenv->open: Resource temporarily unavailable
error: cannot open Packages database in /var/lib/rpm

How To Fix :-

# rm -f /var/lib/rpm/__db*     (Remove Old Lock Files running this command)

# rpm -vv –rebuilddb     (Now you have to rebuild RPM database)

# yum clean all

# yum update all

April 30, 2014 Posted by | Tips & Tricks, Unix/Linux | , , | 2 Comments

POST Error 1962: No operating system found will reboot in 1 sec

After installing VMWare ESXi and rebooting the system, the user experiences the following POST error “Error 1962 No operating system found will reboot in 1 sec?” in Lenovo ThinkCenter (M62z, M72e, M72e Tiny, M72z, M78, M82, M83, M92, M92p, M92p Tiny, M92z, M93, M93p, M93p Tiny, M93z, Edge 62z, Edge 72z, Edge 92, Edge 92z, E93) and Lenovo ThinkStation E31

To resolve this issue, the user should follow the steps as following :-

1. Press F1 during POST to enter the BIOS Setup screen.
2. Go to the EXIT tab and change “OS optimal defaults” to “Disabled”.
3. Go to the STARTUP tab and change “Boot Mode” to “Legacy” (the default setting is AUTO).
4. Save changes and exit the BIOS.  (NOTE:  User may use the F10 keystroke).

January 30, 2014 Posted by | Tips & Tricks, Unix/Linux | , | 1 Comment

Software RAID issue after reboot of OS

After successful installation of OS, if there are some problem to boot os in one Hard-drive, as grub issue like kernel panic or grub error 17 or other issue

1. Reboot the machine in Rescue mode

Boot: linux rescue

# chroot /mnt/sysimages
OR
# mkdir /a
# mount /dev/sdaX /a
# cd /a

# cat /proc/mdstat (UU means both disk are live)

2. Run the following commands to hotadd and rebuild the array

# mdadm /dev/md0 –add /dev/sda1
# mdadm /dev/md1 –add /dev/sda2
# mdadm /dev/md2 –add /dev/sda3

# grub-install /dev/md0    OR

# grub (GRUB shell type above command to reinstall the boot loader on both drives & reboot.)

grub> device (hd0) /dev/sda
grub> root (hd0,0)
grub> setup (hd0)

grub> device (hd1) /dev/sdb
grub> root (hd1,0)
grub> setup (hd1)

grub> quit

This is show on /boot/grub/device.map

grub> find /grub/stage1 (Find out which are the hard disks on which you can install grub)

This file show in /boot/grub/device.map

grub> device (hd0) /dev/sda
grub> root (hd0,0)
grub> setup (hd0)

January 20, 2014 Posted by | RAID, Tips & Tricks, Unix/Linux | , , , | Leave a comment

How to Check or Monitoring Software RAID in Linux

# mdadm

# cat /proc/mdstat

# mdadm –detail /dev/md0 OR /dev/md1

# mdadm -E /dev/md0 (Spare rebuilding means sync & recovery data to each drive to other)

# mdadm -R /dev/md1 (Recovery & transfer in speed 20-40 Mbps depend on CPU & Hard-drive)

# mdadm /dev/md0 -add /dev/sdb1 [Add partition (RAID Device) /dev/sdb1 into /dev/md0]

January 10, 2014 Posted by | RAID, Tips & Tricks, Unix/Linux | , , , | Leave a comment

How to Create Software RAID-1 Mirroring

# yum install mdadm

# fdisk /dev/sdb     (Create Software RAID Type)

# fdisk /dev/sdc     (Create Software RAID Type)

# mdadm –zero-superblock /dev/sdb /dev/sdc     (If device contains a valid md superblock, the block is overwritten with zeroes)

# mdadm –create /dev/md0 –level=1 –raid–devices=2 /dev/sdb1 /dev/sdc1     (Create RAID-1 using /dev/sdb1 & /dev/sdc1)

# mkfs.ext3 /dev/md0     (format /dev/md0 as ext3 file system)

# mkdir /raid1

# mount /dev/md0 /raid1

# df -hT

# vim /etc/fstab     (For automatically mount after reboot)

/dev/md0    /raid1    ext3    noatime,rw    0    0

# cat /proc/mdstat OR

# watch -n 2 cat /proc/mdstat (For RAID Status)

December 30, 2013 Posted by | RAID, Tips & Tricks, Unix/Linux | , , , | Leave a comment

How to mount USB Pendrive only by name not /dev/sdb1

# fdisk -l (show USB Pendrive device, Suppose its /dev/sdb1)

# cd /etc/udev/rules.d/

# vim 99-usb.rules

KERNEL = “sdb1”, NAME=UnixServerAdmin

# reboot (Restart the machine to apply it)

# mount /dev/sdb1 /mnt
Not mount device is not exit

#mount /dev/UnixsServerAdmin /mnt

Its mount, Have Fun !!!

December 20, 2013 Posted by | Tips & Tricks, Unix/Linux | , , , | Leave a comment

How to Compile Linux Kernel

Configure Yum Repository in local machine, So we can install following packages :-

# yum install kernel-header* redhat-rpm-config-* unidef* rpm-build*

# yum install ncurse* (for menuconfig)

# yum install gcc* (for gconfig)

# yum install qt* glibc* gtk* (for xconfig)

# useradd brewbuilder

# groupadd brewbuilder

# tar -xvf  kernel-2.x.tar.gz

# cd kernel-2.x

# make menuconfig OR make gconfig OR make xconfig

# make (Build the actual kernel & modules)

# make modules_install (Installthe modules under /lib/modules/<kernel-version>)

# make install (Install the Kernel in/boot & adds a GRUB entry in the file)

Check grub entry in /etc/grub.conf file and Rrboot the machine with new compile kernel.

December 10, 2013 Posted by | Tips & Tricks, Unix/Linux | , , , | Leave a comment

How to enable case insensitive in Bash terminal

# set completion-ignore-case on

OR

You need to add set completion-ignore-case on to below file and append text as follows :-

# vim /etc/inputrc

set completion-ignore-case on

November 30, 2013 Posted by | Tips & Tricks, Unix/Linux | , , | Leave a comment

zombie_process.sh

###########################################################
## zombie_process.sh ##
###########################################################
#!/bin/bash
x=0;
for x in `ps -ef | grep defunct | awk ‘{print $3}’` ; do
echo $x
kill -9 $x
done

November 20, 2013 Posted by | Security, Shell Script, Tips & Tricks, Unix/Linux | , , , , , | Leave a comment

How to Extend or increase size of LVM partitions in RHEL/CentOS

# /usr/sbin/lvextend -L+50G  /dev/mapper/VolGroup00-LogVol04

  Extending logical volume LogVol04 to 149.75 GB
  Logical volume LogVol04 successfully resized

# /usr/sbin/vgdisplay

# /sbin/resize2fs /dev/mapper/VolGroup00-LogVol04

resize2fs 1.39 (29-May-2006)
Filesystem at /dev/mapper/VolGroup00-LogVol04 is mounted on /var; on-line resizing required
Performing an on-line resize of /dev/mapper/VolGroup00-LogVol04 to 39256064 (4k) blocks.
The filesystem on /dev/mapper/VolGroup00-LogVol04 is now 39256064 blocks long.

Its Done !!!

November 10, 2013 Posted by | LVM, Tips & Tricks, Unix/Linux | , , , | Leave a comment