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

Killing a bash script does nto kill program executed within


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
cameron_schuler
Asked:
cameron_schuler
  • 7
  • 3
  • 3
  • +3
3 Solutions
 
jlevieCommented:
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
 
cameron_schulerAuthor Commented:
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
 
MysidiaCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
sunnycoderCommented:
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
 
sneeuw_chanCommented:
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
 
cameron_schulerAuthor Commented:
It's probably best that this thread dies as it looks like their is no solution to the problem.
0
 
sneeuw_chanCommented:
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
 
cameron_schulerAuthor Commented:
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
 
sneeuw_chanCommented:
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
 
cameron_schulerAuthor Commented:
Interesting... I'll give it a try tomorrow.
0
 
MysidiaCommented:
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
 
ahoffmannCommented:
does ./serverprogram run in foreground?
does it spawn/fork other processes?
0
 
cameron_schulerAuthor Commented:
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
 
jlevieCommented:
You'd only need the script to still be running if the script needed to do other things after the program exited.
0
 
cameron_schulerAuthor Commented:
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
 
jlevieCommented:
Why not have "launcher" kill "serverprogram" that the script starts? That'll cause the script to exit automatically.
0
 
cameron_schulerAuthor Commented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 7
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now