Solved

Killing a bash script does nto kill program executed within

Posted on 2004-09-21
17
810 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
  • 7
  • 3
  • 3
  • +3
17 Comments
 
LVL 40

Expert Comment

by:jlevie
Comment Utility
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
Comment Utility
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 25 total points
Comment Utility
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
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 2

Accepted Solution

by:
sneeuw_chan earned 50 total points
Comment Utility
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
Comment Utility
Interesting... I'll give it a try tomorrow.
0
 
LVL 23

Expert Comment

by:Mysidia
Comment Utility
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
Comment Utility
does ./serverprogram run in foreground?
does it spawn/fork other processes?
0
 

Author Comment

by:cameron_schuler
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Why not have "launcher" kill "serverprogram" that the script starts? That'll cause the script to exit automatically.
0
 

Author Comment

by:cameron_schuler
Comment Utility
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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

728 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

10 Experts available now in Live!

Get 1:1 Help Now