October 2025
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories

October 2025
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

certificate is signed using a weak signature algorithm!

certificate is signed using a weak signature algorithm!

Turns out that this results in SSL certificates that use the weaker MD5 signature hash algorithm which is the cause of the complaint. This is easily fixed by adding ‘-sha1’ to the openssl command line when generating the certificate. Like so:

$ openssl req -new -x509 -sha1 -nodes -out server.crt -keyout server.key

Tomcat stop start script for redhat

#Startup script for Tomcat
JAVA_HOME=/usr/java/jdk1.6.0_43
export JAVA_HOME
start_tomcat=/opt/tomcat6/bin/startup.sh
stop_tomcat=/opt/tomcat6/bin/shutdown.sh

start() {
echo -n “Starting tomcat:”
su -c ${start_tomcat} – tomcat
echo “done.”
}
stop() {
echo -n “Shutting down tomcat: “
${stop_tomcat}
echo “done.”
}

# See how we were called
case “$1” in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 10
start
;;
*)
echo “Usage: $0 {start|stop|restart}”
esac
exit 0

#Startup script for Tomcat
# description: Start tomcat
# chkconfig: 345 99 01
JAVA_HOME=/usr/java/jdk1.6.0_43
export JAVA_HOME
start_tomcat=/opt/tomcat6/bin/startup.sh
stop_tomcat=/opt/tomcat6/bin/shutdown.sh

NAME=”$(basename $0)”
unset ISBOOT
if [ “${NAME:0:1}” = “S” -o “${NAME:0:1}” = “K” ]; then
NAME=”${NAME:3}”
ISBOOT=”1″
fi

# Define the tomcat username
TOMCAT_USER=”${TOMCAT_USER:-tomcat}”

start() {
echo -n “Starting tomcat:”
su -c ${start_tomcat} – tomcat
echo “done.”
}
stop() {
echo -n “Shutting down tomcat: “
${stop_tomcat}
echo “done.”
}

# See how we were called
case “$1” in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 10
start
;;
status)
if [ -f “/var/run/${NAME}.pid” ]; then
read kpid < /var/run/${NAME}.pid
if [ -d “/proc/${kpid}” ]; then
echo “${NAME} (pid ${kpid}) is running…”
RETVAL=”0″
fi
else
pid=”$(/usr/bin/pgrep -d , -u ${TOMCAT_USER} -G ${TOMCAT_USER} java)”
if [ -z “$pid” ]; then
echo “${NAME} is stopped”
RETVAL=”3″
else
echo “${NAME} (pid $pid) is running…”
RETVAL=”0″
fi
fi
;;
*)
echo “Usage: $0 {start|stop|restart}”
esac
exit 0

#Startup script for Tomcat
# description: Start tomcat
# chkconfig: 345 99 01
JAVA_HOME=/usr/java/jdk1.6.0_43
export JAVA_HOME
start_tomcat=/opt/tomcat6/bin/startup.sh
stop_tomcat=/opt/tomcat6/bin/shutdown.sh

NAME=”$(basename $0)”
unset ISBOOT
if [ “${NAME:0:1}” = “S” -o “${NAME:0:1}” = “K” ]; then
NAME=”${NAME:3}”
ISBOOT=”1″
fi

# Define the tomcat username
TOMCAT_USER=”${TOMCAT_USER:-tomcat}”

start() {
echo -n “Starting tomcat:”
su -c ${start_tomcat} – tomcat
echo “done.”
}
stop() {
echo -n “Shutting down tomcat: ”
${stop_tomcat}
echo “done.”
}

# See how we were called
case “$1” in
start)
if [ -f “/var/run/${NAME}.pid” ]; then
read kpid < /var/run/${NAME}.pid if [ -d "/proc/${kpid}" ]; then echo "Error: ${NAME} (pid ${kpid}) is already running..." RETVAL="0" fi else pid="$(/usr/bin/pgrep -d , -u ${TOMCAT_USER} -G ${TOMCAT_USER} java)" if [ -z "$pid" ]; then start RETVAL="3" else echo "Error: ${NAME} (pid $pid) is already running..." RETVAL="0" fi fi ;; stop) stop ;; restart) stop sleep 10 start ;; status) if [ -f "/var/run/${NAME}.pid" ]; then read kpid < /var/run/${NAME}.pid if [ -d "/proc/${kpid}" ]; then echo "${NAME} (pid ${kpid}) is running..." RETVAL="0" fi else pid="$(/usr/bin/pgrep -d , -u ${TOMCAT_USER} -G ${TOMCAT_USER} java)" if [ -z "$pid" ]; then echo "${NAME} is stopped" RETVAL="3" else echo "${NAME} (pid $pid) is running..." RETVAL="0" fi fi ;; *) echo "Usage: $0 {start|stop|restart|status}" esac exit 0

Install phpMyAdmin on CentOs 6

Install phpMyAdmin on CentOs 6 and had interesting walkthrough of the installation

1. Get the latest copy of phpMyAdmin from sourceforge

wget http://downloads.sourceforge.net/project/phpmyadmin/phpMyAdmin/3.5.7/phpMyAdmin-3.5.7-english.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fphpmyadmin%2Ffiles%2FphpMyAdmin%2F3.5.7%2F&ts=1361966494&use_mirror=jaist

2. Extract the files

tar -zxvf phpMyAdmin-3.5.7-english.tar.gz

3. move the extracted files to proper location

mv phpMyAdmin-3.5.7-english.tar.gz /var/www/html/phpmyadmin

4. cd /var/www/html/phpmyadmin

5. Make a copy of configuration files

cp config.sample.inc.php config.inc.php

6. Modify the values vi config.inc.php

$cfg[‘Servers’][$i][‘auth_type’]=’cookie’; to $cfg[‘Servers’][$i][‘auth_type’]=’http’;

7. Restart apache from any of command

service httpd restart – Work for me
/etc/init.d/httpd restart

8. Open browser on server

http://localhost/phpMyAdmin

You may end in following errors

1. 404 Not Found Error
Please make sure you push your phpMyAdmin directory in your webroot. In my case it’s /var/www/html

2. 403 Forbidden Error
I spend good amount of time finding why access is forbidden and finally did following setting

2.1 Go to /etc/httpd/conf.d/

2.2 vi phpMyAdmin.conf

2.3 Add following lines

Order allow,deny
Options Indexes
Allow from all

Still not able to access, try disabling the SElinux

SElinux is extra security layer in Linux and sometimes it provide conflicts for apache to work

vi /etc/selinux/config
Modify SELINUX=enforcing to SELINUX=disabled //Discuss with your senior. Mine is test server.
Restart your machine

 http://localhost/phpMyAdmin prompt for username/password for mysql login 🙂

 

phpMyAdmin: Script timeout passed, if you want to finish import, please resubmit same file and import will resume.  Posted by alex in Errors

When attempting to import a gzip sql file into MySQL via phpMyAdmin I continued to get the error below. There are a couple ways to get around this issue which depend on if you have access to the server or not. Below I will describe two ways to resolve the error if you have access to the server and two ways to resolve the issue if you do not have access to the server.   Error: Script timeout passed, if you want to finish import, please resubmit same file and import will resume.  

Resolution With Access To Server:  1.Import Using CLI:Importing using mysql commands are much easier to complete and they are also much more effecient. Use the syntax below to import a SQL file into a MySQL database.  

 

2.Modify Timeout Setting:You can modify the config.inc.php file on your server which is located inside the phpMyAdmin root directory. The setting which needs to be modified or added to the config.inc.php file is displayed below. This setting which is set in the phpMyAdmin configuration file is the specific item that causes the exact error in the title of this post.  

view source

1 $cfg[‘ExecTimeLimit’] = 0;

[mysqld]

max_allowed_packet = 500M

 
Find:
post_max_size = 8M
upload_max_filesize = 2M
max_execution_time = 30
max_input_time = 60
memory_limit = 8M

Change to:
post_max_size = 750M
upload_max_filesize = 750M
max_execution_time = 300
max_input_time = 540
memory_limit = 1000M

 

Java Heap Dump and analyze

First get the Java Applications Process Id, one way to do this is with: jps

jmap -dump:format=b,file=dump.bin <javaProcessIdHere>

The size of the heap dump file will be same as the heap memory in use at the time the command is run. For large heap sizes this can take several minutes to run, and can stall (make it unresponsive) the application during this time.

To get a summary of Object Instances and Sizes you can use this command:

jmap -histo:live <javaProcessIdHere>

Generally, this command will run quickly, and should be used when taking a heap dump will be too costly in terms of time-taken/size etc.

To get Heap Dump on Out of Memory Exception (available from Java 1.5.0_07 onwards):

Add the following param: -XX:+HeapDumpOnOutOfMemoryError

If you want the dump to go in a particular path then add: -XX:+HeapDumpOnOutOfMemoryError XX:HeapDumpPath=/usr/local/dumps

To initiate Heap Dump from within the Application Code:

We can also use jmap from our code. To get a pid from code use we need to use java.lang.management.ManagementFactory.

String name = ManagementFactory.getRuntimeMXBean().getName();
String pid = name.substring(0, name.indexOf("@"));
After that we can start jmap process like this:
String[] cmd = { "jmap", "-dump:file=/usr/local/heapdumps/dump.bin", pid };
Process p = Runtime.getRuntime().exec(cmd);

A simple Shell Script to Take Heap Dump every 30 seconds:

while true
do
  jmap -dump:file=/tmp/java-`date +%s`.hprof <processIdOfJVM>
  sleep 30
done

Heap Dump Analysis Tools
  • Java Visual VM (part of JDK)
  • jhat – Java Heap Analysis Tool (Suitable for analyzing very big heap files)

Traffic Calcuation for Tomcat and Apache

  • How many users can we handle?
  • How many requests/second can we handle?
  • How much bandwidth is needed to handle so many users or requests/second?
  • How do i calculate these figures?
  • How many (an application) servers do i need to handle this traffic?

Lets begin with a small example to understand how we arrive at answers to the above questions.

Our Application: It is a typical web-application where users browse web pages, do stuff(your business logic), and then leave! Simple enough for our scenario.

Our Server: Tomcat 6 server (with the default 200 worker threads).

So, we can conclude this easily:

We can handle 200 simultaneous requests definitely!(because there are 200 worker threads available to us).

But, how many requests/second can we handle?

For that we need to know what is the average time required by a thread to process requests. This will depend on your application. Let us assume that every thread takes 0.5 second average time to serve a request.

This means we can handle 200 requests/0.5seconds == 400 requests/second.

But, how many users can we handle simultaneously?

This depends on how frequently the user sends requests to the server! Let’s say an average user sends a request to the server once in every 10 seconds. So he sends 1 request/10 seconds OR 0.1 request/second.

Since our server can handle 400 requests per second, the number of total simultaneous users it can handle is: (400 requests/second) / (0.1 requests/second)

== 4000 (online) users!

How many total users can we support?

If you assume that 5% of users are online at any point in time, then we can support 4000/(5%)==80,000 users.

How much bandwidth is required (bandwidth is provided by data centre hosting your server!)?

Lets say every request results in average 20 KB of data by returning in response. Since we can handle 400 requests/second, we need at least 400 x 20 KB = 8000KBps == 8MBps == 64Mbps (NOTE: B=Bytes, b=bits, for simplicity assumed K as 1000 instead of 1024).

Lets summarize, what traffic our web application handle?

  • 4000 simultaneous users. 80,000 total users.
  • 400 requests/second.
  • And we need at least 64Mbps of bandwidth.

Now, this will give a some idea of how to find out what traffic your web application can handle. Here, we are not discussing how to scale your application/server(that is left for another post). Of-course as you scale(for example by using multiple tomcat servers behind a apache load balancer) you will be able to handle larger traffic.

Solaris Add Defualt Route

netstat -nr

route delete default 192.168.1.251

ou can add default router temporarily by following command

route add default 192.168.10.249

You can add default router permanently by following commands

route -p add default 192.168.1.249

Or

echo 192.168.1.249 > /etc/defaultrouter

It will create /etc/defaultrouter file if not already available.
Next time you restart you system 192.168.1.249 will be automatically added to default router.

How to Install and Configure VNC Server

First you need to check for already installed VNC Server. You can check this by executing following command.

rpm -q vnc-server

If you found message like  “package vnc-server is not installe” then you need to install it first by executing following command from root user.

yum install vnc-server

Once VNC Server installed you need to have a window manager installed if not already available. execute following command to install ‘GNOME Desktop Environment’ from root user.

yum groupinstall “GNOME Desktop Environment”

Now change the configuration of VNC Server’s configuration file

cd /etc/sysconfig
vi vncservers

 

add following lines to the file. change anyusername to your already created username.

VNCSERVERS=”1:root”
VNCSERVERARGS[1]=”-geometry 1024×768″

 

Now login with root user and start the “vnc server”

/sbin/service vncserver start

now login to “root” and configure the vnc password for this user

cd /root/.vnc/

vncpasswd

 

cd .vnc
vi xstartup

uncomment following lines by deleting “#” sign

unset SESSION_MANAGER
exec /etc/X11/xinit/xinitrc

and change the xterm command as follows

xterm -geometry 1024×768 -ls -title “&VNCDESKTOP Desktop” & gnome-session &

Now start the vnc server again by root user

/sbin/service vncserver start

Once started you can now install the vnc client in you client machine and login to CentOs machine as follows

Starting the DB2 control center on Linux

To start the DB2 control center, perform the following steps:

  1. Login as root.
  2. At the command line, run the following command: xhost +localhost.
  3. Run the following command su – ctginst1.
  4. Run the following command db2cc.

Convert VirtualBox VDI to VMware VMDK disks

VBoxManage

We have used VBoxManage to clone VirtualBox disks. The method here is identical, except that we will be converting to a different disk format. Let’s see how this is done.

VBoxManage clonehd source.vdi target.vmdk –format VMDK

Where source is your VirtualBox disk, target is your VMware disk and –format VMDK is the desired output format. Similarly, you can go the other way around, using VMDK as your source and VDI as your target. Make sure to specify the right disk format. VBoxManage clonehd function supports other formats as well.

 

Vice versa

Oh, yes, the other direction is fairly simple.

VBoxManage clonehd source.vmdk target.vdi –format VDI

 

 

QEMU

QEMU is an old friend, too. We’ve seen in my very first article on virtualization, when I was still young and naive. Strike that, I was never young. We have also used QEMU to convert VMDK disks to RAW format, so we could use them with Amazon EC2 machines. The concept is exactly the same here. And it works both ways.

VDI to VMDK

It’s extremely simple.

qemu-img convert something.vdi -O vmdk something.vmdk

VMDK to VDI

You have two ways to do this. Use a one-step qemu-img-vdi tool or go about using classic qemu-img, which will require an intermediate conversion to raw format.

qemu-img-vdi

Pretty much like all other conversions we’ve seen before:

qemu-img-vdi convert something.vmdk -O vdi something.vdi

qemu-img

Now, using qemu-img, you have two steps, first convert to RAW format, then use VBoxManage to convert to VDI. Older versions of VirtualBox used the tool called vditool, so you may find online resources that reference this utility.

Step 1: Convert VMDK to RAW (you don’t need any file extension, btw)

qemu-img convert something.vmdk something.raw

Step 2: Convert RAW to VDI

VBoxManage convertdd something.raw something.vdi

Note: If you’re using sparse disks, as we’ve seen in other tutorials, the raw file will them inflated to their intended max. size. A 20GB disk, even though if only 2.4GB are used, will become a 20GB byte-by-byte file on your hard disk.

That would be all. Next, please!

Conversion method 3 (maybe): VMware Converter

To the best of my knowledge, and I’ve already stated in the past, it does not seem that VMware Converter supports VirtualBox. Therefore, using VMware Converter may or may not work.

Now, you can try a different method. Export your VirtualBox machine as an appliance, which will save it in the .ovf format, including a ready VMDK disk. Then, convert the appliance using VMware Converter. There’s no guarantee, though. I have read a few forum threads here and there where various users claimed to have done it successfully, so it indeed may be possible.

I have not managed this yet, so keep this in mind!

Linux commands for Reference

find . -type f -exec grep ‘NMX_FXNG_AND_CONTRACT_DBF’ {} \;

history

 history|awk ‘{print $2}’|awk ‘BEGIN {FS=”|”} {print $1}’|sort|uniq -c|sort -r

 history|tail -1000|awk ‘{print $2}’|awk ‘BEGIN {FS=”|”} {print $1}’|sort|uniq -c|sort -r

-daystart    This flag starts at the beginning of the day.
-atime    The time the file was last accessed — in number of days.
-ctime    The time the file’s status last changed — in number of days.
-mtime    The time the file was last modified — in number of days.
-amin    The time the file was last accessed — in number of minutes. (It is not available on all implementations.)
-cmin    The time the file’s status last changed — in number of minutes. (It is not available on all implementations.)
-mmin    The time the file was last modified — in number of minutes. (It is not available on all implementations.)
-type    This flag describes the type of file, such as d for directories.
-userX    Files belonging to user X.
-groupX    Files belonging to group X.
-newerX    Files that are newer than file X.

find /home/$1/mail/*/mail/.spam/cur -type f -mtime +7 -exec rm {} \;
find /home/$1/mail/*/mail/.spam/new -type f -mtime +7 -exec rm {} \;
 

Here’s how to list all the files in your home directory tree that were modified exactly one hour ago:$ find ~ -mmin 60 \! -type d

Giving a negative value for a flag means to match that number or sooner. For example, here’s how to list all the files in your
home directory tree that were modified exactly one hour ago or any time since

find ~ -mmin -60 \! -type d

$ date
Mon Oct 23 09:42:42 EDT 2006
$ touch -t 10230842 temp
$ ls -l temp
-rw-r–r–    1 joe        joe               0 Oct 23 08:42 temp
$ find ~ -newer temp \! -type d

find / -user `whoami` -daystart -atime -1 \! -type d

Give different values for the various time flags to change the search times. You can also combine flags. For instance,
you can list all the files in your home directory tree that were both accessed and modified between now and seven days ago:
find ~ -daystart -atime -7 -mtime -7  \! -type d

find mtime

find . -mtime 0 # find files modified between now and 1 day ago
# (i.e., within the past 24 hours)
find . -mtime -1 # find files modified less than 1 day ago
# (i.e., within the past 24 hours, as before)
find . -mtime 1 # find files modified between 24 and 48 hours ago
find . -mtime +1 # find files modified more than 48 hours ago

find . -mmin +5 -mmin -10 # find files modified between
# 6 and 9 minutes ago

* find top 10 largest files in /var:

$ find /var -type f -ls | sort -k 7 -r -n | head -10

* find all files having size more than 5 GB in /var/log/:

$ find /var/log/ -type f -size +5120M -exec ls -lh {} \;

* find all today’s files and copy them to another directory:

$ find /home/me/files -ctime 0  -print -exec cp {} /mnt/backup/{} \;

* find all temp files older than a week and delete:

$ find /temp/ -mtime +7-type f | xargs /bin/rm -f

* find and rename all mp3 files by changing their uppercase names to lowercase:

$ find /home/me/music/ -type f -name *.mp3 -exec rename ‘y/[A-Z]/[a-z]/’ ‘{}’ \;

Grep

some examples of grep command:

* Print Apache’s documentroot directory name:

$ grep -i documentroot  /etc/httpd/conf/httpd.conf

* View file contents without comments and empty lines:

$ grep -Ev “^$|^#” /etc/my.cnf

* print only IP address assigned to the interface:

$ ifconfig eth0 | grep ‘inet addr:’ | cut -d’:’ -f2 | awk ‘{ print $1}’

* How many email messages sent for a particular date:

$ cat /var/log/maillog | grep “status=sent” | grep “May 25” | wc -l

* Find out a running process/daemon from process list (thanks to staranneph for recalling this):

ps -ef | grep mysql

* You can also note cpu/mem usage by using above. like in below command output, you can see that Plesk’s statistics process is utilizing more than 18% cpu alone:

[root@myserver ~]# ps aux | grep statistics
root      8183 18.4  0.0  58384  2848 ?        D    04:05   3:00 /usr/l

source

$ touch -d “Aug 1 2006” file.start
$ touch -d “Sep 1 2006” file.end
$ find /usr/share -daystart -newer file.start \! -daystart -newer file.end

 egrep -i “err|panic|crit|warn” /var/log/messages

User management

groupadd sshd
useradd -g sshd -d /var/empty -s /bin/false sshd
groupadd -r dovecot
useradd -r -g dovecot -s /sbin/nologin dovecot
groupadd -r vmail
useradd -r -g vmail -s /sbin/nologin vmail

Disk space issue

ls -ltrhS | grep “Apr  [2-4]” | awk -F’ ‘ ‘{print $9}’ | xargs rm -f

Memory Usage  Command

ps -eo pmem,pcpu,rss,vsize,args | sort -k 1 -r | more

 Cpu Usage

ps -eo pcpu,pid,args | sort -k 1 -r | head -10

Sometime Apache process, keeps on execution (Seems like Hangs), so generally trying to get the exact PHP file that is running by Apache Process, So here is my Try.

I used Strace to get the opened files by the apache process. (Get PID of
Apache process that is taking time, though you can also get it From top  command)

# pstree -p -n | grep http
(This will show each files that is being processed by that Apache Proc)

# strace -p <PID of Apache>
The list of files could also be get using lsof, but that could not be of full use, as you need the files continuus

To see all the memory used by a process we use pmap(process mapping ).
#pmap pid-of-that-application

pmap 1946

Example :
# pmap `pgrep apache` | grep total

Linux System Monitoring Tools

#1: top – Process Activity Command

Commonly Used Hot Keys

The top command provides several useful hot keys:
Hot Key    Usage
t    Displays summary information off and on.
m    Displays memory information off and on.
A    Sorts the display by top consumers of various system resources. Useful for quick identification of performance-hungry tasks on a system.
f    Enters an interactive configuration screen for top. Helpful for setting up top for a specific task.
o    Enables you to interactively select the ordering within top.
r    Issues renice command.
k    Issues kill command.
z    Turn on or off color/mono

What are the CPU states found in “top” output?
Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st

# us -> User CPU time: The time the CPU has spent running users’ processes that are not niced.
# sy -> System CPU time: The time the CPU has spent running the kernel and its processes.
# ni -> Nice CPU time: The time the CPU has spent running users’ process that have been niced.
# wa -> iowait: Amount of time the CPU has been waiting for I/O to complete.
# hi -> Hardware IRQ: The amount of time the CPU has been servicing hardware interrupts.
# si -> Software Interrupts.: The amount of time the CPU has been servicing software interrupts.

#2: vmstat – System Activity, Hardware and System Information

The command vmstat reports information about processes, memory, paging, block IO, traps, and cpu activity.

vmstat 3

Display Memory Utilization Slabinfo

# vmstat -m

Tail, Vmstat and Date  in Loop, Output every 10 Sec
# vmstat 1 1;for ((;;));do date; vmstat 10 2 | tail -n1;done

3: w – Find Out Who Is Logged on And What They Are Doing

w command displays information about the users currently on the machine, and their processes.
# w username
# w vivek

#4: uptime – Tell How Long The System Has Been Running

# uptime

#5: ps – Displays The Processes

Show Long Format Output

# ps -Al

Print All Process On The Server

# ps ax
# ps axu

Memmory commands   cpu commands

Print Security Information

# ps -eo euser,ruser,suser,fuser,f,comm,label

Set Output In a User-Defined Format

ps axo stat,euid,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,comm

ps -eopid,tt,user,fname,tmout,f,wchan

Display sorted process taking most CPU in descending order
# ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10

CPU COMMAND

10 cpu usage command

ps -e -o pcpu,cpu,nice,state,cputime,args –sort pcpu | sed ‘/^ 0.0 /d’

####Watch changeable data continuously

watch -n.1 ‘cat /proc/interrupts’

cat /proc/interrupts

Check CPU Temperature
# echo `date +%b-%d-%H:%M:%S` | tr -d ‘\ 012’ ; echo -n ‘ ‘; sensors | awk ‘/CPU Temp:/{ print $3 }’

Check those commands which have been used most
# history|awk ‘{print $2}’ |awk ‘{print $1}’ | sort | uniq -c | sort -rn | head -10

Memmory commands

Free Memory  on Linux at Runtime
# sync
# echo 3 > /proc/sys/vm/drop_caches  

Display Only The Process IDs of Lighttpd

# ps -C lighttpd -o pid=
OR
# pgrep lighttpd
OR
# pgrep -u vivek php-cgi
Display The Name of PID 55977

# ps -p 55977 -o comm=

Total Memory Usage Calculation

Print Total sum of actual Memory Usages

ps aux | awk ‘{sum +=$4}; END {print sum}’

Check Actual Memory Consumption

free -m

Check total usage of One particular process

ps aux | awk ‘{print $4,$11}’ | sort |tail -n 23 |grep -w ‘someprocess’ |wc -l

All most consuming process

ps aux | awk ‘{print $4,$11}’ | sort |tail -n 20

Create many Files Sequentially
# seq -w 1 30 | xargs -i -t zcat in_Feb2011/in_Files-{}May2011.gz | grep -E ‘name.html?secsid=3304847|name.html?secsid=30780899’

Delete Empty Directories
# find folder/ -type d -empty | xargs -i -t rm -rf {}
or
# find folder/ -type d -empty -delete

Real Time Monitoring on Linux
# watch -n1 –difference “echo “Uptime”; uptime; echo \n ; ps -eo pcpu,pid,args | sort -k 1 -r |grep -v watch | head -10; echo “\n” ; tail /var/log/cron | grep “check_load” “

DISK SPACE COMMAND

Find files based and sorted on Size
# find / -type f -size +20000k -exec ls -lh {} \; 2> /dev/null | awk ‘{ print $NF “: ” $5 }’  | sort -nrk 2,2

Killing processes in one Line
# kill -9 `ps -ef | grep rsync| grep -v grep| awk ‘{print $2}’`

Check Memory Fault
# dd if=/dev/urandom bs=768304 of=/tmp/memtest count=1050
# md5sum /tmp/memtest; md5sum /tmp/memtest; md5sum /tmp/memtest

Repair Mysql MYISAM File
# myisamchk –force –sort_buffer_size=64M –key_buffer_size=16M –read_buffer_size=8M –write_buffer_size=8M ../data/phplists/phplist_linktrack.MYI

Command to make Services off in Defined Level on Linux
# chkconfig –list | awk ‘{print $1}’ | cut -d: -f1 | grep -vE ‘^crond|^network|^sshd|^syslog|^iptables’ | awk ‘{print $1}’ | while read line; do chkconfig  –level 3 $line off; count=`expr $count + 1`; echo $count $line;done

or Use this… One lineer  
# chkconfig –list | awk ‘{print $1}’| grep -vE ‘^crond|^network|^sshd|^syslog|^iptables’ | xargs -i chkconfig –level 3 {} off

Counting Hits from Web Server Access log
# awk ‘{print $1}’ /opt/indian.com/access_log | grep -vE ‘^:|^common|^-‘ | sort | uniq -c | sort -nr > /var/www/reports/ips/indian.txt

or  # awk ‘$1>10000 {print $1}’ /opt/indian.com/access_log | uniq -c | sort -nr  > /var/www/reports/ips/indian.txt

PERL Search and Replace Text Pattern using Perl On linux Platform
# find . -type f -name “*.html” | xargs perl -pi~ -e ‘s/\/js\/active18\//\/read\/js\/active18\//’

BACKUP SCRIPTS

15 2 * * * root /usr/bin/mysqldump -u root -pPASSWORD –all-databases | gzip > /mnt/disk2/database_`data ‘ %m-%d-%Y’`.sql.gz  

Details about SUID, SGID and Sticky bit permission on linux os
* SUID or setuid: change user ID on execution. If setuid bit is set, when the file will be executed by a user, the process will have the same rights as the owner of the file being executed.
* SGID or setgid: change group ID on execution. Same as above, but inherits rights of the group of the owner of the file on execution. For directories it also may mean that when a new file is created in the directory it will inherit the group of the directory (and not of the user who created the file).
* Sticky bit. It was used to trigger process to “stick” in memory after it is finished, now this usage is obsolete. Currently its use is system dependant and it is mostly used to suppress deletion of the files that belong to other users in the folder where you have “write” access to.

Numeric representation :

Octal digit Binary value Meaning
0 000 setuid, setgid, sticky bits are cleared
1 001 sticky bit is set
2 010 setgid bit is set
3 011 setgid and sticky bits are set
4 100 setuid bit is set
5 101 setuid and sticky bits are set
6 110 setuid and setgid bits are set
7 111 setuid, setgid, sticky bits are set

file : 2644
dir : 2755

Textual representation :

SUID If set, then replaces “x” in the owner permissions to “s”, if owner has execute permissions, or to “S” otherwise. Examples:
-rws—— both owner execute and SUID are set
-r-S—— SUID is set, but owner execute is not set

SGID If set, then replaces “x” in the group permissions to “s”, if group has execute permissions, or to “S” otherwise. Examples:
-rwxrws— both group execute and SGID are set
-rwxr-S— SGID is set, but group execute is not set

Sticky If set, then replaces “x” in the others permissions to “t”, if others have execute permissions, or to “T” otherwise. Examples:
-rwxrwxrwt both others execute and sticky bit are set
-rwxrwxr-T sticky bit is set, but others execute is not set

drwxrwxrwt – Sticky Bits – chmod 1777
drwsrwxrwx – SUID set – chmod 4777
drwxrwsrwx – SGID set – chmod 2777

Procedure to add a swap file

You need to use dd command to create swapfile. Next you need to use mkswap command to set up a Linux swap area on a device or in a file.

a) Login as the root user

b) Type following command to create 512MB swap file (1024 * 512MB = 524288 block size):

# dd if=/dev/zero of=/swapfile1 bs=1024 count=524288

c) Set up a Linux swap area:

# mkswap /swapfile1

d) Activate /swapfile1 swap space immediately:

# swapon /swapfile1

e) To activate /swapfile1 after Linux system reboot, add entry to /etc/fstab file. Open this file using text editor such as vi:

# vi /etc/fstab

Append following line:

/swapfile1 swap swap defaults 0 0

So next time Linux comes up after reboot, it enables the new swap file for you automatically.

g) How do I verify swap is activated or not?

Simply use free command:

$ free -m

strace
October 23rd, 2008 No comments

strace -q -f -c -p

strace -f verbose=all -e write=all -o /tmp/strace.log -p [pid]

strace -T -t -q -f -e trace=file,open,close,read -o /tmp/prod.strace.log -p <oms or ui pid>

-f traces all child processes as they are created byt he currently traced process as a result of the fork() system call.

-e is a qualifying expression which modifies which events to trace or how to race them

verbose=all dereferences structures for all system calls

write=all performs a full hexadecimal and ASCII dump of all the data written to all file descriptors

-o output file

-p process id to trace

Replace -p [pid] with [command] to trace a specific command.

Drop Caches
October 21st, 2008 No comments

Kernels 2.6.16 and newer provide a mechanism to have the kernel drop the page cache and/or inode and dentry caches on command, which can help free up a lot of memory. Now you can throw away that script that allocated a ton of memory just to get rid of the cache…

To use /proc/sys/vm/drop_caches, just echo a number to it.

To free pagecache:

# echo 1 > /proc/sys/vm/drop_caches

To free dentries and inodes:

# echo 2 > /proc/sys/vm/drop_caches

To free pagecache, dentries and inodes:

echo 3 > /proc/sys/vm/drop_caches

As this is a non-destructive operation and dirty objects are not freeable, the user should run “sync” first!

How to copy data in VI editor
January 20th, 2009 No comments

vi first  edit source file
then move your cursor to start of selection
ma       mark current position with letter a
then move your cursor to end of selection
y’a     yank to buffer x from current position to mark a
:e other  edit target file
move cursor to where you want the data
p       put from buffer x

Copying a block of text from one file to another in Vi
December 29th, 2008 No comments

To copy a block of text between files execute the commands:
Command     Explaination
1.         Edit the file containing the text you want to copy.
2.         Go to the top line to be copied.
3.     ma     Mark this line as mark “a”.
4.         Go to the bottom line to be copied
5.     y’a     Yank (y) the text from the current cursor location to the mark “a” (‘a)
6.     :split second-file     Open another window containing the second file. (This the file in which the text is to be inserted.)
7.         Go to the line where the insert is to occur. The text will be place after this line.
8.     p     Put the text after the cursor.

For Vi Editor
Do you like www.linuxnix.com ? Please consider supporting us by becoming a subscriber and get a Linux basics e-book for free.
unix_vi_editor

VI editor is the default file editor in most of the Linux/Nix machines. It is having great capabilities to edit a file with in few key strokes.

Lets start with some general information and then move on to some good things what vi editor can do for you while editing a file.
1. Vi stands for visual.
2. Vi have its variants like vim which stands for Vi IMproved, VimX11 for gui and winvi for MS windows.
3. Vi is the most popular editor and next most popular editor is gedit.
4. Do you know there is a book on VI editor from orally which is of 600+ pages.
5. Some other editors which will do the work of editing files are neno, pico, gedit, emacs, joe, nedit, ed etc.

Learning vi editor and remembering them is a very a easy task if you learn it in a systematic way.
a. Modes of VI
b. Navigational commands
c. Editing commands.
d. Search and Replace
e. Save and quiting a file.

a. Modes of VI :
Vi have two mode of operation.
1. Command mode
2. Inserting mode

Command mode :
Vi editor begins in command mode, where cursor movement(navigation in the file) and editing occur. To enter in to command mode from Inserting mode press esc button.

Inserting mode :
Used for entering text, this is similar to notepad in Windows. To enter in to inserting mode you can use any of the following.
i or I => present line
o => one line down the present line
O => one line above

Note : All comments will work in command mode only.

b. Navigational commands :
1. Character navigation k, h, l and j
h => To move one character left.
j => To move one line down.
k => To move one line up.
l => To move one character right.

How to use above commands in clever way?
Examples :
6j => to move 6 lines down from the present courser.
7k => to move 7 lines above from the present courser.

2. Word Navigation
w => word forward.
e =>word forward, but end of the word.
b => one word backward.

Examples :
32w => To move 32 words forward
6b => To move 6 words back.

3. Setting (nu) mbering to lines
:set nu
Removing of (nonu)mbering to lines
:set nonu

4. Moving paragraphs
move one paragraph up => {{
move one paragraph down => }}

5. Moving page up/down
For up => ctrl+b
For down => ctrl+f

6. Moving start/end of the file
Starting of the file(first line => [[
End of the file(last line) => ]]

7. Going to any line :
:lineno

Example :
If we want to go to 56 line then type
:56

c. Editing commands

8. Replace one letter
Replace one letter => r
Delete one letter => x

>9. Editing one word
Edit one word => cw
Delete one word => dw

10. Editing one line
Editing a line from courser to the end of that line => d$

11. Cutting
deleting(cutting) one line => dd

Examples :
2dd(deleting/cutting two lines)

12. Pasting
Pasting a line below the courser => p
Pasting a line above the courser => P

13. Coping
Copying one line => yy
Copying n lines => nyy

14. Special commands
joining lines => J
undoing things => u
repeating previous command => .

d. Search and replace

15. Search for a term /term

Example : If you want to search for suresh then press /suresh enter
/suresh
Moving to next occurrence, press “n” with out quotes moving to previous occurrence, press “N” with out quotes.

16. Searching and replacing a term(here separator is / )
:%s/searchterm/replaceterm/g
change default separator
:%s_/home/surya/grade_/home/testing/dest_g

To search and replace particular term from given line to other given line.
:%s294,304/sahana/xyz/g

e)Save and quiting a file
:w => save the file
:q => quit the file
:wq => save and quit
:w! => force save the file
:q! => force quit with out save
:wq => save and quit forcefully

==========================================================================================================================================================
Disk Space issues

mpstat -P ALL

cat /proc/interrupts

iostat -kd 1
iostat -c -t
iostat -c -t 2 10
iostat -c 3
iostat -d 5
iostat -x 1

sar -b

vmstat -S M 2 5

Find process causing high iowait

netstat -autpn | grep :80
netstat -autpn | grep :3306
netstat -na
netstat -an|grep :80|sort|more
netstat -an|grep ESTABLISHED
netstat -ntu | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -nr | more
ps ax | awk ‘$3 ~ /^D/ { print $0 }’
netstat -an | grep :80 | wc -l

DO NOT DO THIS until you are certain what is going on.

echo 100 > /proc/sys/vm/inactive_clean_percent
echo 2 10 20 > /proc/sys/vm/pagecache

==========================================================================================================================================================

To View Or List Only Directories In Linux?
Do you like www.linuxnix.com ? Please consider supporting us by becoming a subscriber and get a Linux basics e-book for free.

How to view/list only directories in Linux?
Ans : This can be achieved in two ways
1. Through ls command
2. Through find command

With ls we have to use grep to get the directory listings.
Ls –l grep ^d

Example :
[root@test surendra_a]# ls -l grep ^d
d——— 2 surendra_a surendra_a 4096 Sep 8 09:54 HTWFAIP
drwxrwxr-x 2 surendra_a root 4096 Nov 27 12:30 LinuxCBT – RHEL5
drwxrwxr-x 2 surendra_a root 4096 Oct 12 16:40 Software
[root@test surendra_a]#

With find we can have more controle on how to display only directories.

A. To display all the directories and sub-directories in present directory
#find . -type d

B. Displaying only directories in present directory
#find /root/ -type d –maxdepth 1

C. Displaying just directories in present directry and its sub-directories
#find /root/ -type d –maxdepth 2

 

How TO kill to Process

a) pgrep httpd | xargs kill -9

b) kill -9 `ps -ef | grep ‘httpd’ | grep -v grep | awk ‘{print $2}’`

Top to Cpu Usage commands

ps auxf | sort -nr -k 3 | head -10

Top processes consume RAM Memory

ps auxf | sort -nr -k 4 | head -10

Find and delete / remove files older 7 days / 1 week on Linux

find /root/server -type f -mtime +7 -exec rm -rf {} \;

Remove files older 90 days / 3 months / 1 quarter

find /var/ -type f -mtime +90 -exec rm -rf {} \;

Remove files older 365

find /var/ -type f -mtime +365 -exec rm -rf {} \;

check your public IP

n Linux, we have some best ways to check Public IP for that server.

1. curl ifconfig.me

2. wget -qO- ipecho.net/plain

3. wget -qO- ifconfig.me/ip

check your public IP