Dealing with Runaway Processes

Have you ever been using your Linux distro and suddenly found a program won’t close? It’s frustrating when an application hangs. In Windows, one could right click on the taskbar and choose “Task Manager” and kill the hanging process (which doesn’t always work BTW). In Linux, you can also kill these hanging processes.

First, if you’re using KDE press Control-Escape. This will give all processes in a handy window called the KDE System Guard. Clicking the column heading for “System %” so the arrow on it appears facing up will sort the processes from highest system percentage to lowest. Find the process that seems to be hogging up all the resources (or if you know the name of the process, highlight that) and then hit the kill button. Your process should end it’s routines and exit.

You can also check out which program is hogging up your virtual memory with its process which can also slow things up. Clicking on the column “VmSize” and sorting largest to smallest will allow you to see this and select which process to kill. I often elect to select only user processes using the drop down menu at the top right hand corner of the KDE System Guard. Doing this filters out all system files and shows any hanging applications that are initiated by the user (which is often what is hanging for me).

Don’t worry if you see the same process more than once (for example, Apache or php may have multiple entries if you run a webserver…this is normal). If you’re using Gnome, you’ll either have to use the console method I explain below or launch the Gnome System Manager to get things rolling. Since I don’t use Gnome, I won’t cover the Gnome System Manager here.

Another way you can do things…especially if all Xwindows (KDE, Gnome, Fluxbox, etc) have frozen or are sluggish is to drop to a console. You can do this by killing the Xserver or by dropping to a console. You can press Alt-F2 or Alt-F3 and get directly to a console. Login as root. Now let’s take a look and see what processes are hogging up resources. Kill the Xserver and drop to a console by hitting Control-Alt-Backspace. For our purposes, I’ll assume you’ve made it to the console now.

There’s a quick console way of finding exactly what is consuming the most of your PC as far as processes are concerned. Using the the ‘top’ command will display those processes that are beasts and allow you to take note of them. Look for the process taking up the most CPU% (which should appear at the ‘top’ of your ‘top’ output). Pay specific attention to the PID column of that high CPU% item and make a note of it. This is the process ID number and every program running on a Linux box is assigned one by the Kernel. We’ve found the one making problems for us and have recorded the PID so let’s slay it. Hit Control-C to stop the top command and then type:

kill PID

Where PID is the process ID number you made a note of before. You may not get confirmation that the task has been immediately killed so let’s see if it is still running. We may not get the information we need by using top again since it is mainly for finding the higher consuming processes aka runaways. Instead, let’s use the ps command.

ps aux | more

This command outputs all processes in a nice way…using the | and ‘more’ command allows you to paginate the output so that if there are a TON of processes, you can use the spacebar or arrow keys to page down (you can do that with any command too BTW). Now look for that PID that we just killed in the second column and see if it is there. You could also get creative and use:

 ps aux | grep PID

Where PID is once again the PID you killed. The grep command will search through the results and echo back to you any matching entries it finds. If you didn’t find anything and couldn’t match your PID to that of any displayed in your ps aux command, you just successfully killed that beastly process. As always, for more information, please see the man pages (e.g. man ps or man top).

Hopefully, this allows you to more efficiently manage your processes…runaway or normal. If I’ve printed an error, please let me know via the comments below or if there is a more efficient way of doing things let me know there as well…I’m always open to improvement.

UPDATE: Please checkout the comments section for a few more tips on killing processes!

UPDATE2:  Reader Scott M writes in the comments below “You don’t have to use the PID. You can use the -f option.  e.g. if there are multiple instance of SomeProgram, you can kill them all with one command:

pkill -9 -f SomeProgram”  Thanks Scott!

Alternative Screenshot Methods

Are you trying to take screenshots in Linux but don’t want to use KSnapshot or the built in Gnome Screenshot Utility? There are quite a few ways you can take screenshots in Linux. The thing is, most of them are unknown. It’s almost like the ability to take a screenshot is the best kept secret in Linux. So if taking screenshots is something you need to do on the fly or from a shell, or in a GUI that doesn’t have the utilities you need…read on.

XWD & XV

For X Window desktops such as GNOME and KDE, the command xwd -out filename.xwd lets you click a window to make an XWD image of it. Adding the -frame option will capture the entire window manager as opposed to just the open application. The popular utility xv also does screen captures so check that out as well. Use xwd and xv in the following ways:

%> xwd -root -out myimage.xwd

%> xv -grabdelay 2 myimage.jpg

IMAGEMAGICK

If you have imagemagick installed, you can use it to capture your screen as well. This is done by executing a simple command via shell:

%> import -window root wholescreen.jpg
(to grab the entire screen) or:

%> import window.jpeg

(and then click a window you want captured)

If you’re not sure whether you have ImageMagick installed, type any of the above commands just to see if they respond, or type in “man import” for help/instructions on usage.

SCREENSHOT ‘MOVIE’

There is also a really great tutorial on how to make a bash script take a ‘screenshot movie'; that is, multi-screenshots of every single window you open up.

THE GIMP

The GIMP can also be used to grab screenshots. Go to File >> Acquire >> Screenshot

FBGRAB

Another really great utility is fbgrab which can be used to capture the Linux framebuffer and convert it to a PNG image. This is handy for shell screenshots. This package is in the testing branch of Debian.

SCROT

This little utility allows you to do lots of things you wouldn’t even think of doing with screenshots such as autogenerating filenames and other things like taking screenshots of multiple displays and gluing them together. Find out how to use scrot here.

Conclusion

Screenshots don’t have to be a hassle. The use of many different utilities can simplify and streamline the process. Now you can capture various screens and show them off to all your friends…or capture various screens and hide them from your friends so they don’t know what a big Linux geek you are 😉

Do you have another method for screenshots? Visit the forum and post your method so that others can benefit. Not a member? Register here.

Photo Albums in one shell command!

Managing photo albums with programs or flat files can be time consuming and tedious. However, there is an alternative. Through the use of Imagemagick and album, two fantastic programs, you can build your own photoalbum and even design your own skins utilizing CSS (cascading style sheets). I gave this tutorial a try and I had a sharp looking photo gallery in a matter of minutes. The great thing for me is that it was all command line so I didn’t have to have an Xsession to have it build an album on my server. I just SFTP in, drop the photos, open up a telnet session, executed the command, and viola! A sharp and clean photo album in the directory I executed the code.

Give it a try and you’ll agree…this software is very nice and very handy in case you need to add a photo album to your site.

Tutorial: http://www.linuxplanet.com/linuxplanet/tutorials/5681/2/

Imagemagick: http://www.imagemagick.org/

Album: http://marginalhacks.com/Hacks/album/