[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Killing a bash script does nto kill program executed within

Posted on 2004-09-21
17
Medium Priority
?
822 Views
Last Modified: 2010-04-21

I have a bash script like the following:
-------------------------------
#! /bin/bash

./serverprogram

exit 1
------------------------------

When I run "kill -SIGKILL" on the bash script it kills the script but not the server program executed within.  The server program keeps running.

Is there a way to kill  the server program when the script is killed without modifying the server program?
0
Comment
Question by:cameron_schuler
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 3
  • +3
17 Comments
 
LVL 40

Expert Comment

by:jlevie
ID: 12114687
Given that script, killing "serverprogram" will allow the script to exit. And you should be able to accomplish that with 'killall serverprogram'. See 'man killall' for details.
0
 

Author Comment

by:cameron_schuler
ID: 12115418
True, but I need the "serverprogram" to die when I kill the bash script.  Is that possible?  I can't kill the "serverprogram" directly, it has to be the script.  I could explain the reason behind this but I don't think it's necessary in answering the question.
0
 
LVL 23

Assisted Solution

by:Mysidia
Mysidia earned 50 total points
ID: 12119081
I believe the answer is no.

Without modifying the server program, you can't detect that the script is no longer running.

The SIGKILL signal won't be inherited by the serverprogram because that is not how signals work.
A user process can't trap SIGKILL or modify its behavior

you can trap the _overridable_ signals like SIGTERM and pass them on, but that doesn't work too
well:

#!/bin/bash
trap "kill -TERM %1"  "SIGTERM"
./serverprogram

the problem is that since serverprogram runs in the foreground, the shell won't react to
the trapped signal until the subprocess "serverprogram" exits.
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 100 total points
ID: 12120295
I agree with Mysidia. We have had a rather long discussion in this regard which elaborates on alternatives and the reasons why this is not possible. You can find it at the link below.
http://www.experts-exchange.com/Programming/Programming_Platforms/Linux_Programming/Q_20824350.html
0
 
LVL 2

Expert Comment

by:sneeuw_chan
ID: 12121362
If you put a dash before the process ID in the 'kill' command, it will kill all the children of that process ID as well.  But that's probably not what you want.
But if the script is as you described above, why not just 'exec' the serverprogram ?
0
 

Author Comment

by:cameron_schuler
ID: 12123478
It's probably best that this thread dies as it looks like their is no solution to the problem.
0
 
LVL 2

Expert Comment

by:sneeuw_chan
ID: 12123623
As the original question is phrased, I would say that using 'exec' to run the server would be a solution.
Is the script more complicated than that, so that that is not a solution ?
What is the actual script you're running ?
0
 

Author Comment

by:cameron_schuler
ID: 12128479
What do you mean by using "exec" to run the server?

Yes, the script is a bit more complicated then just that one line.  Some logging and other minor things.
0
 
LVL 2

Accepted Solution

by:
sneeuw_chan earned 100 total points
ID: 12128555
Well, if the last thing the script does is to run the server program, then you can do something like:

  exec ./serverprogram

Which makes the shell process be replaced by the server process.

But that's probably not useful; I assume that you're doing stuff after the server process has finished (like cleaning up logfiles and stuff).
0
 

Author Comment

by:cameron_schuler
ID: 12129520
Interesting... I'll give it a try tomorrow.
0
 
LVL 23

Expert Comment

by:Mysidia
ID: 12130110
Exec'ing the server program is just about equivalent to running it in the background

./servername &
exit 1

Or automatically killing the shell script as soon as the server starts

in either case, the result is that the server program is still running,
no shell or shell script is running.

That might be useful, but not if the shell script needs to stay alive while the server is running
for some other reason
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 12130385
does ./serverprogram run in foreground?
does it spawn/fork other processes?
0
 

Author Comment

by:cameron_schuler
ID: 12133728
ok, exec won't work then because I need the script to be running or do I.....  If it uses the same PID then it might work the way I want it to.

0
 
LVL 40

Expert Comment

by:jlevie
ID: 12134149
You'd only need the script to still be running if the script needed to do other things after the program exited.
0
 

Author Comment

by:cameron_schuler
ID: 12134799
It's more complicated than that.   I have a program called "launcher" that starts about 10 different  programs at startup.  "launcher" need to be able to stop and restart all it's programs.    On of these programs is the script I mentioned above.  When "launcher" kills this script the "serverprogram" within stays alive so when launcher restarts the script it craps out because the server is already running.
0
 
LVL 40

Expert Comment

by:jlevie
ID: 12134850
Why not have "launcher" kill "serverprogram" that the script starts? That'll cause the script to exit automatically.
0
 

Author Comment

by:cameron_schuler
ID: 12136154
The way "launcher" works,  it can only kill the programs it starts.  I don't want to make a special case for this one program.

Anyway I don't think their is a solution to my exact question but I have been given some good alternatives.  So I'll give out the points.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

649 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