Linux 前台与后台
2016-06-03 by dongnan
举个栗子
正在使用 vim 编辑 nginx.conf,需要重启下 nginx 进程。
进入后台
使用 Ctrl + Z 键
[1]+  Stopped                 vim nginx.conf
重启nginx进程
nginx -s reload
查看后台任务
# 执行命令
jobs
[1]+  Stopped                 vim nginx.conf
进入前台
# 执行命令
fg
vim nginx.conf
执行fg命令回到vim任务。
扩展
也许您希望退出 shell 终端后,任务继续执行,那么请参考 nohub 命令。
nohup功能
nohup 不挂起的意思,即使退出 shell 终端命令依然在后台执行。
命令格式
nohup command &
nohup例子
# 执行命令
nohup wget -c wget http://mirrors.yun-idc.com/epel/6/x86_64/epel-release-6-8.noarch.rpm &
# 退出当前的终端
exit
注意,nohup 后面加入需要的命令, 符号 & 表示后台运行。
验证
再次登陆,仍然可以找到 wget 命令的进程。
# 执行命令
ps aux | grep wget
root     29660  0.0  0.4 134512  2276 ?        S    10:31   0:00 wget -c wget http://mirrors.yun-idc.com/epel/6/x86_64/epel-release-6-8.noarch.rpm
# nohup 会自动日志
tail nohup.out
--2014-05-31 10:33:38--  (try: 3)  http://mirrors.yun-idc.com/epel/6/x86_64/epel-release-6-8.noarch.rpm