3 个回答

老生常谈的问题。如果你的进程需要长时间稳定的后台运行,那么建议写成service。对于CentOS 7, Ubuntu 16.04等操作系统用的是systemd做service manager,写systemd脚本即可。对于CentOS 6, Ubuntu 14.04这些操作系统,用的是upstart管理的服务,你可以写upstart服务脚本。相关的资料自行查阅资料即可,写起来也不难。

如果只是临时需要后台运行,那么可以考虑放到screen或者tmux虚拟终端中运行,可以帮你hold住session,防止意外断开。

在本地虚拟机配置ServerAliveInterval:

vim ~/.ssh/config
ServerAliveInterval=30

或者在服务器上配置ClientAliveInterval:

vim ~/.ssh/config
ClientAliveInterval=30

稍微解释一下:

  • ServerAliveInterval : number of seconds that the client will wait before sending a null packet to the server (to keep the connection alive).(每隔30秒客户端给服务端发送空包)
  • ClientAliveInterval : number of seconds that the server will wait before sending a null packet to the client (to keep the connection alive).(每隔30秒服务端给客户端发送空包)

@Feng_Yu 说的 2 种方式都可行。

除此之外还可以使用 pm2 supervisor 等进程管理工具

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题