Skip to main content

Posts

Showing posts from January, 2012

Self terminating code for bash scripts

Self terminating code for bash scripts is very easy. But have to be sure not to kill itself in the process. The procedure is simple. Find all the PID with the same name as itsself $0 and then kill all those instances one by one in a for loop except itself $$ (PID of its own).
This is a very general script and could be used anywhere and in any script.

=================================== if [ "$1" != "" ] ; then # if no command line parameter supplied then move on. if [ "$1" == "kill" ] ; then # if command line parameter is "kill" then kill all running instances of this script. nnn=`ps aux | grep $0 | tr -s " " | cut -d" " -f2 ` echo -e $$ "\n====" # prints PID of this instance of script. make sure not to kill itself. for ii in `echo $nnn` do if [ "$ii" -ne "$$" ] ; then echo "Killing PID:" $ii kill -9 $ii fi done exit fi fi

Random wallpaper change script for Linux

This script would change wallpaper at regular intervals in a random manner. The script can also be killed at any time from any terminal. just like pulseaudio. #For eg. my-laptop>$ ./wallpaper-changer.sh kill
Usage: wallpaper-change.sh [option] [option] = kill : Kill all running instances of the script = change : Change the wallpaper = dir : Change the wallpaper directory to = -h : Display this help.
=============================================== #!/bin/bash # Wallpaper randomizer script # written by JOnes v4.1.5 # this script keeps rotating the wallpapers at a fixed interval and does it randomly. # It goes into background. You can either kill it manually or use the "kill" switch as a command line parameter.

# location of wallpapers / pics. wallpaperdir="$HOME/Pictures"
# wallpaper change interval time in seconds. Cannot changed during runtime. changetime=1800


# do not have to edit beyond th…

find if another instance of the same script is running in background

A bash script snippet to find if another instance of the same script is running in background. This code snippet is very useful for those scripts which have to stay in the background and do some scheduled tasks or just plain monitoring like the laptop battery. It is supposed to be included at the beginning of the code. It would check if another instance of the script is running as a background job. If yes then it issues a warning and terminates the script.
A note of warning : For multicore CPUs it is a tricky deal to figure the actual number of instances. If CPU is dualcore then echo ${run_count} outputs 2 for the first run. And for the second time around it outputs 3. The extra number is the suspended instance of the script. For quadcore the number would go as 4 -> 5 -> 6. For for the first time just run without the comment on echo ${run_count} and then set the value of exp_count (2 for dualcore , 4 for quadcore etc.).

================================================
process=$0 #…

Dopamine induced learning

While watching the killer whales and dolphins perform at the San Diego waterworld, I wondered what motivates these animals to display such acquired skills. I understand that it is the food that they get which prompts such abnormal behavior. But where did the process of learning triggered in those primitive brains. I believe it is the Dopaminergic neurons that are responsible for the learning behavior. These neurons have a predictive error response, which means they give a good stimulus to the brain in a reward comes along. But if expected rewards are not obtained it causes depression. This is known as motivation to us. If we get good results in a job we get motivated to do it. If not we get depressed. So, logically we always do something which gives maximum reward, that is, we always try do something we are good at. Wikipedia article -- "In nature, we learn to repeat behaviors that lead to maximizing rewards. " This is true even for animals. They repeat the behavior which re…