Bash

Superkill - A Quickie Script for Killing All Instances of an Application

Evolution crashed on me this morning and I kept getting an error message: "Evolution appears to have exited unexpectedly...". Every time I started the application, it provided that message then locked so I went to my trusty command line. I did a ps -ef | grep evo and saw that several evolution-based processes were running:
steven    7885     1  0 09:57 ?        00:00:08 evolution --component=mail
steven    7894     1  0 09:57 ?        00:00:00 /usr/lib/evolution/evolution-data-server-2.22
steven    7918     1  0 09:57 ?        00:00:00 /usr/lib/evolution/2.22/evolution-exchange-storage
steven    7931     1  0 09:57 ?        00:00:00 /usr/lib/evolution/2.22/evolution-alarm-notify 
steven   11346  7715  0 11:12 pts/1    00:00:00 grep evo
I then proceeded to kill each process manually. It's only 4 but it was enough to bring out my inner programmer and solve a repetitive task programatically. So I decided to add a couple more pipes. I looked at the results from the grep'd ps and noticed that all process IDs are in character slots 10-14 so I cut -c 10-14 the processes that were piped in. That printed out as expected so I fed that to kill via xargs and, voila!, all those processes were killed. Not content to have to type all the pipes every time I wanted to do it, I threw the pipe in a BASH script that I lovingly call 'superkill' and take the first argument as the string to grep and kill the returned processes. The code is below:
#!/bin/bash

prog=$1

#echo ${prog}
ps -ef | grep ${prog} | cut -c 10-14 | xargs kill
Simple enough but quite powerful. I then threw the script in my /usr/bin directory so my PATH would pick it up. Now I can just superkill evolution (sounds like a creationist book, I know) and have all the processes ended. Here is the file. Just extract it (tar xzvf superkill.tar.gz) and put the resulting file in your /usr/bin directory. Then superkill away! Of course this code comes with no warranty and you assume all risks. Before you run the script you may want to call a ps -ef | grep <processname> before doing superkill <processname> so you make sure there are no unexpected results. I could echo the resulting process and ask for confirmation, I suppose. Hold for that version (depends if I get feedback it works well enough for my purposes).

Install Initial Ubuntu Items GUI

Because of all the great response from the simple shell script, I've created a GUI using Zenity and Bash so that people new to the CLI or those who wish to select and deselect what packages to install or uninstall may do so. You'll be able to play DVDs, listen to your MP3s, have 5.1 Surround Sound playback enabled and more. This isn't a 'how-to' but rather a 'just-do-it'. If you want to know the how-to, just view the source of the script. It's basically doing some simple apt-gets with a GUI front-end.

Below is a screenshot of the window. Just download the shell script here on my server, -- you'll need to extract it (tar xvf ubuntuTasks.sh.tar.gz) then make sure it's executable (chmod +x ubuntuTasks.sh) and execute it (./ubuntuTasks.sh). You'll be greeted with a user-friendly GUI where you can select and deselect items.


steven@steven-laptop:~/fun$ chmod +x ubuntuTasks.sh
steven@steven-laptop:~/fun$ ./ubuntuTasks.sh 

Once you hit OK, you'll have to input your sudo password when it returns to the CLI. It shouldn't prompt you for anything else. Please shoot me a message if you need any help or have any suggestions.

Initial Ubuntu Install Items Script

UPDATE: Please go here for the new GUI version of this script that also includes some extra items.
I was actually going to write an entry this morning about packages to purge or install upon a fresh Ubuntu build but Ubuntu Linux Help beat me to the punch. It's a great article and well worth the read. I've modified the list slightly and created a bash script to execute it. The file is here. Just extract it ( tar xvf ubuntuTaks.sh.tar.gz ) make sure it's executable ( chmod +x ubuntuTasks.sh ) and execute it using ./ubuntuTasks.sh
Below is the code inside in case you want to just copy and paste it into your own file:

#!/bin/bash

sudo apt-get remove --purge mono-common libmono0
sudo aptitude install sbackup
sudo aptitude install ubuntu-restricted-extras && sudo aptitude install w32codecs
sudo apt-get install msttcorefonts && sudo fc-cache -fv
sudo aptitude install vlc
sudo aptitude install k3b
sudo perl -pi -w -e 's/\; default-sample-channels \= 2/default-sample-channels \= 6/g;' /etc/pulse/daemon.conf

As always, this code comes with no warranties. I have run it on my system and all is well.

Quickie: Deleting Old Files

Many people have the need to delete files older than a certain range. For instance, you may want to remove all podcasts that are older than a certain time period or delete other files that you receive regularly. Making use of the find command and mtime, you are able to delete them with ease.

find . -name *.ogg -mtime +7 -exec rm {} \;

The above one-liner removes all my ogg-based media files in my current directory that are more than a week old. It is best to use absolute paths -- ESPECIALLY IF RUNNING BY CRON. So the cron-safe code looks like this:

find /home/steven/music/ -name *.ogg -mtime +7 -exec rm {} \;

You can also use this with other command such as moving the file, batch renaming them (if you want all older files to have a .bak extension or whatnot), as well as many other uses. To move the files, simply:

find /home/steven/feeds/ -name *.csv -mtime +30 -exec mv {} /home/steven/feeds/old \;

With enough MAN pages and will, you can do all kinds of cool stuff.

Quickie: Access Remote GUI Programs Using SSH Forwarding

This is a cool tip from Linux Journal

Basically you do an ssh session, throw in the C and X switches and specify what program to execute. Something like:

ssh -C -X user@coderswasteland.com gnome-terminal

Sweet, huh?

Quickie: Batch Renaming Files in Linux

For those wanting to be able to quickly and efficiently rename a bunch of files all at once, below is some example code to help you achieve that:

ls *.xml | sed 's/\(testing\)\(.*\)/mv \1\2 production\2/' | sh

The first piece ls *.xml simply lists the file types we want to change. You can, of course, alter this to anything you'd like.

The second part uses sed so search for a text and replace it with another. There are numerous regex tutorials around the net.
Within the second part, items are grouped so that I may reuse these items in the next part. Specifically, I am looking for (part1) the word "testing" and (part2) anything else. The mv command then takes as the first parameter the original two values to denote what file is to be changed and the second parameter of the command does not use the first variable but rather the word "production" and then appends whatever was in the second variable.

Part 3 simply executes this.

The end result makes a sample file named:
testing_1.xml
be
production_1.xml

Quickie: Burning Video DVDs Command Line Linux

Make sure you have dvdbackup and growisofs/mkisofs. (Your usual sudo apt-get install in Debian/Ubuntu).

Simply:

dvdbackup -i /dev/dvd -M -o </your/output/path>

growisofs -speed 1 -dvd-compat -Z /dev/dvdrw -dvd-video </your/output/path/videodir>

Example:

dvdbackup -i /dev/dvd -M -o /home/steven/Documents/VID/

growisofs -speed 1 -dvd-compat -Z /dev/dvdrw -dvd-video /home/steven/Documents/VID/MY_BACKUP_VIDEO/

The second path is where the first command wrote to. Basically it will be one folder deeper than that first path. It is the path that contains the VIDEO_TS directory.

Referenced from:
http://ubuntuforums.org/showthread.php?t=133642

Latest Video


only search Coder's Wasteland
Powered by Drupal, an open source content management system

Digg this