0

I've copied a list of several long-running commands into bash shell and pressed Enter. The commands are now being executed sequentially.

For example:

train_agi.sh
find_meaning_of_life.sh
destroy_universe.sh

Now I want the current command to complete, but the subsequent commands to be aborted.

What would be the best way to accomplish this? I was thinking about terminating the shell, but I'm not sure.

10
  • 2
    It would really help us if you add to your question the command expression you've used, so we may help you better. Overall, closing the terminal / terminating the current running tty shouldn't do harm if you know what you are running and what might be the outcome would be.
    – Pizza
    Apr 6, 2021 at 7:16
  • 1
    Also, please add your Ubuntu flavor and version to your question.
    – Pizza
    Apr 6, 2021 at 7:17
  • There is no command expression. As I wrote, I just written 10 lines / 10 commands in a text editor, copied them, opened the terminal with bash, pasted the lines using Ctrl+Shift+V and pressed Enter. Normal interactive terminal....
    – Ark-kun
    Apr 6, 2021 at 8:03
  • My mistake. I've probably chosen wrong StackOverflow website. My question is about Bash, not Ubuntu.
    – Ark-kun
    Apr 6, 2021 at 8:04
  • @Ark-kun, this information is not enough, no matter which Stackoverflow site. Without knowing the commands, it is difficult to help you. You could try add wait in between the commands.
    – pLumo
    Apr 6, 2021 at 8:10

1 Answer 1

1

You should be able to suspend all the commands, using Ctrl+Z as many times as there are queued commands, then kill the ones that you do not wish to run. You can then resume the first command by placing its job back in the foreground using fg (or background using bg, if you prefer).

To illustrate:

$ /bin/sleep 300 ; /bin/sleep 200 ; /bin/sleep 100
^Z
[1]+  Stopped                 /bin/sleep 300
^Z
[2]+  Stopped                 /bin/sleep 200
^Z
[3]+  Stopped                 /bin/sleep 100
steeldriver@DESKTOP-SPDO14V:~$
steeldriver@DESKTOP-SPDO14V:~$ jobs
[1]   Stopped                 /bin/sleep 300
[2]-  Stopped                 /bin/sleep 200
[3]+  Stopped                 /bin/sleep 100
steeldriver@DESKTOP-SPDO14V:~$
steeldriver@DESKTOP-SPDO14V:~$ kill %2 %3
[2]-  Terminated              /bin/sleep 200
[3]+  Terminated              /bin/sleep 100
steeldriver@DESKTOP-SPDO14V:~$
steeldriver@DESKTOP-SPDO14V:~$ fg %1
/bin/sleep 300
-
2
  • Seems racy to me - I tried with Ctrl-Z held down and got this: paste.ubuntu.com/p/pGnvvkj9zV
    – muru
    Apr 6, 2021 at 13:19
  • Thank you. I've eventually went with Ctrl+Z, but the result was somewhat strange. I've pressed Ctrl+Z and got ^Z [1]+ Stopped. I've then pressed Ctrl+C repeatedly until I got the interactive prompt. I then tried to use fg, but that resulted in Segmentation fault. Calling fg again printed bash: fg: current: no such job. The program (or main part of it) was still running though. I wanted to follow the job with something else. Calling wait XXX resulted in wait: pid XXX is not a child of this shell. So I've used loop polling /proc/XXX to wait for the job to finish.
    – Ark-kun
    Apr 6, 2021 at 23:36

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.