0

I'm running some simulations for which I have scripts that launch them in a for loop.

Let's say script.sh looks a bit like this

#!/bin/bash

for i in {0..N}
do
    # do some stuff

    command # this line actually launches the simulation
done

Now, I usually launch several of these scripts at the same time and command is the same for all of them, so if I open top or htop I will have several lines that just say command, their only difference being the PID.

Sometimes, for whatever reason, I need to stop one of these commands, but I don't really know which PID the one I want to stop is. Let's say I start each script.sh from a different directory. Can I do something before starting command to keep track of the PID of each of them, say export their PID to a file, so if in some directory I see something has gone wrong and I need to stop it, I can kill just that one?

Thanks.

MyUserIsThis
  • 337
  • 1
  • 3
  • 14
  • It depends on how the background processes that run the simulations. If you use `&` at the end of the command, the special parameter `$!` will have the PID *after* the command is started. If the backgrounding is something that `command` does, you'll probably have to get it to tell you what it's done. – Gordon Davisson Nov 28 '21 at 06:57

0 Answers0