Pregunta

If I issue this at a bash prompt: $ cmd_1 ; cmd_2

cmd_1 is in the foreground. Can I prevent cmd_2 from running without halting cmd_1? I have until cmd_1 finishes, at which time cmd_2 will start, and then it is too late.

I don't think this is possible, but maybe with job control?

Does a -Z $ bg push both commands into the bg? Rather, does cmd_1 go to the bg and cmd_2 is (where? "pending background?" like @ cmd?) still waiting for cmd_1 to exit?

The commands were not invoked conditionally, ala "cmd_1 && cmd_2", nor were the commands backgrounded on invocation. cmd_1 is running in fg, cmd_2 has yet to execute.

The facepalm here is that cmd_1 is going to take a really long time to complete, cmd_2 is destructive and I'd really not like to have to interrupt and do cmd_1 over again. I'd just like cmd_1 to finish and cmd_2 to not run at all.

I could cause cmd_2 to fail by moving cmd_2 to cmd_2.sav until cmd_1 finishes and result in a "-bash: cmd_2: command not found" but I was hoping for a more elegant solution. cmd_2 might be used by others before cmd_1 is finished. (Yes, I could wrap cmd_2.real in a cmd_2 wrapper with a user test or somesuch, but this way lies madness).

¿Fue útil?

Solución

There could be a better way, but this would work:

  1. Press Control-Z to pause cmd_1.
  2. cmd_2 will start running. You can press Control-C to kill it, or you can do your "rename trick" before step 1 so that it will have no chance of starting.
  3. Once cmd_2 is killed/prevented from starting, type fg to resume cmd_1 operation.

If you want to try this out ahead of time to ensure the behavior you get is what you're expecting, practice it with a combination of sleep and echo commands. i.e., run sleep 10; echo "hello" from the terminal. Pressing Control-Z suspends the sleep command, and "hello" is output. Typing fg resumes the sleep.

Otros consejos

In this particular case, just add...

echo "Press a key to begin... ${0}"
read

At the start of cmd_2. This way you don't seriously hamper someone wanting to use cmd_2 and you give yourself a chance to kill it before it destroys something you want to preserve.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top