I am trying to run several commands at the same time. I prepare it in a script by appending each command to a single command that would look something like
and=" & "
command=$command1$and$command2
eval $command
However, when I run this, each command is forked and completes correctly, but it then hangs at the command line and never returns me to the prompt.
Example (script run.sh). If the two commands are:
command1="echo 'Hello'"
command2="echo 'World'"
then the output is:
person: ./run.sh
Hello
World
## It Hangs here
instead of
person: ./run.sh
Hello
World
person:
so how do I terminate the forked processes, or have the script block until the forked processes complete?
Assuming your examples are simplified (since 'echo' should complete immediately and return to the prompt), you just need to background the second command. IOW, try:
command="$command1 & $command2 &"
Note that I've removed your "and" variable, since it is mere obfuscation. A single &
is in no way an and
operation.
Are you getting output like this:
~/dev/other $ echo foo &
[1] 522
~/dev/other $ foo
▯ # blink, blink, blink…
In this case you are being returned to the prompt, but the prompt is appearing before echo
completes, and thus the string to be echo
ed is appearing after the prompt, followed by a newline. You can write your next command on that new line after the prompt, or just hit enter
to get a new prompt.
If this is happening in a script, you can add the builtin wait
to the end of the script to ensure that the script doesn't return the prompt before all of its backgrounded child processes are complete.
我认为您的命令应如下所示:
command= $command1$and$command2$and
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.