Trap killing PID


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trap killing PID
# 1  
Old 09-03-2016
Linux Trap killing PID

Hi,

Just wonder if there is a way to identify the PID of the killing process using trap.

Please let me know possible solution.

Code:
#!/bin/ksh

hello () {
print "in hello";
print "PID of process issued SIGNAL"; --> this is what i'm looking for. 
}

trap hello SIGKILL SIGTERM

while true
do
  print "in loop";
  sleep 1;
done


Last edited by Scrutinizer; 09-03-2016 at 02:39 AM.. Reason: code tags
# 2  
Old 09-03-2016
First note that you cannot trap a SIGKILL signal. If your process receives a SIGKILL, it will be terminated without a chance to execute any more instructions. (This is the case for any process; not just shell scripts.)

If you are writing a C program, and you use a signal catching function that gives you a siginfo_t structure when a signal is received and the signal received is of a few certain types and was sent under certain conditions, that C program can determine the process ID and user ID of the process that sent the signal. I don't know of any shell that catches this information and makes it available to code running as an action in a trap command when a shell script receives a signal.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 09-03-2016
I don't think you can do that easily in a shell script.

But if you control the killing process you can have it transfer its PID to the process that is to be killed, through a named pipe in /tmp for example:

Code:
#!/bin/ksh

hello () {
print "in hello";
read killproc < "$pipe"
print "PID of process that issued SIGNAL: $killproc"
exit
}

pipe=/tmp/killerproc.$$
mkfifo $pipe
trap "rm -f $pipe" EXIT
trap hello SIGKILL SIGTERM


while true
do
  print "in loop of process $$";
  sleep 1;
done

And then kill the process with something like this:

Code:
# killpid=<pid of process to be killed>
pipe=/tmp/killerproc.$killpid
kill "$killpid" ; echo $$ > $pipe

This would need to be refined, but you get the idea.

Last edited by Scrutinizer; 09-03-2016 at 05:21 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 09-05-2016
Thank You Don Cragun and Scrutinizer for your useful responses. I will try in one of this approach.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

--killing backround Procs spawned from the parent script with Ctrl+C trap

Hello: Am trying to understand why the method #2 works but method #1 does not. For both methods, sending CTRL+C should kill both the Parent script & all of the spanwd background procs. Method #1: ========================== #!/bin/sh ctrl_c() { echo "** Trapped CTRL-C" ... (3 Replies)
Discussion started by: gilgamesh
3 Replies

2. UNIX for Dummies Questions & Answers

Need script for killing Pid in Linux

need script(shell or python) for killing pid in linux (2 Replies)
Discussion started by: roshan9995
2 Replies

3. Homework & Coursework Questions

VM trap may work differently than a pure install trap.

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: That is the last reply I received from my instructor, and I'm looking for some alternatives. When using... (2 Replies)
Discussion started by: newuser45
2 Replies

4. Shell Programming and Scripting

Finding the age of a unix process, killing old processes, killing zombie processes

I had issues with processes locking up. This script checks for processes and kills them if they are older than a certain time. Its uses some functions you'll need to define or remove, like slog() which I use for logging, and is_running() which checks if this script is already running so you can... (0 Replies)
Discussion started by: sukerman
0 Replies

5. Solaris

Finding PID of Killing process

Say I have 2 processes(perl scripts on Solaris machine) A and B. the process A kill the process B. While in the process B how do I print the PID of the process that Killed it(process A) before dieing. My process A looks like open(STATS, "ps -ef|"); while ($inputLine = <STATS>) { if... (7 Replies)
Discussion started by: enigma_007
7 Replies

6. Shell Programming and Scripting

Cntl+z Trap is not detecting ??? Help required to add a trap detection ???

Hi folks, I have tried to add some trap detection in the below script....this script is used to monitor database activities...in a rather awkward way :rolleyes:.... The idea behind adding trap is that....this script creates lots of temporary files in the running folder to store the count... (1 Reply)
Discussion started by: frozensmilz
1 Replies

7. UNIX for Dummies Questions & Answers

Need to get pid of a process and have to store the pid in a variable

Hi, I need to get the pid of a process and have to store the pid in a variable and i want to use this value(pid) of the variable for some process. Please can anyone tell me how to get the pid of a process and store it in a variable. please help me on this. Thanks in advance, Amudha (7 Replies)
Discussion started by: samudha
7 Replies

8. UNIX for Advanced & Expert Users

killing a process pid

What option is used with kill to cause the server to reread its config file. (16 Replies)
Discussion started by: jo calamine
16 Replies

9. Shell Programming and Scripting

killing PID's of ptree

gurus, normally to stop a process ,i need to kill all its child & then parent process. i do it manually as follows bash-2.03$ ps -ef | grep bpm|grep -v grep tibadmin 21882 21875 0 May 27 ? 0:00 /bin/sh ./bpmse_20.sh -Xms512m -Xmx512m /tibco/UpdateCustomer/dat/UpdateCustome ... (0 Replies)
Discussion started by: abhijeetkul
0 Replies

10. UNIX for Dummies Questions & Answers

Session PID & socket connection pid

1. If I use an software application(which connects to the database in the server) in my local pc, how many PID should be registered? Would there be PID for the session and another PID for socket connection? 2. I noticed (through netstat) that when I logged in using the my software application,... (1 Reply)
Discussion started by: pcx26
1 Replies
Login or Register to Ask a Question