Shell – Replace instance of process in place

processprocess-managementshell-script

I suspect this is not doable just because of the security implications, but here's what I'd like to do.

Basically we run a bash shell-script on our CentOS server that calls Program-A (in our case JMeter, but that is arbitrary) which runs and dumps data into a log file. After that process finishes the shell-script starts up Program-B to analyze the log.

What I would like to do is stop the instance of Program-A1 and replace it with another instance of Program-A2, or some how swap them in place so I can safely end Program-A1 without starting up Program-B prematurely.

Why would I want to do this? The main reason is that Program-A loads some configuration files at its startup and if we make changes to those config files we have to restart the program for them to take effect, which I'd like to avoid.

I understand this is most likely not possible, but if it is I would greatly appreciate the information.

EDIT: I suppose I wasn't clear enough. Basically my shellscript looks like this:

./Program-A
./Program-B

When Program-A finishes dumping to our log, Program-B picks up the log and parses it. The problem I'm having is sometimes Program-A's settings are not set correctly or something is wrong with the environment when it starts which means that we'll need to run the whole thing again. We'd like to avoid that by just replacing our first instance of Program-A (calling it Program-A1) with a new instance of Program-A (we'll call this Program-A2). Does this make more sense to everyone?

The main reason we would like to do this is because Program-A and Program-B are actually a single part of a GIANT shell script that takes hours to run. Rather than restart the whole process for one individual part, we'd like to restart the single troubled program.

Best Answer

It looks to me as if you are trying to replace a RUNNING process from OUTSIDE the process. That's some radical stuff.

When I first looked at the question, it seemed that you are looking exactly for exec. But exec is called by the program itself. So unless you have coded the process so that you can force it to just exec another process, you can't do that while it is running.

You could potentially create a signal trap in your Program-A to exec a program with predefined name (which you can then set to be whatever you want) and then use kill on this process to force it to exec. From the outside, it will look like the process kept running - it does, it just becomes someone else. However, if you haven't done that and you want to do this on a running process, I don't think you can do anything.

However, if the outer shell is running, but it hasn't started the critical process yet, you can just replace the file of the problematic process.

Related Question