Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
558 views
in Technique[技术] by (71.8m points)

bash - run a shell script and immediately background it, however keep the ability to inspect its output

How can I run a shell script and immediately background it, however keep the ability to inspect its output any time by tailing /tmp/output.txt

It would be nice if I can foreground the process too later.

PS It would be really cool if you can also show me how to "send" the backgrounded process in to a gnu screen that may or may not have been initialized.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To 'background' a process when you start it

Simply add an ampersand (&) after the command.

If the program writes to standard out, it will still write to your console/terminal.

To foreground the process, simply use the fg command.

(You can see a list of jobs in the background with jobs.)

for example:

sh -c 'sleep 3 && echo I just woke up' & jobs

To background a currently running process

If you have already started the process in the foreground, but you want to move it to the background, you can do the following:

  1. Press Ctrl+z to put the current process to sleep and return to your shell. (This process will be paused until you send it another signal.)
  2. Run the bg command to resume the process, but have it run in the background instead of the foreground.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...