Sponsored Content
Top Forums Shell Programming and Scripting Funny : why can't I kill my parent terminal in a script ? Post 302475391 by ygemici on Sunday 28th of November 2010 01:53:38 PM
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:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 06:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy