Blog is moving

My blog is moving to http://victormendonca.com/blog/. If you are looking for a specific or older post you are in the right place Otherwise check out my new page for more up to date content.

Wednesday, March 26, 2008

How to kill running processes with multiple instances


I have my .bashrc file setup with many aliases, you can see more here. One of them is to kill processes, but sometimes it's not useful for running processes with multiple instances.

My alias looks like:

alias fproces='ps x | grep '


So when I get a process or an app that I want to kill I can do the following:

$ fproces gedit
7892 ? S 0:00 gedit
7894 pts/1 R+ 0:00 grep gedit
$ kill -9 7892


This is not that useful for applications/processes like Firefox:

$ fproces firefox
5994 ? S 0:00 /bin/sh /usr/bin/firefox
6006 ? S 0:00 /bin/sh /usr/lib/firefox/run-mozilla.sh /usr/lib/firefox/firefox-bin
6011 ? Sl 54:24 /usr/lib/firefox/firefox-bin
7898 pts/1 R+ 0:00 grep firefox
$ kill -9 5994 6006 6011


So I came up with a little script that does the following:

$ finish.sh
Enter app name
firefox
5994 ? S 0:00 /bin/sh /usr/bin/firefox
6006 ? S 0:00 /bin/sh /usr/lib/firefox/run-mozilla.sh /usr/lib/firefox/firefox-bin
6011 ? Sl 54:38 /usr/lib/firefox/firefox-bin

Do you want to kill all items? [y|n]


The script lists all processes related to the app and kill it if you say “y”. In this example I typed in “firefox” when prompted for an app name.

Here is what the script looks like:

#!/bin/bash

## reads process name and assign it to var app
echo "Enter app name"
read app

## sets a minimum size for the var app. Without this, you could easily kill all running processes
minim=`echo "$app" | wc -m`

if [ "$minim" -lt "4" ] ; then
echo "Please try again with an app name"
exit 1
fi

## looks for the running process instances, lists it and asks if you want to kill all of them
ps x | grep "$app" | grep -v 'grep'
echo
echo "Do you want to kill all items? [y|n] "

## reads the answer and acts if it's an “y”
read resposta
if [ "$resposta" = "y" ] ; then
termina=`ps x | grep "$app" | grep -v 'grep' | awk '{print $1 ; }'`
for i in `echo $termina` ; do
kill -9 "$i"
done
fi



Vic.

Sunday, March 23, 2008

How to get email and sms notifications from your server


Ever wondered if you could make you server send you an email or a sms to your cell phone if it ever went down, or if you logs folder were too big, or any other event that you can think? Well, here's a really simple way of doing so.

In my case, I'll be using two events, one if the server goes down, and another if my logs folder gets too big (I have a very simple Linux box that does not have file rotation).


1- Mayday, server is down

There are two ways we can do this, and it will depend on how often do you need to check if the server is down. If you need it to be done every minute or so, I'd advise using a service instead of crontab. I could not personally say what's the system hog difference, but I'm open for feedbacks. In my case I'll be checking every hour, so I'll use crontab.

Other thing required is a cli email client, and there are many you can use. I choose msmtp, which is extremely easy to configure. Here's what I have set to use my gmail account:

defaults
tls on
tls_certcheck off
logfile ~/.msmtp.log
account gmail
host smtp.gmail.com
port 587
from me@gmail.com
auth on
user me@gmail.com
password ######
account default : gmail


To send an email is as easy as:

$ echo -e "Subject: Test \n\n This is the message body" | msmtp -v email@address1 email@address2


So now that we have means to send the message, let's create the script. Remember that the script will need to be run from another PC and not from the server. A server that is down cannot send emails!!

#!/bin/bash

## pings the server IP. Here I'm using only one ping as some distros ping 4x times (Fedora), and others will ping consecutively by default (Ubuntu)
ping -c 1 server

## checks if the ping was successful or not, if not it sends an email and a sms with the last time it tried to ping
if [ “$?” != “0” ] ; then
echo -e "Subject: Server is down \n\n Victor, \n Server is down. Last time tested was $(date)." | msmtp -v me@adreess.ca 12312345678@cellprovider.com
fi


Now we add the script to your crontab and send any error msg to a txt. Make sure that your account has pinging privileges

$ crontab -e
# Check if server is up every hour
0 * * * * /home/user/bin/script.sh 2>> /home/user/var/log/server-up-log.txt



2- Checking /var/logs size

The process here will be almost the same, however both script and crontab need to be run on the server. Our script should look something like:

#!/bin/bash

## Some txt editing. We will check /var/log size by using “df” and then striping it down to numbers only
e=`df -h | grep 'var\/log' | awk '{ print $5 ;}' | sed 's/%//'`

## If the size of /var/log gets higher than 75% we will get an email and a sms
if [ "$e" -gt "75" ] ; then
echo -e "Subject: Log folder \n\n Victor, \n Log folder has reached 75%" | msmtp -v me@adreess.ca 12312345678@cellprovider.com
fi


Add the script to your crontab on the server and you are done.

Have fun!!!


Vic.

Sunday, March 9, 2008

Quick and simple Quake Terminal


For the gamers out there, you know exactly what I'm talking about. I've never been big with gaming, but the idea of having a shortcut to shade a terminal window and bring it back whenever I want is pretty neat.

I already run a small terminal window as part of my desktop, but sometimes its not as flexible as a normal window. I'll write a post on how to use a terminal as a desktop background in the future. For now you can see a pic here.

To achieve what we want we will use a utility called wmctrl, which is on the Ubuntu repos (if I'm not mistaken the Universe repo). There are other ways you can achieve the same (like Tilda).

$ sudo apt-get install wmctrl


wmctrl bases it's commands on window name, so to make sure you have the proper name for the terminal window, open it with the desired profile and type:

$ wmctrl -l
0x03800021 -1 hostname Left
0x03c000cc 0 hostname Jinzora Media Jukebox - Genres - Mozilla Firefox
0x01407084 1 hostname blog - File Browser
0x04400057 1 hostname quake-terminal - OpenOffice.org Writer
0x0380859b 1 hostname victor@hostname: ~


These are the list of windows I have open. My terminal window is called “victor@hostname”, so this is the name I'm going to use. Next, I'll open my favorite text editor and add the following lines to a script:

#!/bin/bash

if [ -f ~/bin/temp/quake ] ; then
wmctrl -r 'victor@hostname: ~' -b remove,below
wmctrl -r 'victor@hostname: ~' -b remove,shaded
rm -r ~/bin/temp/quake
else
wmctrl -r 'victor@hostname: ~' -b add,below
wmctrl -r 'victor@hostname: ~' -b add,shaded
touch ~/bin/temp/quake
fi


Here's a breakdown to make it easy. I have a folder on /home/victor/bin with many scripts I created, so I'll add this one as well, and I called it ter-quake.sh. Next, I created a temp folder inside my bin folder to generate a file that will check if my script was run previously or not. And here's the explanation for the script

#!/bin/bash

## This first line checks if the file /bin/temp/quake was created, which meas the script was run before
if [ -f ~/bin/temp/quake ] ; then
## if the script was run before, it will unshade the terminal window and place it on top
wmctrl -r 'victor@victor-laptop: ~' -b remove,below
wmctrl -r 'victor@victor-laptop: ~' -b remove,shaded
## here we remove the file
rm -r ~/bin/temp/quake

## otherwise, if the file does not exist
else
## the sript will shade the window, place it bellow all others and...
wmctrl -r 'victor@victor-laptop: ~' -b add,below
wmctrl -r 'victor@victor-laptop: ~' -b add,shaded
## create the file
touch ~/bin/temp/quake
fi


Save the script and make it executable:

$ sudo chmod a+x ~/bin/ter-quake.sh


Last step, we will add a keyboard shortcut for the script. I've achieved that by using gnome's gconf-editor:

- Type “Alt+F2” and enter “gconf-editor”
- Browse to “/apps/metacity/keybinding_commands”
- Choose any of the empty commands and add the path to your script
- Go to “/apps/metacity/global_keybindings” and under “run_command_#” add the shortcut you would like to use. I've used q.


Vic.


Note: This is another useful topic from Linux Journal that I wanted to share. Issue 167. Link

Saturday, March 8, 2008

How to check for a running screen when logging via ssh


For some of you that might have never used screen, it's a program that allows you to create multiple virtual terminals inside one terminal window. You can even break the terminal window into two screens.

I'm not getting into details for the program's usage. You can get more information on these two links:

GNU Screen
How to split screen your command line

In my case, because screen preserves a running program even if your ssh session has lost connection, I use it to download torrents via ctorrent on one of my servers. So when I connect back to my server to check the torrent download status, I have to issue a command like the one bellow to check for running screens, and then connect to it:

$ screen -ls
There is a screen on:
7583.pts-0.pluto (Detached)
1 Socket in /var/run/screen/S-webm.

$ screen -r 7583


A simpler way of doing this would be:

$ screen -DD -R


This command will check for a running screen, and if none is available it will create one. So to make my live a bit easier (and my login a bit fancier), I've added a little script to my ~/.bash_profile (Ubuntu) to prompt me if I want to connect. This is what I get when I login:

Last login: xxxxxxxxxxxxxxxxxxxxxxxxxxxx
There is a screen on:
7583.pts-0.pluto (Detached)
1 Socket in /var/run/screen/S-webm.

A screen was detected, would you like to connect? [y|n]


To achieve that, I appended the following script to my .bash_profile:

## Check for a running Screen
scrtst=`screen -ls | grep 'No Sockets' > /dev/null`
if [ "$?" != "0" ] ; then
screen -ls
echo "A screen was detected, would you like to connect? [y|n] "
read opcao
if [ "$opcao" = "y" ] ; then
screen -DD -R
fi
fi


There are many different ways you can do this. You could even check how many screens are running and list an option to connect to, but I'll leave that up to your imagination.


Vic.



Notes:
- Opcao means option in Portuguese. I like using Portuguese words for variables.
- This idea was adapted from a Linux Journal column - Tech Tip, Use Screen to Avoid Loosing Remote Work - Issue 167