Linux SSH 客户端断开后保持进程继续运行配置步骤

文章目录

Linux 系统中,通常我们在执行一些运行时间比较长的任务时,必须等待执行完毕才能断开 SSH 连接或关闭客户端软件,否则可能会导致执行中断。本文介绍几种保障程序在用户退出登录后持续运行的方法。

使用管理终端执行


通过 管理终端(IPMI等) 会登录服务器的本地会话,在该终端执行的程序不会受到 SSH 登录用户退出的影响。具体操作方式如下:

1. 通过 管理终端(IPMI等) 登录服务器。

2. 执行所需程序或脚本。

3. 下次需要查看任务执行状态时,再次连接管理终端查看即可。

使用 nohup 执行


nohup 的作用顾名思义,它使得后面的命令不会响应挂断(SIGHUP)信号。也就是说,通过远程登录执行 nohup 后,即使退出登录后,程序还是会正常执行。通常情况下,nohup 命令最后会跟上 & 字符,表示将这个命令放至后台执行,这样才能真正做到将这个命令放至后台持续的执行。

操作示例:
1. 正常的执行命令为 bash hello.sh,执行结果为每秒输出一行的小程序:

Linux SSH 客户端断开后保持进程继续运行配置步骤

2. 在命令头尾分别加上 nohup 和 &,变为 nohup bash hello.sh &,可以看到 nohup 输出了一行信息,再按一下回车键就跳回了 shell 命令行,此时命令已经在后台执行了,nohup 将命令的输出重定向至当前目录的 nohup.out 文件中。同时注意到 nohup 会将对应程序的 PID 输出,PID 可用于需要中断进程时 kill 进程。

Linux SSH 客户端断开后保持进程继续运行配置步骤

3. 通过 tail -f nohup.out 可以持续的查看 nohup.out 的输出,达到监视程序的效果。

Linux SSH 客户端断开后保持进程继续运行配置步骤

4. 在命令中也可以使用重定向将程序的输出改为自己想要的文件名,如 nohup bash hello.sh >hello.log &,则程序的输出就会写到 hello.log 文件中。

5. 若程序不会自动退出,那么此时需要使用 kill 命令来结束进程。比如,可以使用命令 kill -TRM <PID> 来操作,其中 PID 即为之前 nohup 输出的值,在此例中该值为 1231。

使用限制:

nohup 通常用于执行无干预的自动化程序或脚本,无法完成带有交互的操作。

Linux SSH 客户端断开后保持进程继续运行配置步骤

使用 screen 执行(推荐)


安装 sceen 工具

Linux 系统默认未自带 screen 工具,需要先进行安装:

  • CentOS 系列系统: yum install screen
  • Ubuntu 系列系统: sudo  apt-get  install screen

使用简介

1. 创建 screen 窗口

  1. screen -S name
  2. # name可以设置为ssh、ftp,用于标注该 screen 窗口用途
  3. # 示例:
  4. screen -S ftp

2. 列出 screen 进程,并进入所需 screen

  1. screen -ls
  2. ##列出 screen 进程列表

如下图

Linux SSH 客户端断开后保持进程继续运行配置步骤

然后进行所需操作,比如运行脚本、执行程序等等。

如下图示例:创建ftp连接后台下载传输文件

Linux SSH 客户端断开后保持进程继续运行配置步骤

3. 退出保存

前述 ftp 操作示例开始传输后,在窗口中键入Ctrl+a 键,再按下 d 键,就可以退出 SSH 登录,但不会影响 screen 程序的执行。

4. 保存会话以便继续执行

可以利用 screen 这种功能来管理的远程会话。操作步骤概述:

  • 正常 SSH 登录服务器
  • 创建 screen 窗口
  • 执行所需任务
  • 需要临时中断退出时,按 Ctrl +d 保存退出
  • 需要继续工作时,再次 SSH 登录服务器,然后直接执行 screen -r -d 恢复会话即可。

此文章来自固定链接:http://www.rakbuluo.com/p27/

原文链接:,转发请注明来源!

发表评论

  • 57 Responses to “Linux SSH 客户端断开后保持进程继续运行配置步骤”
    • Lulxrkwj

      molina healthcare login is buying cialis online illegal
      <a href="https://withoutshort.com">buy viagra</a> Emnvczt
      https://withoutshort.com viagra pills

      回复
    • YDwene

      <a href=http://www.canadian2pharmacy.com>viagra online</a> http://www.withoutdrvisit.com

      回复
    • Erbqvub

      how long does a viagra take to kick in treatment of sunstroke does viagra affect young men <a href="https://viawithoutdctrs.com/#">viagra prescription</a> - acheter cialis generique belgique https://viawithoutdctrs.com/# - meglio il cialis o levitra northeast district department of health ct

      回复
    • Nbmoprest

      cialis professional <a href="https://buycialisxz.com/ ">http://buycialisxz.com/ </a> cialis dapoxetine

      回复
    • Fjjuprest

      what viagra does <a href="http://viagraonlinejc.com/ ">turmeric viagra </a> how often can i take viagra

      回复
    • Lqxdgjy

      find a doctor near me can i take two 10mg cialis
      <a href="https://withoutshort.com">how much watermelon viagra</a> Anhtdua
      https://withoutshort.com viagra generic

      回复
    • JDwene

      <a href=http://www.canadian2pharmacy.com>viagra without doctor prescription</a> http://www.viagwithoutdr.com

      回复
    • Kndnboop

      best online pharmacy for viagra <a href="http://viagratx.com/ ">is viagra taxed</a> viagra after prostate removal

      回复
    • Ajqdn92

      cialis facial flushing wv department of health and human resources cialis and radical prostatectomy <a href="https://tadalafilkiva.com/#">tadalafil 20 mg</a> - cialis se comprar sin receta https://tadalafilkiva.com/# - how to use liquid cialis pcp search

      回复
    • IDwene

      <a href=http://www.viaprescription.com>vardenafil</a> http://www.canadian2pharmacy.com

      回复
    • Iamburce

      You made your position pretty clearly!. <a href=https://www.goldkamagra.com>ajanta kamagra oral jelly</a> kamagra oral jelly+ alkohol
      kamagra jelly illegal: https://www.goldkamagra.com kamagra oral jelly

      回复
    • Abwc33n

      cialis 5mg en ligne best kamagra deals
      Aevmv61 <a href=" https://withoutshort.com/ ">generic viagra online</a>
      https://withoutshort.com viagra online

      回复
    • Eamburce

      Regards. Numerous material. <a href=https://www.goldkamagra.com>kamagra 100mg</a> cvs kamagra oral jelly
      se usa kamagra gel: https://www.goldkamagra.com kamagra oral jelly

      回复
    • CharlesHob

      tinder online , what is tinder
      <a href=http://tinderdatingsiteus.com/?>tinder date </a>

      回复
    • AhbzAniny

      payday loans bismarck nd <a href="http://daymoneygo.com/ ">how to register cash loan business in south africa</a> installment payday loan canada

      回复
    • Abcfbopinnots

      generic viagra pro <a href="https://viagriyvik.com/ ">viagra and paypal uk no prescription </a> viagra samples free online

      回复
    • FqbbAniny

      payday loan carmichael <a href="http://ascashapply.com/ ">cash advance long beach ca</a> ocbc singapore cash loan

      回复
    • Jbnvblafe

      payday 2 instant cash shared <a href="http://samcash21.com/ ">cash advance winter garden</a> abc payday loans

      回复
    • Nbnhprest

      cheap generic viagra 100mg <a href="https://vigedon.com/ ">viagra soft tabs uk </a> buy viagra 50mg uk

      回复
    • priligy purchase online canada

      <a href="https://ddapoxetine.com/">priligy purchase online canada</a>

      回复
    • KbbfIdovE

      viagra professional and super active <a href="https://kloviagrli.com/ ">http://kloviagrli.com/ </a> acheter viagra a montreal

      回复
    • Fbsgprest

      cialis online free shipping <a href="https://llecialisjaw.com/ ">http://llecialisjaw.com/ </a> cialis

      回复
    • JbbnAniny

      mix viagra with cialis <a href="http://ljcialishe.com/ ">best generic cialis site</a> quel est le prix du cialis 20mg

      回复
    • Kvaxboop

      cash out equity loan rates <a href="http://rirocash.com/ ">cash advance ft worth</a> consolidate payday loans texas

      回复
    • cialis for daily use canada dftmua

      canadian pharmacies without a prescription <a href="http://edpharmacystore.com/">understanding</a> cialis canadian pharmacy's
      <a href=http://edpharmacystore.com/>sell</a>

      回复
    • IExcuppy

      medicare complete united healthcare - <a href="https://www.withoutbro.com">cialis online</a> Yghcnow
      how much is cialis in uk
      viagra farmacias similares mexico: <a href="https://www.withoutbro.com/">cialis without doctor prescription</a>

      回复
    • LabxAniny

      cialis y sintrom <a href="https://jecialisbn.com/ ">costo cialis en mexico</a> cialis e problemi circolatori

      回复
    • Jbnbblafe

      ED Trial Pack <a href="http://online21rxon.com/ ">best canadian online pharmacies</a> Viagra Soft Tabs

      回复
    • AhkdAniny

      reputable canadian mail order pharmacies <a href="http://storerxpharmcanada.com/ ">canadian online pharmacies prescription drugs</a> Viagra

      回复
    • FqhhAniny

      modafinil online pharmacy <a href="http://happyrxpharmacy21.com/ ">prescription drugs canada</a> canadian prescription drugstore

      回复
    • Aqcfbopinnots

      ebay cialis <a href="http://cialisvja.com/ ">levitra or cialis which is better</a> best ed cialis pills

      回复
    • CharlesHob

      how to use tinder , tinder app
      <a href=http://tinderdatingsiteus.com/?>how to use tinder </a>

      回复
    • Nncsprest

      generic viagra mastercard accepted <a href="http://lightvigra.com/ ">various kinds of viagra</a> buying generic viagra online

      回复
    • how to get a prescription for tadalafil

      <a href="https://cialis.bee-rich.com/">how to get a prescription for tadalafil</a>

      回复
    • KuikIdovE

      buy viagra with discover card <a href="http://viagerkr.com/ ">viagra in dubai</a> where can i get viagra overnight

      回复
    • Kbcxboop

      online rx pharmacy <a href="http://xuypharmacyonline.com/">canadian pharmaceuticals online</a> discount drugs online pharmacy

      回复
    • JbbvAniny

      canada pharmacies online prescriptions <a href="http://canadianpharmnorx.com/ ">canadian drugs pharmacies online</a> compare prescription prices

      回复
    • LbsxAniny

      canada medication pharmacy <a href="https://ssviagriia.com/ ">viagra generic dosage</a> non prescription online pharmacy reviews

      回复
    • free christian dating site for marriage

      free dating sites

      回复
    • generic viagra without subscription

      <A HREF="http://viaaagra.com/">generic viagra without subscription</A>

      回复
    • Tinder dating site

      tider , how to use tinder
      <a href=http://tinderdatingsiteus.com/?>tider </a>

      回复
    • LbgAniny

      where can i buy viagra in canada <a href="http://genqpviag.com/#">viagra</a> paypal to buy viagra http://genqpviag.com/ - order viagra from canada ’

      回复
    • JdbxAniny

      viagra cana <a href="http://llviabest.com/#">price for viagra</a> use paypal to buy viagra http://llviabest.com/ - online exteze and viagra ’

      回复
    • prescription drugs from canada prices huagym

      discount viagra online <a href=http://www.canada1drugstore.com/tadalafil/>viagra buy online</a> cialis in usa pharmacy
      <a href="http://www.canada1drugstore.com/tadalafil/">cialis and pharmacy</a> cialis for sale

      回复
    • tinder site

      tindr , tinder sign up

      回复
    • albuterol sulfate inhalation solution dosage

      <A HREF="https://amstyles.com/">albuterol sulfate inhalation solution dosage</A>

      回复
    • vidalista 20 mg instructions

      <a href="https://vidalista40mg.mlsmalta.com/">vidalista 20 mg instructions</a>

      回复
    • walgreens price for cialis 20mg

      <A HREF="https://wisig.org/">walgreens price for cialis 20mg</A>

      回复
    • can you overdose on dapoxetine

      <A HREF='https://dapoxetine.bee-rich.com/'>can you overdose on dapoxetine</A>

      回复
    • ivermectin for cancer treatment

      <a href="https://ivermectin.mlsmalta.com/">ivermectin for cancer treatment</a>

      回复
    • cost of hydroxychloroquine 200mg

      <a href="https://hydroxychloroquine.mlsmalta.com/">cost of hydroxychloroquine 200mg</a>

      回复
    • sildenafil citrate 20 mg dosage

      <a href="http://droga5.net/">sildenafil citrate 20 mg dosage</a>

      回复
    • ivermectin human dose

      <a href="https://ivermectin.webbfenix.com/">ivermectin human dose</a>

      回复
    • side effects of vidalista cardiac

      <A HREF="https://vidalista.mlsmalta.com/">side effects of vidalista cardiac</A>

      回复
    • careprost at walgreens pharmacy

      <a href="https://carepro1st.com/">careprost at walgreens pharmacy</a>

      回复
    • clomiphene online

      <A HREF=https://salemeds24.wixsite.com/clomid>clomiphene online</A>

      回复
    • is hydroxychloroquine working

      <A HREF=https://hydroxychloroquine.webbfenix.com/>is hydroxychloroquine working</A>

      回复