Run command in background with foreground terminal access












4















I am trying to create a function that can run an arbitrary command, interact with the child process (specifics omitted), and then wait for it to exit. If successful, typing run <command> will appear to behave just like a bare <command>.



If I weren't interacting with the child process I would simply write:



run() {
"$@"
}


But because I need to interact with it while it runs, I have this more complicated setup with coproc and wait.



run() {
exec {in}<&0 {out}>&1 {err}>&2
{ coproc "$@" 0<&$in 1>&$out 2>&$err; } 2>/dev/null
exec {in}<&- {out}>&- {err}>&-

# while child running:
# status/signal/exchange data with child process

wait
}


(This is a simplification. While the coproc and all the redirections aren't really doing anything useful here that "$@" & couldn't do, I need them all in my real program.)



The "$@" command could be anything. The function I have works with run ls and run make and the like, but it fails when I do run vim. It fails, I presume, because Vim detects that it is a background process and doesn't have terminal access, so instead of popping up an edit window it suspends itself. I want to fix it so Vim behaves normally.



How can I make coproc "$@" run in the "foreground" and the parent shell become the "background"? The "interact with child" part neither reads from nor writes to the terminal, so I don't need it to run in the foreground. I'm happy to hand over control of the tty to the coprocess.



It is important for what I'm doing that run() be in the parent process and "$@" be in its child. I can't swap those roles. But I can swap the foreground and background. (I just don't know how to.)



Note that I am not looking for a Vim-specific solution. And I would prefer to avoid pseudo-ttys. My ideal solution would work equally well when stdin and stdout are connected to a tty, to pipes, or are redirected from files:



run echo foo                               # should print "foo"
echo foo | run sed 's/foo/bar/' | cat # should print "bar"
run vim # should open vim normally









share|improve this question




















  • 1





    What does "interact with the child process" entail? The script interacting with it, programmatically? You interacting with it? Are you reinventing expect?

    – JdeBP
    6 hours ago











  • The script interacting with it, not the user. Maybe statusing it, sending it a signal, something like that. (In my full script I'm using the coproc FIFO as a side channel to send and receive data.)

    – John Kugelman
    5 hours ago
















4















I am trying to create a function that can run an arbitrary command, interact with the child process (specifics omitted), and then wait for it to exit. If successful, typing run <command> will appear to behave just like a bare <command>.



If I weren't interacting with the child process I would simply write:



run() {
"$@"
}


But because I need to interact with it while it runs, I have this more complicated setup with coproc and wait.



run() {
exec {in}<&0 {out}>&1 {err}>&2
{ coproc "$@" 0<&$in 1>&$out 2>&$err; } 2>/dev/null
exec {in}<&- {out}>&- {err}>&-

# while child running:
# status/signal/exchange data with child process

wait
}


(This is a simplification. While the coproc and all the redirections aren't really doing anything useful here that "$@" & couldn't do, I need them all in my real program.)



The "$@" command could be anything. The function I have works with run ls and run make and the like, but it fails when I do run vim. It fails, I presume, because Vim detects that it is a background process and doesn't have terminal access, so instead of popping up an edit window it suspends itself. I want to fix it so Vim behaves normally.



How can I make coproc "$@" run in the "foreground" and the parent shell become the "background"? The "interact with child" part neither reads from nor writes to the terminal, so I don't need it to run in the foreground. I'm happy to hand over control of the tty to the coprocess.



It is important for what I'm doing that run() be in the parent process and "$@" be in its child. I can't swap those roles. But I can swap the foreground and background. (I just don't know how to.)



Note that I am not looking for a Vim-specific solution. And I would prefer to avoid pseudo-ttys. My ideal solution would work equally well when stdin and stdout are connected to a tty, to pipes, or are redirected from files:



run echo foo                               # should print "foo"
echo foo | run sed 's/foo/bar/' | cat # should print "bar"
run vim # should open vim normally









share|improve this question




















  • 1





    What does "interact with the child process" entail? The script interacting with it, programmatically? You interacting with it? Are you reinventing expect?

    – JdeBP
    6 hours ago











  • The script interacting with it, not the user. Maybe statusing it, sending it a signal, something like that. (In my full script I'm using the coproc FIFO as a side channel to send and receive data.)

    – John Kugelman
    5 hours ago














4












4








4








I am trying to create a function that can run an arbitrary command, interact with the child process (specifics omitted), and then wait for it to exit. If successful, typing run <command> will appear to behave just like a bare <command>.



If I weren't interacting with the child process I would simply write:



run() {
"$@"
}


But because I need to interact with it while it runs, I have this more complicated setup with coproc and wait.



run() {
exec {in}<&0 {out}>&1 {err}>&2
{ coproc "$@" 0<&$in 1>&$out 2>&$err; } 2>/dev/null
exec {in}<&- {out}>&- {err}>&-

# while child running:
# status/signal/exchange data with child process

wait
}


(This is a simplification. While the coproc and all the redirections aren't really doing anything useful here that "$@" & couldn't do, I need them all in my real program.)



The "$@" command could be anything. The function I have works with run ls and run make and the like, but it fails when I do run vim. It fails, I presume, because Vim detects that it is a background process and doesn't have terminal access, so instead of popping up an edit window it suspends itself. I want to fix it so Vim behaves normally.



How can I make coproc "$@" run in the "foreground" and the parent shell become the "background"? The "interact with child" part neither reads from nor writes to the terminal, so I don't need it to run in the foreground. I'm happy to hand over control of the tty to the coprocess.



It is important for what I'm doing that run() be in the parent process and "$@" be in its child. I can't swap those roles. But I can swap the foreground and background. (I just don't know how to.)



Note that I am not looking for a Vim-specific solution. And I would prefer to avoid pseudo-ttys. My ideal solution would work equally well when stdin and stdout are connected to a tty, to pipes, or are redirected from files:



run echo foo                               # should print "foo"
echo foo | run sed 's/foo/bar/' | cat # should print "bar"
run vim # should open vim normally









share|improve this question
















I am trying to create a function that can run an arbitrary command, interact with the child process (specifics omitted), and then wait for it to exit. If successful, typing run <command> will appear to behave just like a bare <command>.



If I weren't interacting with the child process I would simply write:



run() {
"$@"
}


But because I need to interact with it while it runs, I have this more complicated setup with coproc and wait.



run() {
exec {in}<&0 {out}>&1 {err}>&2
{ coproc "$@" 0<&$in 1>&$out 2>&$err; } 2>/dev/null
exec {in}<&- {out}>&- {err}>&-

# while child running:
# status/signal/exchange data with child process

wait
}


(This is a simplification. While the coproc and all the redirections aren't really doing anything useful here that "$@" & couldn't do, I need them all in my real program.)



The "$@" command could be anything. The function I have works with run ls and run make and the like, but it fails when I do run vim. It fails, I presume, because Vim detects that it is a background process and doesn't have terminal access, so instead of popping up an edit window it suspends itself. I want to fix it so Vim behaves normally.



How can I make coproc "$@" run in the "foreground" and the parent shell become the "background"? The "interact with child" part neither reads from nor writes to the terminal, so I don't need it to run in the foreground. I'm happy to hand over control of the tty to the coprocess.



It is important for what I'm doing that run() be in the parent process and "$@" be in its child. I can't swap those roles. But I can swap the foreground and background. (I just don't know how to.)



Note that I am not looking for a Vim-specific solution. And I would prefer to avoid pseudo-ttys. My ideal solution would work equally well when stdin and stdout are connected to a tty, to pipes, or are redirected from files:



run echo foo                               # should print "foo"
echo foo | run sed 's/foo/bar/' | cat # should print "bar"
run vim # should open vim normally






shell tty background-process job-control coprocesses






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 5 hours ago







John Kugelman

















asked 7 hours ago









John KugelmanJohn Kugelman

1,67411018




1,67411018








  • 1





    What does "interact with the child process" entail? The script interacting with it, programmatically? You interacting with it? Are you reinventing expect?

    – JdeBP
    6 hours ago











  • The script interacting with it, not the user. Maybe statusing it, sending it a signal, something like that. (In my full script I'm using the coproc FIFO as a side channel to send and receive data.)

    – John Kugelman
    5 hours ago














  • 1





    What does "interact with the child process" entail? The script interacting with it, programmatically? You interacting with it? Are you reinventing expect?

    – JdeBP
    6 hours ago











  • The script interacting with it, not the user. Maybe statusing it, sending it a signal, something like that. (In my full script I'm using the coproc FIFO as a side channel to send and receive data.)

    – John Kugelman
    5 hours ago








1




1





What does "interact with the child process" entail? The script interacting with it, programmatically? You interacting with it? Are you reinventing expect?

– JdeBP
6 hours ago





What does "interact with the child process" entail? The script interacting with it, programmatically? You interacting with it? Are you reinventing expect?

– JdeBP
6 hours ago













The script interacting with it, not the user. Maybe statusing it, sending it a signal, something like that. (In my full script I'm using the coproc FIFO as a side channel to send and receive data.)

– John Kugelman
5 hours ago





The script interacting with it, not the user. Maybe statusing it, sending it a signal, something like that. (In my full script I'm using the coproc FIFO as a side channel to send and receive data.)

– John Kugelman
5 hours ago










1 Answer
1






active

oldest

votes


















1














Those three examples you gave should work if you add this line after wait:



jobs |grep -qi stopped && fg %1


So the script becomes:



run() {
exec {in}<&0 {out}>&1 {err}>&2
{ coproc "$@" 0<&$in 1>&$out 2>&$err; } 2>/dev/null
exec {in}<&- {out}>&- {err}>&-
wait
jobs |grep -qi stopped && fg %1
}


Though the %1 needs to change if you're running concurrent jobs. Then it gets more complicated because you then have to code identification of the job from the jobs output.






share|improve this answer
























  • I'd prefer it if Vim didn't suspend itself in the first place. This would be a complicated workaround to implement because I want the "interact with child" bits to run before I call wait. The interaction I'm doing is in fact a loop that runs until the co-process finishes. The wait is merely a formality to get its exit code; it doesn't actually do any waiting.

    – John Kugelman
    5 hours ago













Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f507786%2frun-command-in-background-with-foreground-terminal-access%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Those three examples you gave should work if you add this line after wait:



jobs |grep -qi stopped && fg %1


So the script becomes:



run() {
exec {in}<&0 {out}>&1 {err}>&2
{ coproc "$@" 0<&$in 1>&$out 2>&$err; } 2>/dev/null
exec {in}<&- {out}>&- {err}>&-
wait
jobs |grep -qi stopped && fg %1
}


Though the %1 needs to change if you're running concurrent jobs. Then it gets more complicated because you then have to code identification of the job from the jobs output.






share|improve this answer
























  • I'd prefer it if Vim didn't suspend itself in the first place. This would be a complicated workaround to implement because I want the "interact with child" bits to run before I call wait. The interaction I'm doing is in fact a loop that runs until the co-process finishes. The wait is merely a formality to get its exit code; it doesn't actually do any waiting.

    – John Kugelman
    5 hours ago


















1














Those three examples you gave should work if you add this line after wait:



jobs |grep -qi stopped && fg %1


So the script becomes:



run() {
exec {in}<&0 {out}>&1 {err}>&2
{ coproc "$@" 0<&$in 1>&$out 2>&$err; } 2>/dev/null
exec {in}<&- {out}>&- {err}>&-
wait
jobs |grep -qi stopped && fg %1
}


Though the %1 needs to change if you're running concurrent jobs. Then it gets more complicated because you then have to code identification of the job from the jobs output.






share|improve this answer
























  • I'd prefer it if Vim didn't suspend itself in the first place. This would be a complicated workaround to implement because I want the "interact with child" bits to run before I call wait. The interaction I'm doing is in fact a loop that runs until the co-process finishes. The wait is merely a formality to get its exit code; it doesn't actually do any waiting.

    – John Kugelman
    5 hours ago
















1












1








1







Those three examples you gave should work if you add this line after wait:



jobs |grep -qi stopped && fg %1


So the script becomes:



run() {
exec {in}<&0 {out}>&1 {err}>&2
{ coproc "$@" 0<&$in 1>&$out 2>&$err; } 2>/dev/null
exec {in}<&- {out}>&- {err}>&-
wait
jobs |grep -qi stopped && fg %1
}


Though the %1 needs to change if you're running concurrent jobs. Then it gets more complicated because you then have to code identification of the job from the jobs output.






share|improve this answer













Those three examples you gave should work if you add this line after wait:



jobs |grep -qi stopped && fg %1


So the script becomes:



run() {
exec {in}<&0 {out}>&1 {err}>&2
{ coproc "$@" 0<&$in 1>&$out 2>&$err; } 2>/dev/null
exec {in}<&- {out}>&- {err}>&-
wait
jobs |grep -qi stopped && fg %1
}


Though the %1 needs to change if you're running concurrent jobs. Then it gets more complicated because you then have to code identification of the job from the jobs output.







share|improve this answer












share|improve this answer



share|improve this answer










answered 5 hours ago









Colin PearseColin Pearse

262




262













  • I'd prefer it if Vim didn't suspend itself in the first place. This would be a complicated workaround to implement because I want the "interact with child" bits to run before I call wait. The interaction I'm doing is in fact a loop that runs until the co-process finishes. The wait is merely a formality to get its exit code; it doesn't actually do any waiting.

    – John Kugelman
    5 hours ago





















  • I'd prefer it if Vim didn't suspend itself in the first place. This would be a complicated workaround to implement because I want the "interact with child" bits to run before I call wait. The interaction I'm doing is in fact a loop that runs until the co-process finishes. The wait is merely a formality to get its exit code; it doesn't actually do any waiting.

    – John Kugelman
    5 hours ago



















I'd prefer it if Vim didn't suspend itself in the first place. This would be a complicated workaround to implement because I want the "interact with child" bits to run before I call wait. The interaction I'm doing is in fact a loop that runs until the co-process finishes. The wait is merely a formality to get its exit code; it doesn't actually do any waiting.

– John Kugelman
5 hours ago







I'd prefer it if Vim didn't suspend itself in the first place. This would be a complicated workaround to implement because I want the "interact with child" bits to run before I call wait. The interaction I'm doing is in fact a loop that runs until the co-process finishes. The wait is merely a formality to get its exit code; it doesn't actually do any waiting.

– John Kugelman
5 hours ago




















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f507786%2frun-command-in-background-with-foreground-terminal-access%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Entries order in /etc/network/interfaces

新発田市

Grub takes very long (several minutes) to open Menu (in Multi-Boot-System)