1

I can easily start 3 processes on 3 different terminals, and kill each one by pressing Ctrl+C. Now, is there any way to start all 3 processes at once, and then finish them equally easily? Ideas:

  1. If I could start 3 processes in such a way that they would run on the same terminal, and Ctrl+C would kill all 3, that would work.

  2. If I could create two scripts, init.sh and kill.sh that would start/kill the 3 processes, that would work too.

Both of those work because they are easy. Having to spawn a process on the background, then finding its pid, then copying it, then killing it with yet another command isn't easy.

Kusalananda
  • 333,661

1 Answers1

9

Using bash's job control:

$ sleep 10m & sleep 11m & sleep 12m &
[1] 1821
[2] 1822
[3] 1823
$ jobs
[1]   Running                 sleep 10m &
[2]-  Running                 sleep 11m &
[3]+  Running                 sleep 12m &
$ kill %1 %2 %3
$ jobs
[1]   Terminated: 15          sleep 10m
[2]-  Terminated: 15          sleep 11m
[3]+  Terminated: 15          sleep 12m

In bash, running command & sends it to the background. This way, you can start multiple commands in the same shell, running in the background. The kill builtin can be used to kill these background jobs. The first (oldest) active job is %1, the next is %2 and so on. Also see: Kill all background jobs

muru
  • 72,889