[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


sending crtl-c to a pid

Posted on 2012-09-06
Medium Priority
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 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.
Question by:Dooglave
  • 2
  • 2

Author Comment

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

Accepted Solution

arnold earned 2000 total points
ID: 38375281
kill -INT <pid>

should be correct.
will check and update
LVL 81

Expert Comment

ID: 38377150
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.

Author Comment

ID: 38379902
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
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.
Suggested Courses
Course of the Month18 days, 16 hours left to enroll

834 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