Wednesday, September 27, 2006

[Perl] Script to take backups

#!/usr/bin/perl
# Written By Vaibhav Gupta
# vaibhav.gupta@gmail.com
# Modify @FILES_TO_BACKUP @DIRS_TO_BACKUP @FILES_PATH as per your need.

$Directory=`date +%Y%m%d%k%M`;
chomp $Directory; # you know why ?
$HOMEDIRECTORY="~guptav/";
@FILES_TO_BACKUP=(".vimrc",".bashrc","public_html/cgi-bin/index.cgi");
@DIRS_TO_BACKUP=( "cvsroot","public_html/","bin");
@FILES_PATH=("/etc/httpd/conf/httpd.conf");

print "\nCreating directory $Directory\n";
@message = `mkdir $Directory`;

foreach $filename (@FILES_TO_BACKUP) {
@message = `cp $HOMEDIRECTORY$filename $Directory`;
}
foreach $filename (@FILES_PATH) { #Absolute Filename
@message = `cp $filename $Directory`;
}
foreach $dirname (@DIRS_TO_BACKUP) {
@message = `cp -r $HOMEDIRECTORY$dirname $Directory`;
}
@message = `tar -cvzf $Directory.tgz $Directory`;
print @message;

#Vaibhav.

Monday, September 11, 2006

[shell] To find the PPID of a PID and more.

1 #!/bin/bash
2 pid=$1
3 if [ -f "/proc/$pid/stat" ]; then
4 name=`cat /proc/$pid/stat | cut -d" " -f 2`
5 else
6 echo "Not a Valid pid $pid"
7 exit
8 fi
9
10
11 while [ "$pid" -gt "0" ]; do
12 echo -n $pid
13 echo -n [$name]
14 echo -n " --> "
15 name=`cat /proc/$pid/stat | cut -d" " -f 2`
16 pid=`cat /proc/$pid/stat | cut -d" " -f 4`
17 done
18
19 echo "0"
20

-Vaibhav

Writing Kernel Modules for 2.6

hello.c
1 #include <linux/module.h>
2 #include <linux/kernel.h>
3
4 int init_module(void)
5 {
6 printk(KERN_INFO "Hello world \n");
7 return 0;
8 }
9
10 void cleanup_module(void)
11 {
12 printk(KERN_INFO "Goodbye world \n");
13 }

Makefile
1 obj-m += hello.o
2
3 all:
4 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
5
6
7 clean:
8 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
9 fresh: clean all

Commands:

Install Module# insmod hello.ko
Remove Module # rmmod hello.ko
Module info # modinfr hello.ko
List Module # lsmod
Check Log # tail /var/log/messages

Files :
/proc/modules | List of modules |
/proc/kallsyms | List of Symbols |

Vaibhav.