sending crtl-c to a pid

Posted on 2012-09-06
Last Modified: 2012-09-08
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 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.
Question by:Dooglave
    LVL 6

    Author Comment

    I think the debug script is looking for ctrl-c with this line:
     trap 'break' 2 # allow break by Ctrl-C
    LVL 76

    Accepted Solution

    kill -INT <pid>

    should be correct.
    will check and update
    LVL 76

    Expert Comment

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

    Author Comment

    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 :(

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now