Sponsored Content
Top Forums UNIX for Dummies Questions & Answers help with understanding script Post 302474219 by iluvsushi on Tuesday 23rd of November 2010 03:48:51 PM
Old 11-23-2010
help with understanding script

i am trying to understand this script example. the text does not explain it. can someone tell me briefly what each of the functions do. any help will be appreciated.
Code:
#!/bin/bash

killtree() {
    local _pid=$1
    local _sig=${2-TERM}
    for _child in $(ps -o pid --no-headers --ppid ${_pid}); do
        killtree ${_child} ${_sig}
    done
    kill -${_sig} ${_pid}
}

if [ $# -eq 0 -o $# -gt 2 ]; then
    echo "Usage: $(basename $0) <pid> [signal]"
    exit 1
fi

killtree $@
#
#       ......
#
while [ $(ps -p $! -o etime= | tr -d ": ") -lt 10000 ]
do
  sleep 60
done 2> /dev/null

kill -s SIGHUP $!

function my_ps()
function killps()   # kill by process name
{
   local pid pname sig="-TERM"   # default signal
   if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
       echo "Usage: killps [-SIGNAL] pattern"
       return;
   fi
   if [ $# = 2 ]; then sig=$1 ; fi
   for pid in $(.............) ; do
       pname=$(................... )
       if ask "Kill process $pid <$pname> with signal $sig?"
           then kill $sig $pid
       fi
   done
}
_killall ()
{
    COMPREPLY=( $( /bin/ps -u $USER -o comm  | \
        sed -e '1,1d' -e 's#[]\[]##g' -e 's#^.*/##'| \
        awk '{if ($0 ~ /^$USER/) print $0}' ))

    return 0
}
complete -F _killall killall killps


Last edited by Scott; 11-23-2010 at 04:54 PM.. Reason: Code tags, please...
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need help understanding script command

We use a UNIX-based system (Lawson) at work and I was given this command to request a data extract from the db admin. The only thing I really understand is the last line as it appears to be joining the files created from the first three lines into one. Is there anyone who can help me breakdown the... (4 Replies)
Discussion started by: KGee
4 Replies

2. Shell Programming and Scripting

Help understanding a script

Hello everybody, Can anybody tell me of what "~" refers to in the below code snippet. lsvg $vgNAME | awk 'BEGIN {freeDISK=1} {if (($4 ~ /PP/ && $5 ~ /SIZE/) || ($4 ~ /FREE/ && $5 ~ /PPs/)) {freeDISK *= $6 }} END {print freeDISK*1024 }' Thanks in advance, (6 Replies)
Discussion started by: tenderfoot
6 Replies

3. Shell Programming and Scripting

Need help for understanding of script

# sub: find block (in cols), return line-numbers (begin-end) or 0 if notfound sub findb{ my ($exp1,$col1,$exp2,$col2)= @_; # $exp = expression to find, $col - column to search in my $cnt=0; my ($val1,$val2); my ($beg,$end); for($cnt=1;$cnt<=65536;$cnt++){ $val1 =... (3 Replies)
Discussion started by: suvenduperl
3 Replies

4. UNIX for Advanced & Expert Users

understanding awk in this script

i am analyzing a query written by another developer ,need to understand part of script am looking at a code ..and it converts comma files to pipe delimited and also takes away quotes from any columns, source field format: 2510,"Debbie",NewYork changes to target: 2510|Debbie|NewYork ... (1 Reply)
Discussion started by: coolrock
1 Replies

5. Shell Programming and Scripting

Need help understanding this script.

Can someone explain what is happening line by line in this script, particularly after the do statement. The script works, it renames all the files in my directory that has a date in the file name. But I would like to know more about it. #!/bin/bash newdate=12-10-1995 for file in *--* do ... (6 Replies)
Discussion started by: Harleyrci
6 Replies

6. Shell Programming and Scripting

Help understanding the script

Hi Guys, I am new to scripting , I am trying to rebuild a script based on an old script. Can someone help me figure out what the script is doing? This is only a part of the script. I am looking to interpret these two points in the scripts:- 1) test=`echo $?` while I do not... (3 Replies)
Discussion started by: rajsan
3 Replies

7. Shell Programming and Scripting

Help--understanding the script

Hi, New to this forum, as well as to unix scripting..need help to understand below script ... sendNotice_sp() { ATTACH=${LNXLOG:-} if ; then if ; then mail -s "$ERR_MSG" $ERR_EMAIL_TO < $ATTACH fi else mail -s "$ERR_MSG" $ERR_EMAIL_TO < /dev/null fi } afaik this is sm kind of... (6 Replies)
Discussion started by: gnnsprapa
6 Replies

8. Shell Programming and Scripting

Help understanding the script

Could someone please help me in understanding the code below: #!/usr/bin/ksh Month=`date|cut -c5-7` Day=`date|cut -c9-10` Year=`date|cut -c27-28` Rom2Jul() { case $Month in Feb) Day=$(( $Day+31 ));; Mar) Day=$((... (27 Replies)
Discussion started by: hasn318
27 Replies

9. UNIX for Beginners Questions & Answers

Shall script use and understanding

Below script is called as Reducer, I am not sure how it work, can some expert explain me what this script does as i am a beginner. inputfile: hi hi how are are you code: #!/bin/bash lastkey=""; -- what does this mean, because i saw in debug mode it is taking value as hi count=0;... (13 Replies)
Discussion started by: mirwasim
13 Replies

10. UNIX for Beginners Questions & Answers

Understanding a script for sum

Hello, How come the following script adds each numeric value to a total sum ? x=$1 func() { for i in $1 $2 $3; do let x= $x+$i done } func "8 8 8" 9 9 echo $x A.How the program sums the string "8 8 8" if it`s only the first field value ($1)? B.If we define x to be $1... (3 Replies)
Discussion started by: uniran
3 Replies
KILL(2) 						      BSD System Calls Manual							   KILL(2)

NAME
kill -- send signal to a process LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <sys/types.h> #include <signal.h> int kill(pid_t pid, int sig); DESCRIPTION
The kill() system call sends the signal given by sig to pid, a process or a group of processes. The sig argument may be one of the signals specified in sigaction(2) or it may be 0, in which case error checking is performed but no signal is actually sent. This can be used to check the validity of pid. For a process to have permission to send a signal to a process designated by pid, the user must be the super-user, or the real or saved user ID of the receiving process must match the real or effective user ID of the sending process. A single exception is the signal SIGCONT, which may always be sent to any process with the same session ID as the sender. In addition, if the security.bsd.conservative_signals sysctl is set to 1, the user is not a super-user, and the receiver is set-uid, then only job control and terminal control signals may be sent (in par- ticular, only SIGKILL, SIGINT, SIGTERM, SIGALRM, SIGSTOP, SIGTTIN, SIGTTOU, SIGTSTP, SIGHUP, SIGUSR1, SIGUSR2). If pid is greater than zero: The sig signal is sent to the process whose ID is equal to pid. If pid is zero: The sig signal is sent to all processes whose group ID is equal to the process group ID of the sender, and for which the process has permission; this is a variant of killpg(2). If pid is -1: If the user has super-user privileges, the signal is sent to all processes excluding system processes (with P_SYSTEM flag set), process with ID 1 (usually init(8)), and the process sending the signal. If the user is not the super user, the signal is sent to all processes with the same uid as the user excluding the process sending the signal. No error is returned if any process could be signaled. For compatibility with System V, if the process number is negative but not -1, the signal is sent to all processes whose process group ID is equal to the absolute value of the process number. This is a variant of killpg(2). RETURN VALUES
The kill() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error. ERRORS
The kill() system call will fail and no signal will be sent if: [EINVAL] The sig argument is not a valid signal number. [ESRCH] No process or process group can be found corresponding to that specified by pid. [EPERM] The sending process does not have permission to send sig to the receiving process. SEE ALSO
getpgrp(2), getpid(2), killpg(2), sigaction(2), sigqueue(2), raise(3), init(8) STANDARDS
The kill() system call is expected to conform to ISO/IEC 9945-1:1990 (``POSIX.1''). HISTORY
The kill() function appeared in Version 7 AT&T UNIX. BSD
March 15, 2012 BSD
All times are GMT -4. The time now is 02:48 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy