How to automatically kill slow MySQL queries after N seconds?

Good MySQL related application requires right performance tuning. Without right tuning of MySQL configuration, application connections started failing and application became slowing down.

If you are using persistent connection, there may be so many connections which are running but in ‘Sleep’ mode currently. If you have MySQL >= 5.1, where there is a processlist in the INFORMATION_SCHEMA. Generate the KILL QUERY commands in bulk from within the MySQL client for query running longer than 20 minutes (1200 seconds)

Good MySQL related application requires right performance tuning. Without right tuning of MySQL configuration, application connections started failing and application became slowing down.

If you are using persistent connection, there may be so many connections which are running but in ‘Sleep’ mode currently. If you have MySQL >= 5.1, where there is a processlist in the INFORMATION_SCHEMA. Generate the KILL QUERY commands in bulk from within the MySQL client for query running longer than 20 minutes (1200 seconds):

SELECT GROUP_CONCAT(CONCAT('KILL QUERY ',id,';') SEPARATOR ' ') KillQuery
FROM information_schema.processlist WHERE user<>'system user'
AND time >= 1200

You can do WHERE clauses against the INFO field to look for a specific query, the TIME field against long running queries, or the DB field against a specific database.

To run add this query in crontab, create a shell file with following content:

SECONDS_TOO_LONG=1200
QUERIES_RUNNING_TOO_LONG=`mysql -uroot -ppassword -ANe"SELECT COUNT(1) FROM information_schema.processlist WHERE user<>'system user' AND time >= ${SECONDS_TOO_LONG}"`
if [ ${QUERIES_RUNNING_TOO_LONG} -gt 0 ]
then
    KILLPROC_SQLSTMT="SELECT GROUP_CONCAT(CONCAT('KILL QUERY ',id,';') SEPARATOR ' ') KillQuery FROM information_schema.processlist WHERE user<>'system user' AND time >= ${SECONDS_TOO_LONG}"
    mysql -uroot -ppassword -ANe"${KILLPROC_SQLSTMT}" | mysql -uroot -ppassword
fi

Shell file with above content will kill all queries running more than 1200 seconds (20 mins).

Here is another variation:

SECONDS_TOO_LONG=1200
QUERIES_RUNNING_TOO_LONG=`mysql -uroot -ppassword -ANe"SELECT COUNT(1) FROM information_schema.processlist WHERE user<>'system user' AND time >= ${SECONDS_TOO_LONG}"`
if [ ${QUERIES_RUNNING_TOO_LONG} -gt 0 ]
then
    KILLPROC_SQLSTMT="SELECT CONCAT('KILL QUERY ',id,';') KillQuery FROM information_schema.processlist WHERE user<>'system user' AND time >= ${SECONDS_TOO_LONG}"
    mysql -uroot -ppassword -ANe"${KILLPROC_SQLSTMT}" > /tmp/kill_log_queries.sql
    mysql -uroot -ppassword < /tmp/kill_log_queries.sql
fi

Shell file with above content will kill all queries running more than 1200 seconds (20 mins) with adding all queries in log file and running same log file afterwards.

How to execute a .jar file from the terminal

To execute .jar file, java command should be used as below:

java -jar {path_of_the_file}/{file_name}.jar

And to execute above command, Java package must be installed on Ubuntu PC. To check if java package is already installed, execute below command in a terminal:

java -version 

It should display current version of Java package installed.

If it displays “The program java can be found in the following packages”, It means Java hasn’t been installed yet. Execute below command in a terminal to install java package,

sudo apt-get install default-jre

This will install the Java Runtime Environment (JRE) only not Java Development Kit (JDK). If Java Development Kit (JDK) is needed, which is usually needed to compile Java applications, execute the following command in terminal:

sudo apt-get install default-jdk

That is everything to install Java. Now run first command to execute .jar file.

cPanel SoftException: GID of script is smaller than min_gid

After upgrading EasyApache in WHM, sometimes it gives 500 (Internal Server Error) error in the browser.  If you check the error_log file, you find:

SoftException in Application.cpp:363: GID of script "/home/current_user/public_html/index.php" is smaller than min_gid

OR

SoftException in Application.cpp:363: UID of script "/home/current_user/public_html/index.php" is smaller than min_uid

If you check the permission of user/group for this file, it gives you root. So, apache can’t read these files uploaded by the root user. One solution is to change the permission of user/group to your current user.

chown current_user:current_user /home/current_user/public_html/ -R

This will solve the permission related issue on your site.

References :
http://www.flynsarmy.com/2011/10/cpanel-softexception-uid-is-smaller-than-min_uid/
http://forums.eukhost.com/f15/how-solve-error-softexception-application-cpp-303-a-6205/

vi Editor: UNIX

vi is a command-line text editor originally created for the Unix operating system.

The name vi is derived from the shortest unambiguous abbreviation for the command visual; the command in question switches the line editor ex to visual mode.

Most of the network administrators are familiar with this little editor in Unix, because they use it regularly. But, for first timers, it’s most difficult editor. First timers have to remember all commands and keys to edit a simple file.

vi has two modes, Insert mode and Command mode. In insert mode, you can add/edit the texts in file. And in command mode, you can navigate and command the editor like save, exit, copy, paste, etc.

These are the commands and keys for those who want to get familiar with vi editor.

Command to open the vi editor:

vi filename

This command creates a new file if filename is not available in current directory. By default, vi begins in command mode.

To start the insert mode, you can use following keys:

Insert text at beginning of line:

I

Insert text at cursor:

i

append text after cursor:

a

Append text at line end:

A

Open line above cursor:

O

Open line below cursor:

o

To switching back, and start the Command mode, press [ESC]

Most commands execute as soon as typed except for “colon” commands which execute when you press the return key.

For cursor movement in command mode, you can use following commands/keys:

Go to beginning of line

0

Go to end of line

$

Go to line number ##

:##

Go to line n

nG

Go to last line

G

Left 6 chars

6h

Move left, down, up, right

h j k l

Move left, down, up, right

← ↓ ↑ →

Scroll Backward 1 screen

[ctrl] b

Scroll Forward 1 screen

[ctrl] f

Scroll by sentence forward/backward

( )

Scroll by word forward/backward

w b

Scroll by paragraph forward/backward

{ }

Scroll Up 1/2 screen

[ctrl] u

Scroll Down 1/2 screen

[ctrl] d

For deleting/changing text/character in command mode, you can use following commands/keys:

Change word

cw

Replace one character

r

Delete word

dw

Delete text at cursor

x

Delete entire line (to buffer)

dd

Delete (backspace) text at cursor

X

Delete 5 lines (to buffer)

5dd

Delete current to end of line

D

Delete lines 5-10

:5,10d

For editing content in command mode, you can use following commands/keys:

Copy line

yy

Copy n lines

nyy

Copy lines1-2 /paste after 3

:1,2t3

Move lines 4-5/paste after 6

:4,5m6

Paste above current line

P

Paste below current line

p

Undo all changes to line

U

Undo previous command

u

Join previous line

J

Find next string occurrence

n

Search backward for string

?string

Search forward forstring

/string

% (entire file) s (search and replace) /old text with new/ c (confirm) g (global – all)

:%s/oldstring/newstring/cg

Ignore case during search

:set ic

Repeat last command

.

For saving and quiting in command mode, you can use following commands/keys:

Save changes to buffer

:w

Save changes and quit vi

zz or :wq

Save file to new file

:w file

Quit without saving

:q!

Save lines to new file

:10,15w file

In all of the above commands, a number n will tell vi to repeat that command n times.

:syntax on Turn on syntax highlighting
:syntax off Turn off syntax highlighting
:set number Turn on Line numbering (shorthand :set nu)
:set nonumber Turn off Line numbering (shorthand :set nonu)

:set ignorecase Ignore case sensitivity when searching
:set noignorecase Restore case sensitivity (default)