1

Note: I h ave moved this to stack-overflow, I think that is the right place: here

I have a strange issue, I can't quite get my head around. What I do is run a function called fn_rundumper which basically just calls the bash code: 'pterm -z "cmd.sh" param1 &'

Here is the output of that, which includes the background pid "27938858":

fn_rundumper
running fn_dumper...
[1] 27938858
fn_dumper...done

Then I look at the job numbers that are running this also shows PID "27938858":

jobs -l
[1] + 27938858 Running              $(pterm -z "$SCRIPTS_DIR/run_dumper.sh" $VO

Then I do a ps command to see what pterms are running:

ps | grep pterm
 24285189 ?        00:00:00 pterm
 27938859 ?        00:00:00 pterm

This yields the PID "27938859". This is different to the others!

When I kill PID 27938858 nothing seems to happen. When I kill PID 27938859 then the background pterm is closed.

The problem is that I may want to run many different background pterms as well as some pterms that are not opened as background tasks (i.e. run separately). So when I come to tidy up I just want to close the pterms that I have opened via my script.

I was trying to use jobs -l to see the PIDs I need to close, but as I just described this is the wrong PID.

Can anyone explain why this happens? and also what I need to do to get the correct PID? Thanks!

- edit - The best I can think of is to do a 'ps | grep pterm' before and after and compare the results to find the new pterm PID... do-able, but ugly :(

2

What's happening is that you are creating two processes, the first is that of your function fn_rundumper, and the second is that of pterm opened by your function. That's way your function call takes a pid ending with 858, and pterm called from your function takes the pid next after it, ending with 859.

As for the pid of the first pterm, it is that of the terminal you used to execute your function. That's why killing it will kill everything created in it (as long as you are not using the nohup command).

To see the same pid in a ps command, try: ps | grep run_dumper.sh, which is the pid of the bash script that runs your fn_rundumper function.

4
  • Hi Linostar, This did not quite work, I don't think that running it from a function has any effect. But this did point me in an important direction. I tested it again all on the command line (no function call). When I use back-tick around the function it has the same behaviour. If I don't put back-tick around the function then the PIDs are all correct! i.e.: "pterm -z "$VOE_SCRIPTS_DIR/run_dumper.sh" $PROJECT_DIR &" becomes: "pterm -z "$VOE_SCRIPTS_DIR/run_dumper.sh" $PROJECT_DIR &" .... so back tick must be adding a PID level. ALso, sorry, I moved this into stack overflow, please see link – code_fodder Feb 2 '15 at 10:30
  • But +1 for that pointer : ) – code_fodder Feb 2 '15 at 10:31
  • Were you using backticks? Yes, backticks will create a separate process as you already deduced. Glad to be able to help a bit. – Linostar Feb 2 '15 at 11:04
  • Yes I was! - I only guessed what was happening, but its a strange behaviour. Your notes got me where I wanted to go thanks! – code_fodder Feb 2 '15 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.