Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I tell bash to run a command after the currently executing command has completed?

If I've started a process that turns out to be long-running, can I "queue up" a command to run immediately after it?

I know I can do this with cmd1 && cmd2 syntax if I was starting from scratch, but what if cmd1 is already running?

like image 955
jl6 Avatar asked Jan 01 '13 00:01

jl6


People also ask

How do you run a command after the previous finished in Linux?

Using the && Operator. We should note that the && operator executes the second command only if the first one returns an exit code of 0. If we want to run the next script, even if the first one fails, we can replace the && operator with the; operator, which runs the next command regardless of the exit code.

Does bash wait for command to finish before executing next?

The bash WAIT command is used to halt the execution of a script until all background jobs or specified JobID/PIDs terminate successfully and return an expected exit code to trigger the next command that was “waited for.”

Which command will run the second program only if the first program succeeded?

Using double ampersands (&&) introduces error checking. The second command will run only if the first command is successful.


2 Answers

You can press C-z to stop the current foreground task, then type "fg %%; some-other-command" to resume the task and run another command afterwards.

like image 88
Evan S. Avatar answered Sep 20 '22 18:09

Evan S.


If the currently-running command isn't reading input, you can just type the command in the same window. Bash will read the input and run the command when the current one finishes.

like image 26
Mark Reed Avatar answered Sep 23 '22 18:09

Mark Reed