Archive

Archive for January, 2009

Zapping processes in Bash

January 24th, 2009 No comments

The following is a handy little script I wrote which politely asks a collection of matching processes to exit. After a second the process is forcibly killed.

This command is designed as a drop-in replacement for the slightly cryptic pgrep and pkill commands.

?Download zap.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
zap() {
	# Usage: zap [SIGNAL|-NUMERIC|shoot] <fgrep>
	SIGNAL=''
	case "$1" in
		shoot|-0) SIGNAL='SHOOT';;
		term|-15) SIGNAL='TERM';;
		kill|-9) SIGNAL='KILL';;
		hup|-1) SIGNAL='HUP';;
	esac
	if [ "$SIGNAL" != '' ]; then
		shift
	else
		SIGNAL='SHOOT'
	fi
	PROCS=`ps ax -eo pid,comm | fgrep "$1"`
	MODE=0
	for DATA in $PROCS; do
		if [ "$MODE" -eq 0 ]; then
			PID="$DATA"
			MODE=1
		else
			CMD="$DATA"
			MODE=0
			if [ "$SIGNAL" == 'SHOOT' ]; then
				echo -n "Shooting #$PID - $CMD..."
				kill $PID
				sleep 1
				kill -9 $PID
				echo "Shot"
			else
				echo -n "Killing #$PID - $CMD..."
				kill $SIGNAL $PID
				echo "Killed"
			fi
		fi
	done
}

To install it simply dump the above text inside your existing ~/.bashrc file.

Usage is quite simple:

To politely kill all processes (then force-killing if it still does not die) containing the string ‘gnome’:

1
zap gnome

To just kill-forcefully:

1
zap -9 gnome
Categories: HowTo's Tags:

Autorun as Administrator in Vista

January 18th, 2009 3 comments

When asked how to automate the installation of software the other day I was a little dismayed that no-one seemed to have covered the topic of auto-running a batch file from a USB disk as an administrative user.

Admin level access is obviously required in order to install software without relentless ‘Program X requires administrative access’ UAC prompts.

Like most things in Vista the solution is exceptionally stupid and requires significant trips with Google to rather obscure second-hand Microsoft knowledge base articles.

The overview is something like this:

Autorun as admin process diagram

First create the autorun.inf file:

?Download autorun.inf
1
2
3
4
[autorun] 
open=invoke.bat 
icon=autorun.ico,0
label=Setup computer from USB disk

So this file simply sets up the autorun.inf file which contains exactly what to list in the Vista ‘Autoplay’ dialog when the USB disk is inserted. You will probaly want to rename the ‘Label’ line and perhaps put a pretty icon on your disk called ‘autorun.ico’.

Now create your actual batch file that you wish to run as admin and call it ‘setup.bat’.

As a small note of caution. For some reason Vista does not change to this files active working directory when executing. I can really think of no reason this behavior exists so you might want to add the following rather cryptic command as the first in the batch file. This changes the active directory into the current batch file (in this case ‘setup.bat’) directory.

?Download setup.bat
1
2
@ECHO OFF
cd /d %~dp0

There obvious when you think about it isn’t it. Shame on you for not knowing about the handy and invisible shell variable “~dp0″. I am sure there is a long and interesting story about why this variable is so called and why it doesnt show up with the rest of things when you type ‘set’. This is why I switched to Linux some time ago.

Anyway, now we’ve done that create a shortcut to the ‘Setup.bat’ file and rename it to ‘setup.lnk’ (remember that the ‘.lnk’ bit will always be invisible to you anyway so when you rename in explorer all you will see is the ‘setup’ bit sans extension).

Open the shortcuts properties and hit the ‘Advanced’ button. Tick the ‘Run as Administrator’ box.

Finally, lets join it all together by creating the ‘Invoke.bat’ file which runs the shortcut (which will in turn run the ‘setup.bat’ program after asking via UAC for administrative permission).

?Download invoke.bat
1
2
@ECHO OFF
"Setup.lnk"

Now save everything and remove you disk (safely) and re-insert. You will now be able to run the ‘setup.bat’ file which should contain all your auto-install scripts after a UAC admin prompt. Yey.

Categories: HowTo's Tags: , ,

Pretty URLS in CodeIgniter

January 12th, 2009 No comments

CodeIgniter has to be my favorite framework for PHP. The way it keeps out of your way while working to an MVC standard is something deserving of the highest praise.

For reasons passing understanding though the programmers of this excellent system have desided not to provide pretty URL’s out of the box. Getting infomation on this is a little tricky so here is my take on the situation.

Simply dump the following into a file called ‘.htaccess’ (Note the starting Dot) in the root path (thats the one at the very start of your path tree that should contain the ‘system’ folder).

?Download .htaccess
1
2
3
4
5
6
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$		- [L]
 
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

As with most mod_rewrite hacks you will need that enabled on an apache server. Its very rare to find a hosting company that doesn’t use this setup so you should be fine.

You will also need to change the following variable to a blank value in the system/application/config/config.php file:

1
$config['index_page'] = '';

And you’re done. From now on your Urls are addressed in the form: http://SITE/CONTROLLER/METHOD/VALUE1/VALUE2 (e.g. http://mysite.com/users/edit/123123)

Categories: HowTo's, PHP Tags: , , ,