• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 596
  • Last Modified:

sending crtl-c to a pid

I have a script I didn't write that when you issue ctrl-c it traps and then runs some other clean up code.

If I run the scritp ./debug & (at the command line)
then issue: kill -2 PID
it kills the script the same way it does by hitting crtl-c

But if I execute the script from another script and run: kill -2 PID, it doesn't work.

I tried -INT -SIGINT, SIGUSR1 and I can kill the pid but the script doesn't do it's cleanup which creates a tgz of all the output files.

So how can I kill this script gracefully and make it think it received a regular old ctrl-c or kill -2 PID?

This is what the test.sh script looks like right now:
#! /bin/bash
./debug &
echo $PID " is the pid you are looking for"
sleep 45
kill -2 $PID

remember kill -2 works fine from the command line only if I execute the .debug script from the command line. But it breaks when I launch it from another bash script.
  • 2
  • 2
1 Solution
DooglaveAuthor Commented:
I think the debug script is looking for ctrl-c with this line:
 trap 'break' 2 # allow break by Ctrl-C
kill -INT <pid>

should be correct.
will check and update
It should work.
It all depends on how you have
trap "action" 2
is the cleanup process within the trap or does it call a defined "function" to perform the cleanup?
break exits out of a loop. not sure what cleanup you are looking it to do.
you can use trap to trap any/all other signals and perform the tasks according to the signal received.
DooglaveAuthor Commented:
Your answer is correct. I didn't write the other script and I didn't want to modify it since it's not mine. But I had to :(
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now