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

孤街醉人 提交于 2019-11-28 08:23:56

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.
israelss

Another way is using the nohup command with & at the end of the line.

Something like this

nohup whatevercommandyouwant whateverparameters &

This will run it in the background and send its output to a nohup.log file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!