[Linux]服务器断开连接程序继续执行

在Linux系统中,通常我们在执行一些运行时间比较长的任务时,必须等待执行完毕才能断开SSH连接或关闭客户端软件,否则可能会导致执行中断。

例如我要下载一个文件,需要花费几个小时,但是我一断开ssh连接,下载就会停止。

该怎么解决呢?

可以使用linux自带的nohup命令,全称no hang up(不挂起),nohup命令的帮助信息如下:

(还可以使用screen执行,不过需要安装screnn工具,我不想麻烦...)

NOHUP(1)                            User Commands                       NOHUP(1)                    

NAME
       nohup - run a command immune to hangups, with output to a non-tty

SYNOPSIS
       nohup COMMAND [ARG]...
       nohup OPTION

DESCRIPTION
       Run COMMAND, ignoring hangup signals.

       --help display this help and exit

       --version
              output version information and exit

       If  standard  input  is a terminal, redirect it from /dev/null.  If standard output is a terminal, append
       output to 'nohup.out' if possible, '$HOME/nohup.out' otherwise.  If standard error is a  terminal,  redi‐
       rect it to standard output.  To save output to FILE, use 'nohup COMMAND > FILE'.

       NOTE:  your shell may have its own version of nohup, which usually supersedes the version described here.
       Please refer to your shell's documentation for details about the options it supports.

       GNU coreutils online help: <http://www.gnu.org/software/coreutils/>  Report  nohup  translation  bugs  to
       <http://translationproject.org/team/>

我们可以使用 nohup 你的命令 & 来实现这个功能,如下图

# nohup ping www.baidu.com &
[2] 8313
#nohup: ignoring input and appending output to ‘nohup.out’

还可以通过以下两条命令实现监控

jobs -l

效果如图:

tail -f nohup.out

效果如图:

最后我们可以使用kill命令结束进程

8313为我前面ping命令的进程号,再用jobs -l命令查看

kill -9 8313
jobs -l

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值