Solved

Killing a bash script does nto kill program executed within

Posted on 2004-09-21
17
812 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
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 25 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 50 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 50 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CentOS 6.6 Port opening question 1 137
pipe to sed or perl, please help 5 128
„dd“ vs „ddrescue“ 28 232
Openwrt vnstat 9 185
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 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