Posts Tagged ‘bash’

常用命令

March 4th, 2014

Linux

  • find /home -iname “*.log” -exec rm {} \;

    Delete file recursively by extension.

Windows

bash script for killing all mysql processes / connections

September 29th, 2011

Here is a small bash script for killing all mysql processes (or connections).

The basic idea is to select all process IDs into a tmp file (each process ID a line), and then read the file line by line and kill the process, finally remove the tmp file.

NOTE:
This bash only works for MYSQL >= 5.1.7. Becuase before 5.1.7 process list is not stored in the table INFORMATION_SCHEMA.PROCESSLIST.

USAGE:

  • either by providing the MYSQL root password as a parameter to the bash as:
    user> mysql_kill_all.sh MysqlRootPass
  • or by editing the password the line mysqlpass=$1 as:
    mysqlpass=MysqlRootPass and then excute the bash without parameter:
    user> mysql_kill_all.sh.

    Of course, you can also edit the other variables in the bash file.

——————————-
SOURCE FILE: mysql_kill_all.sh
——————————-

#!/bin/bash

# mysql host/user/pass who has the privilege to read schema INFORMATION_SCHEMA
mysqlhost=localhost
mysqluser=root
mysqlpass=$1
# tmp file path
tmpprocesslistfile=/tmp/mysql_processlist.txt

# select all process IDs into the tmp file. each line has a process id.
echo "SELECT id FROM INFORMATION_SCHEMA.PROCESSLIST INTO OUTFILE '$tmpprocesslistfile';" | mysql -h$mysqlhost -u$mysqluser -p$mysqlpass INFORMATION_SCHEMA

# read the file line by line and kill the process
while read qryid
do
echo "kill $qryid;" | mysql -h$mysqlhost -u$mysqluser -p$mysqlpass
done < "$tmpprocesslistfile"

# remove the tmp file
echo 'All connections have been killed.';
echo 'Please enter sudo password to remove the tmp file if necessary.';
sudo rm $tmpprocesslistfile

kill a process by name on Linux

November 23rd, 2010

Way 1: find the pid of the process and then kill it by pid.

Step 1: How to find pid:
$> ps aux | grep amsn
output:

weiping 7208 0.4 2.2 260684 86808 ? Sl 09:22 0:15 wish8.5 /usr/bin/amsn weiping 7804 0.0 0.0 8952 876 pts/10 S+ 10:15 0:00 grep amsn

or
$> pidof wish8.5
output:

7208

Step 2: Kill the process
$> kill 7208

Way 2: use killall

killall -9 wish8.5

Way 3: use pkill

pkill wish8.5

Way 4: use ps + kill

export sspid=`ps aux | grep wish8.5| awk 'NR==1{print $2}' | cut -d' ' -f1`;kill $sspid

bash commands

November 12th, 2010

1. for loop

for (( EXP1; EXP2; EXP3 ))
do
	command1
	command2
	command3
done

eg:

#!/bin/bash
for (( i=0; i<5; i++ ))
do
	echo "Hello $i..."
done

2. if / else / fi statements

basic grammar (note the spaces between the [ ]):

if [ EXP ]; then
	statements...
else
	statements 2...
fi

eg:

step=5
alpha=$(echo "scale=1; $step/10" | bc)
if [ $(echo "$alpha<1" | bc) -eq 1 ]; then
	alpha=0$alpha
fi

2.1 Arithmetic Comparisons

-lt <
-gt >
-le <=
-ge >=
-eq ==
-ne !=

2.2 String Comparisons

= equal
!= not equal
< less then
> greater then
-n s1 string s1 is not empty
-z s1 string s1 is empty

3. real number calculation

(echo "scale=2; EXP" | bc)

where scale=2 sets the number of the digits after decimal point.
eg1:

a=1;b=2;
c=$(echo "scale=2; $a/$b" | bc)
echo $c

output:

.50

eg2:

exp="1223/234";
d=$(echo "scale=2; ${exp}" | bc)
echo $d

output:

5.22

reference: http://linuxconfig.org/Bash_scripting_Tutorial

4. functions

basic grammar:

function div() {
    STATEMENTS
    return XXX
}>
NOTE: A bash function can only return a number. There is no way to return directly a string. So we need to use $() to capture the result outputted in the function body.
eg:
# ----------------------------------------------
# return $1/$2 with 1 digit after decimal point.
# ----------------------------------------------
function div() {
   res=$(echo "scale=1; $1/$2" | bc)
   if [ $(echo "$res<1" | bc) -eq 1 ]; then
      res=0$res
   fi
   echo $res
}
# use the function
step=5
alpha=$(div $step 10)

using bash to count lines in file

February 22nd, 2010

find *.c -print0 | xargs -0 wc -l