Home > Net >  How to stop a shell script running in the background?
How to stop a shell script running in the background?

Time:09-17

I run the first shell script and it starts printing. I pause the execution using CTRL-Z. I give the bg command and the first shell script starts running. I run a second shell script. Now both the scripts are printing simultaneously. Now I need to stop the execution of the first shell script only and the second shell script should continue printing. What commands can I use to do this ?

CodePudding user response:

If you catch the PID of each execution you can stop one of them with kill command

myCommand & echo $!

You can write a script for run the two command and catch the PID of each of them and stop the one you want with kill command.

CodePudding user response:

You can stop command2, send the signal to stop command1 and then let command2 continue

$ command1&
[1] 42510
$ command2
^Z
[2]   Stopped                 command2
$ jobs
[1]-  Running                 command1 &
[2]   Stopped                 command2
$ kill -STOP %1

[1]   Stopped                 command1
$ jobs
[1]   Stopped                 command1
[2]-  Stopped                 command2
$ kill -CONT %2
$ jobs
[1]   Stopped                 command1
[2]-  Running                 command2 &
  • Related