Funny : why can't I kill my parent terminal in a script ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Funny : why can't I kill my parent terminal in a script ?
# 1  
Old 11-17-2010
[SOLVED] Funny : why can't I kill my parent terminal in a script ?

Hellooooooo Smilie

Today I've created a script that does some things (not important).
And for fun I want that it kills the terminal from which I launch it. (too lazy to press Alt-F4 lol)
So I write
Code:
#!/bin/bash
kill $PPID

but it doesn't work ... while if in my xterm I launch another xterm and in this second xterm I write
Code:
kill $PPID

both xterm windows are closing.

I could manage to make my script work with the "-9" option ... but I don't like to use this way Smilie

I really would like to understand that mystery ... if someone may help me please ^_^

Last edited by xib.be; 11-26-2010 at 02:06 PM..
# 2  
Old 11-17-2010
Wanna suicide ?
remove the #!/bin/bash line

you can give a try with and without this line exec bash
and also add the line
Code:
kill -9 $$

Wanna kill your parents ? Maybe i am wrong but i think it is forbidden by design (security reason, generation of zombie process, ressource waste...)

(This is maybe possible with some tools developped in C ?)
I am sure some C guys can tell you more

for a definitive suicide (don't do it if you don't understand what it does or if you don't have another user that can alter the user you modify) :
Code:
echo "exit" >>.profile
. ./.profile


Last edited by ctsgnb; 11-17-2010 at 03:06 PM..
This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 11-17-2010
try this Smilie
Code:
## justdoit ##
#!/bin/bash
pid=$(ps -ef | grep "$PPID" | awk  '{if (NR==1) print $3} ')
if [ $(pstree -p $pid | grep -o bash | wc -l) -gt 1 ] ; then
 echo -e "You are maybe in subshells..\nDetails..\n|||||||The process view|||||||"
 pstree -hp $pid
 exit 1
else
  x=$(pstree $pid|sed -n '1!p'|grep -Ev "sed|pstree")
  if [ $(echo "$x"|wc -l) -gt 0 ] && [ ! "$x" == "" ] ; then
   echo -e "Some Jobs Running..\n $x"
  else
   echo "Session is terminating"
   kill $pid
  fi
fi

This User Gave Thanks to ygemici For This Post:
# 4  
Old 11-21-2010
I've also read the following caveat referenced in the Oreilly Power Tool book purchase it and read the section "Killing All Your Processes " (chapter 38.12)
Code:
kill -TERM -1


also
Code:
man 2 kill


# 5  
Old 11-26-2010
Hi thank you both of you for your advices Smilie

Finally, I found my "error" or maybe I should say another way. It wasn't the PPID I had to kill ... but the PPPID x) and I can kill it without any trouble contrary to PPID.
Here is my script : "launch-and-kill-terminal-window"
Code:
#!/bin/bash
($@&)
#kill  $PPID #--> KO cause this is the bash pid and not the "xterm" pid
pppid=`ps -p $PPID -o ppid=`
kill $pppid

then you can launch any program from your terminal wihtout having to close your terminal window manually Smilie
for example:
Code:
$ ./launch-and-kill-terminal-window xclock

et voilą Smilie
just my xclock window... no more xterm window

NB this works well in xterm ... in a gnome-terminal, it closes ALL your gnome-terminal windows, cause gnome-terminal works differently from xterm (again an example why you should use standards :P)



@ctsgnb
I think the line
Code:
#!/bin/bash

is important in a script as not evrybody use bash by default. And depending on the interpreter some things change. for example if I remeber tcsh uses variables differently from bash

I've tried with exec but I didn't manage to make it work as I wanted

@ygemici thank you very much for your script. very nice tricks I'll add to my script

Last edited by xib.be; 11-26-2010 at 02:14 PM..
# 6  
Old 11-26-2010
I thought it bizzare you had to use PPID and not the terminal's own ID, but you're right: An interactive shell just swallows SIGINT, SIGTERM, SIGQUIT, and sometimes even SIGHUP! There doesn't seem to be any graceful way to kill these things, which is weird.
# 7  
Old 11-28-2010
MySQL

I modified my script
you can try this Smilie

Code:
 
## exshell ##
 
#!/bin/bash
 
clear
shells="bash|ksh|csh|tcsh|pdksh|wksh|zsh"

find_stat () {
sshpid=$(ps -ef | grep $PPID|sed -n '1p'|awk '{print $3}' )
while [ $(ps -p $sshpid -o comm=) != "sshd" ]
 do
  sshpid=$(ps -ef | grep $sshpid|grep -v grep|awk '{print $3}'|sort -n|sed -n '1p')
 done
curview=$(pstree -p $sshpid)
myparent=$(echo "$curview"|grep -Eow "$shells"|sed -n '1p' )
jstatus=$(pstree -p $PPID) ; jstat=$(echo "$jstatus"|wc -l)
}

find_details () {
echo "-->Shells view<--">tmpsedX
for shell in $(echo "$shells"|sed 's/|/ /g')
 do
  echo "$curview"|grep -Eow "$shells"|sed -n "/$shell/p;" |sed -ne '{1p;$=;};'|sed 'N;s/\(.*\)\n\(.*\)/\2 \1 shell(s) were opened./'>>tmpsed
X
 done
incrparent=$(echo `expr $(sed -n "/$myparent/s/^\([0-9][0-9]*\).*/\1/p" tmpsedX) - 1`)
sed "2s/^[0-9][0-9]* $myparent/1 $myparent [parent] + $incrparent $myparent [subshell(s)]/" tmpsedX
}

get_view () {
echo -e "\n-->Process view<--\n$(echo "$curview"|sed 's/---pstree(.*)//')\nYou are in now \"`ps -p $PPID -o comm=`\" shell with pid $PPID number\n"
}

exit_shell () {
echo "Shell session is terminating.."
kill -1 $PPID ;sleep 1 ; kill -1 $PPID 2>/dev/null ; exit 1 ;
}

get_status ()
{
find_stat ;
parstat=$(echo "$curview"|grep sshd|grep -Eow "$shells"|wc -l)
substat=$(echo "$curview"|grep -Eow "$shells"|wc -l)
}

get_status ;

if [ $jstat -gt 1 ] ; then
 echo -e "Some jobs are open and running..\nPlease check your (background) jobs\n"
 get_view ;
 echo -e "\n*** Warning ***\nThere are some open process(jobs)!!
if you want to close forcely ssh session continue then open process will not be saved!!
anyway continue (y/n)?"
  read chc
   if [ "$chc" == "y" ] ; then
      exit_shell ;
   else
      exit 1 ;
   fi
fi


if [ $parstat -eq 1 ] ; then
     get_view ;
     echo -e "You are in parent shell(\"`ps -p $PPID -o comm=`\")\nClose Session(y/n)?"
     read pchc
     if [ "$pchc" == "y" ] ; then
        exit_shell ;
     fi
fi

if [ $substat -gt 1 ] ; then
     echo -e "You have some [shell script or child process(shell)] subshell(s)!!\n"
     find_details ; get_view ; find_stat ;
     echo -e "Do you want to close this session..\n(y/n)?"
     read nchc
     if [ "$nchc" == "y" ] ; then
        exit_shell ;
     fi
fi

regards
ygemici

Last edited by ygemici; 12-29-2010 at 03:59 AM..
This User Gave Thanks to ygemici For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

System should not kill the child process when parent id is 1

HI i would like to know how i can simulate a shell scripts for my requirement. example Server name child Process id Parent Process id Vpesh 16013 15637 Server name child Process id Parent Process id Vpesh 16014 15637 Server name child... (1 Reply)
Discussion started by: vpesh
1 Replies

2. Shell Programming and Scripting

Kill specific terminal using shell/bash

Hello elite shell/bash specialists, I have done plenty of STFW and some RTFM, but I cannot find a clear solution to my challenge Goal: My goal is to have a script(of any language, preferably shell/bash/anything that can run things on unix), which will kill specific unix terminal windows for... (0 Replies)
Discussion started by: kamil-mech
0 Replies

3. Shell Programming and Scripting

Kill Parent/ Child processes

I am trying to kill PIDs that are tied to a KSH "load_sqlplus" and I am using the below code LIST_PID=`ps -ef | grep -i "load_sqlplus" | grep -v grep | awk '{print $2}'` if ; then echo "Processes killed" "PID : " $LIST_PID kill -9 $LIST_PID else echo "Nothing to Kill" fi... (4 Replies)
Discussion started by: venky338
4 Replies

4. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

5. Shell Programming and Scripting

shell script to kill process with respect to terminal

Hi, I've a script which kills all process, but i need a script shell script(sh), where it'll kill process on that particular terminal. below is example TY=`tty` for P in $TY do `kill -9 $P 2>/dev/null`; done echo "test process killed" break ... (3 Replies)
Discussion started by: asak
3 Replies

6. UNIX for Dummies Questions & Answers

Kill child processes, when parent is "bash"

Consider this simple command line bash -c 'echo $$ ; sleep 10000'This will print the newly created bash PID and sleep for a long time. If I go to another terminal and do something like ps -flax | grep leepI'll see something like 501 92418 91910 0 0:00.00 ttys000 0:00.00 bash -c echo $$... (5 Replies)
Discussion started by: teras
5 Replies

7. Shell Programming and Scripting

Kill a process from parent shell within a shell script

Hi, I am looking for a solution for the following problem: Im Using tcpdump within a shellskript started in a subshell by using brackets: ( /usr/sbin/tcpdump -i ... -c 1 ) - I want the outout of tcpdump saved in a variable - Than tcpdump-Process in the Subshell should be killed - and I... (6 Replies)
Discussion started by: 2retti
6 Replies

8. UNIX for Dummies Questions & Answers

Need help to kill parent and all of its sub processes

Hi, I am writing korn shell script. My requirement is, i have to kill the parent process and all of its child processes. Can some one please help me on this? Thanks in advance for your help.. (1 Reply)
Discussion started by: Sheethal
1 Replies

9. Shell Programming and Scripting

How to kill history from terminal to others using same id

I have a application ID and many users in the team are using this id. I dont want the people to check whati am running with the id from my terminal. is there a way to kill history get back from my console to everybody so thatwhat ever i type in my console cant be seen from other users who are... (5 Replies)
Discussion started by: dsravan
5 Replies

10. UNIX for Dummies Questions & Answers

kill parent and child

Hello all, I have gone through the search and looked at posting about idle users and killing processes. Here is my question I would like to kill an idle user ( which I can do) but how can I asure that all of his process is also killed whit out tracing his inital start PID. I have tried this on a... (4 Replies)
Discussion started by: larry
4 Replies
Login or Register to Ask a Question