1. 使用nohup的问题及原因
    ​详见这里​
  2. 使用screen代替nohup
  • 安装
yum install -y screen
  • 进入screen独立终端
    screen命令无需root权限即可执行。
screen
或者
screen <command>
  • 在screen独立终端内拥有完整的bash环境,可以随意执行后台命令。
比如
./a_service
或者
./a_daemon &
  • 退出screen独立终端
按 ctrl + a ,再按 d 键
退出时会显示screen终端进程ID:
----------------------------------------------
[detached from 78898.pts-0.localhost]
  • 查看screen的终端进程ID
[user@localhost ~]$ screen -ls
There is a screen on:
78898.pts-0.localhost (Detached)
1 Socket in

这里的78898为screen终端进程ID。

  • 恢复screen终端
screen

将再次进入之前的screen独立终端中,且会发现前边运行的进程没有退出,还在继续执行。

[user@localhost ~]$ ps aux | grep -i screen   
user 78898 0.0 0.0 129852 1504 ? Ss 20:25 0:00 SCREEN

[user@localhost ~]$ sudo ps aux | grep -i top
root 78949 0.0 0.0 229264 5056 pts/1 S+ 20:31 0:00 sudo top
root 78952 0.3 0.0 158044 2564 pts/1 S+ 20:31 0:00 top

之前在screen独立终端中运行了sudo top命令。

  • 再次退出screen独立终端
按 Ctrl + d,或者执行命令logout
[screen is terminating]
[user@localhost ~]$ screen -ls
No Sockets found in