Monthly Archives: January 2008

how to: using bash to kill a parent process and all spawned child processes

I got this project in a Linux environment where I have to terminate a process before running one. Sounds easy at first glance, it’s very easy to kill a process in Linux, all I have to do is get the process’ PID (process id) and terminate it using the kill command…. HA! A no-brainer problem!

But after thinking about it for a few minutes, it hit me, it’s possible that the process that I want to terminate can spawn child processes, and it’s child processes can spawn another set of child processes… and so on… and so forth… and I have to terminate all these child processes too! Aaarrggh!

And so, all because I’m lazy, I opened up Google and did a little script hunting. I found some tips but it doesn’t fit my needs. Most solutions were how to kill one level of child processes only (or maybe I did not try hard enough). After a few hours wasted on looking for a “already-done-by-others” solution, I gave up and decided to write my own….

So much for being lazy… So, on to the drawing board…
Well, I hope you can make sense of what’s the diagram above (don’t ask me how I came up with it…). If you don’t know what it is, just believe me when I say it’s a tree.

The behaviour of a process spawning child processes can be described by a tree. Based on this, all we have to do is determine the nodes at each depth. The idea is to store process ids in an array based on what depth in the process tree they belong. After generating the array, we can decide if we want to terminate processes from parent to child or vice versa. In my case, I have to terminate from parent to child.

Based on that gibberish idea above, I managed to write the following code:

#!/bin/bash

ids[0]="$1"

index=0
quit=0

while [ $quit -eq 0 ]
do
    ((index++))

    # get all child processes spawned by this/these ppid/s
    ids[$index]=$(ps -o pid --ppid ${ids[$index-1]} | \
      pcregrep '\d+' | tr \\n ' ')

    # if no child processes found
    if [ ! "${ids[$index]}" ]
    then
        # quit
        ((quit++))
    fi
done

# kill process from parent to all child processes
for i in $(seq 0 ${#ids[@]})
do
    if [ "${ids[$i]}" ]
    then
        kill ${ids[$i]}
    fi
done

The code above assumes that the root process id is known. You may have to do some checking first if you have a valid root process id as parameter.

And that’s it! I’m just hoping that my laziness can appreciate fruition…

trying out kde 4

KDE 4 shows a lot of promise when it was first announced that’s why I’ve been waiting for it with much anticipation for quite some time now. It was scheduled for release last year but, unfortunately, it was postponed… The day came though, it was finally released last January 11th. And so, given that I’ve been itching to put my hands on it, I decided to install and check it out.

Installation is easy, instructions are that simple.
Ubuntugeek has a nice article for that.

In summary, all you have to do is follow these steps:

add the kde 4 repository to sources.list
xxx@mybox:~$ sudo echo -e “\ndeb http://ppa.launchpad.net/kubuntu-members-kde4/ubuntu gutsy main” >> /etc/apt/sources.list
xxx@mybox:~$ sudo apt-get update

remove previously installed kde4 packages
xxx@mybox:~$ sudo apt-get remove kdelibs5 kde4base-data kde4libs-data
xxx@mybox:~$ sudo apt-get install kde4-core

After I installed the KDE 4 core packages, this is where my disappointments surfaced…
Now I have the following problems…

  1. WiFi connection’s not working… and I can’t configure it.
  2. keyboard shortcuts are not working…
  3. sometimes the desktop freezes when I change the desktop theme…
  4. (this is where I decided that it’s not ready to replace my current set-up yet)

I managed to make the WiFi working by using GNOME’s network manager applet. Since I have no shortcut keys, I have to run it by right-clicking on the Desktop then the “Run Command” option or via konsole:


xxx@mybox:~$ nm-applet –sm-disable


On the bright side, the desktop looks great though…


To wrap it up, KDE 4’s not that stable yet, but given that it’s newly released, I expect it to have some rough points. The KDE team did a great job on developing it, so expect that rough points will be polished in the next coming months. And that’s something to look forward to…

having fun with super macro

Just got back from vacation about a week ago. I’m swamped with work stuff, so it took me this long to post a new blog.

June and I took some pictures using her camera, a Canon Powershot A460, last Christmas vacation and it was a lot of fun! (pardon me, I know digicams existed for a long time now, but it was just recently that I was able to play with one… intimately)

(June woke up early just to take these shots…)

IMG_0762IMG_0775

IMG_0764euphorbia

IMG_1227IMG_0745

(and here are some shots that I took with willing insects to pose for me…)

IMG_1014IMG_0830

IMG_0831IMG_0829

You can download the full size photos from my flickr account if you want to use it as wallpapers.

Beware! Taking pictures is addicting! Now I know why people are that crazy to spend on DSLR cameras… I feel nothing but self-pity for I can’t afford one… 😦