This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Scripting 101

Chat log Class 10

hemanth hm's picture
Sun, 2010-10-17 18:37

<hemanth> SourRust, hey :)
<SourRust> hey
<hemanth> Hows it going?
* datruth29 (4575bfc4@gateway/web/freenode/ip.69.117.191.196) has joined #p2pu-webcraft/scripting-101
<datruth29> hey everybody
<hemanth> hey datruth29
<datruth29> did we start yet?
<hemanth> in 2 more mins
<hemanth> just waiting for few mins
* Lakshmi (51628fe1@gateway/web/freenode/ip.81.98.143.225) has joined #p2pu-webcraft/scripting-101
<datruth29> okay
* hemanth has changed the topic to: Traps in BASH
<hemanth> Today also we are doing a specific unit called traps in bash
* SourRust has quit (Ping timeout: 265 seconds)
<hemanth> after this there begins the real fun of functions
<Lakshmi> Ok
<hemanth> i want to take functions in detail from next week, so will do only traps today
<hemanth> # Functions.
<hemanth> # Interactive Scripts.
<hemanth> would be the end of basic scripting
<hemanth> later we can take about OOP
<datruth29> sounds good
<hemanth> OOP => "A programming technique that enables you to work with self-contained collections of data structures and routines that can interact with other objects."
<hemanth> In simple lines it would be Object Oriented Programming
<hemanth> So dealing with traps, we need to understand few basic concepts of a process
<hemanth> ps
<hemanth> say ps in your terminals
<hemanth> ps - report a snapshot of the current processes.
<Lakshmi> Yes
<hemanth> ps aux | less
<hemanth> a -> Lift the BSD-style "only yourself" restriction
<datruth29> that displays all processes, correct?
<hemanth> u->Select by effective user ID (EUID)
<hemanth> datruth29, yes processes of that user
<hemanth> to see it more better
<hemanth> say 'pstree'
<hemanth> in your terminals
<hemanth> pstree - display a tree of processes
<hemanth>  init-+-getty
<hemanth>                 |-getty
<hemanth>                 |-getty
<hemanth>                 `-getty
<hemanth> init is the first process that spawns
<datruth29> this is so much easier to read
<hemanth> one can also use top and htop
<hemanth> The  top  program  provides  a dynamic real-time view of a running system
<hemanth> To get the pid of the process
<hemanth> on can use pidof <process_name> or pgrep <process_name>
<hemanth> say like pidof firefox or pgrep firefox
<hemanth> So after seeing all these, can anyone define me what a process is?
<datruth29> it's basically a running program?
<hemanth> yes :)
<hemanth> For the sake of textbook definition "A process is an instance of a computer program that is being executed. It contains the program code and its current activity. Depending on the operating system (OS), a process may be made up of multiple threads of execution that execute instructions concurrently."
<datruth29> ahhh
<hemanth> :)
<hemanth> So each process can receive signals, based on which they react
<hemanth> We can send these signals to the process
<hemanth> So we have set of utilities like kill,pkill,killall
<hemanth> to send signals to these process
<hemanth> there are about 68 signals
<hemanth> but not all code can recognize all signals
<hemanth> the most useful signals are
* vibha5bhat (75c0c0f4@gateway/web/freenode/ip.117.192.192.244) has joined #p2pu-webcraft/scripting-101
<hemanth> SIGHUP (1) - Hangup detected on controlling terminal or death of controlling process.
<hemanth> SIGINT (2) - Interrupt from keyboard.
<hemanth> SIGKILL (9) - Kill signal i.e. kill running process.
<hemanth> SIGSTOP (19) - Stop process.
<hemanth> SIGCONT (18) - Continue process if stopped.
<hemanth> Example: kill -SIGKILL pidof firefox
* hemanth sends logs so for to vibha5bhat
* mvoltz (601a1a4f@gateway/web/freenode/ip.96.26.26.79) has joined #p2pu-webcraft/scripting-101
<hemanth> We can also do kill -l <signal_number>
<hemanth>  1) SIGHUP  2) SIGINT  3) SIGQUIT  4) SIGILL
<hemanth>  5) SIGTRAP  6) SIGABRT  7) SIGBUS  8) SIGFPE
<hemanth>  9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2
<hemanth> 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT
<hemanth> 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
<hemanth> 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU
<hemanth> 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH
<hemanth> 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN
<hemanth> 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4
<hemanth> 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
<hemanth> 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12
<hemanth> 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14
<hemanth> 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10
<hemanth> 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6
<hemanth> 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
<hemanth> 63) SIGRTMAX-1 64) SIGRTMAX
<hemanth> are the list of signals
* hemanth sends logs to mvoltz
<hemanth> killall firefox-bin
* SourRust (473b12d5@gateway/web/freenode/ip.71.59.18.213) has joined #p2pu-webcraft/scripting-101
<hemanth> would kill all the instances of firefox program
* hemanth sends logs so far to SourRust
<hemanth> pkill command on the other hand is simple
<hemanth> pkill sends SIGTERM to the process
<hemanth> queries ?
* hemanth waits for few many reading the logs :)
<hemanth> Do send me a SIGCONT once done :)
<datruth29> lol
<hemanth> :)
<Lakshmi> :)
<hemanth> The parent process might die and its child will be orphans...so sad..
<hemanth> there are some process which will sleep and wakeup, yes they are daemons
<hemanth> there are even zombies :D
<hemanth> yes its all so much easy and fun
* hemanth still waits for SIGCONT!
<hemanth> will i become a demon ?
<hemanth> Lakshmi, vibha5bhat,mvoltz, SourRust, shall i move on?
<SourRust> yup all caught up
<vibha5bhat> yes
<Lakshmi> Yeah
<hemanth> signal and their values must be know to trap them, some signals can never be caught, the signals SIGKILL (9) and SIGSTOP (19) cannot be caught, blocked, or ignored.
<hemanth> Interrupt from keyboard (Ctrl^C)  is  SIGINT
<hemanth> SIGSTP  Stop typed at tty (CTRL^z)
<hemanth> So, will running a script a user might hit
<hemanth> CTRL^C or ^Z
<hemanth> arithmetic overflows might happen due to bugs in the script
* Lakshmi_ (51628fe1@gateway/web/freenode/ip.81.98.143.225) has joined #p2pu-webcraft/scripting-101
<hemanth> So what can we do to trap such signal?
<hemanth> Simple, we user the trap command
* Lakshmi has quit (Quit: Page closed)
<hemanth> Trap ->Trap signals and other events.
<hemanth> It defines and activates handlers to be run when the shell receives signals
<hemanth>     or other conditions.
<hemanth> trap: trap [-lp] [[arg] signal_spec ...]
<hemanth>  -l print a list of signal names and their corresponding numbers
<hemanth>  -p display the trap commands associated with each SIGNAL_SPEC
<hemanth> Different variants of using trap
<hemanth> trap arg signal
<hemanth> trap cmd signal
<hemanth> trap 'action' SIGTERM SIGINT SIGFPE SIGSTP
<hemanth> trap 'action' 15 2 8 20
<hemanth> So on...
<hemanth> lets see how to apply them
<hemanth> trap 'echo "Exit 0 signal"' 0
<hemanth> that captures an interrupt #0
<hemanth> cat > trap.bash
<hemanth> #!/bin/bash
<hemanth> trap 'echo "Exit 0 signal"' 0
<hemanth> echo "Testing trap..."
<hemanth> exit 0
<hemanth> Ctrl^D
<hemanth> chmod a+x trap.bash
<hemanth> ./trap.bash
<hemanth> What do you notice?
<datruth29> exit 0 seems to call the echo statement you placed in the script
<Lakshmi_> yes
<hemanth> yes, first it echos Testing trap...
<hemanth> then when it executes exit 0
<hemanth> that is been trapped, by trap and hence echo's Exit 0 signal
<hemanth> fine?
<datruth29> yes
<hemanth> we can trap commands like
<hemanth> trap "rm ${file}; exit" 0 1 2 3 15
<hemanth> No such file or directory from rm could be avoided
<hemanth> trap ' ' 2
<hemanth> can be done to capture ctrl^c
<hemanth> clear?
<hemanth> o/
<hemanth> ?
<SourRust> do traps stop the default action like exit 0 example
<mvoltz> what is a typical action to take once something is successfully trapped?
<hemanth> SourRust, you meant you want to trap 0?
<SourRust> like in the exit 0 example, did it stop the exit 0
<hemanth> yes, it trapped
<SourRust> kk
<hemanth> mvoltz, it depends on what you want to do
<hemanth> something an error is displayed
<hemanth> somethings another function is called
<hemanth> somethings process cleanup is done
<mvoltz> gotcha
<hemanth> SourRust, but the code flow does not continue, its just a trap there
<hemanth> to test, you can say echo "After exit 0"
<hemanth> got it?
<SourRust> ah, ok
<SourRust> yp
<SourRust> yup
<hemanth> So, now one must get the doubt, once i have set the trap, how do i clear them
<hemanth> to clear them we use trap - signal
<hemanth> trap - 1 2 3 15
<hemanth> would clear 1,12,3 and 15
<hemanth> that was set b4
<hemanth>  /was/were
<hemanth> trap 'echo " disabled"' SIGINT SIGQUIT SIGTSTP
<hemanth> would trap  capture CTRL+C, CTRL+Z and quit
<hemanth> So in the flow if the user hits CTRL+C it says 'disabled'
<hemanth> So to exit such a code
<hemanth> one must trap - SIGINT SIGQUIT SIGTSTP (clear all)
<hemanth> and then say exit 0
<hemanth> so those traps work for that script
<hemanth> and wont affect the shell
<hemanth> or can use a () subshell
<hemanth> clear?
<Lakshmi_> yes
<mvoltz> yes
<SourRust> yup
<hemanth> nice, with that trapping signals comes to and end, you guys try making a switch case to catch them by name/number and push them to the repo :)
<hemanth> next class, is all fun with functions :)
<hemanth> till then happy hacking :)
<SourRust> cya
<Lakshmi_> ok
<datruth29> later!
* datruth29 has quit (Quit: Page closed)
<hemanth> byee