参考链接:Linux的bg和fg命令简单介绍
在工作中,往往需要连接远程服务器,这是一个连接只有一个窗口(当然可以用xshell开启多个窗口连接),那么如何在当前窗口同时运行多个程序呢?这就需要用到&,bg,fg和nohup了。
关于 &
在命令的最后加上&
,则可以安静运行程序(如果程序没问题或者不会一下子就运行完的话,击打任意键后程序就在后台运行着):
jobs查看后台运行的程序
用jobs
命令查看后台运行的程序,可以看到后台程序和其编号i
:
fg命令将后台运行的命令调用到前台:foreground
用fg i
命令将后台运行的程序调用到前面来,其中i为jobs
命令查看到的编号:
当然,fg
也可以将ctrl+z
后挂起的程序(见下)唤醒到前台。
ctrl + z 将正在运行着的程序暂停(挂起)
当程序在当前窗口运行着时,用ctrl+z
将正在运行着的程序暂停,或者说挂起:
bg命令将挂起的程序放到后台运行
当程序挂起时,可以用bg i
命令,将挂起的命令放于后台背景运行,其中i
是jobs
的编号。
nohup命令
如果希望关掉ssh窗口后,程序依旧运行,并且可以重新登录新的ssh连接后还能看到程序的输出日志,那么就得用nohup命令。格式如下:
nohup 命令 [2>&1 重定向文件名] &
其中[]内的可选,如果没有,则将日志默认输出到当前目录下的nohup.out文件。
nohup
之后似乎无法重新唤醒到前台,只能通过tail -f 日志文件名
来查看运行日志。
ps命令查看nohup的程序
使用nohup
命令,并关闭当前ssh连接后,再重连,是不能通过jobs
查看运行着的进程的。
这时可以通过ps -f | grep 程序名
来查找该进程的pid
,方便查看是否运行和kill
掉它。
问题
screen
功能是什么,似乎也可以做到后台运行?
nohup
中的2>&1
是什么意思?
以后有时间再完善。
Together with the whole thing which seems to be developing inside this specific subject matter, all your perspectives are actually quite stimulating. However, I am sorry, because I do not subscribe to your whole strategy, all be it stimulating none the less. It looks to everyone that your comments are actually not completely justified and in reality you are generally your self not completely confident of your point. In any case I did appreciate examining it.
I’ll right away grab your rss as I can not find your email subscription link or newsletter service. Do you’ve any? Please let me know so that I could subscribe. Thanks.
I have checked your page and i have found some duplicate content, that’s why you
don’t rank high in google’s search results, but there is a tool that can help you
to create 100% unique content, search for; Boorfe’s tips unlimited content