CentOS 不间断会话(ssh关闭后如何保证程序继续运行)(nohup和screen)
当使用ssh与远程主机的会话被关闭时,在远程主机上运行的命令也随之被中断。
就是ssh 打开以后,bash等都是他的子程序,一旦ssh关闭,系统将所有相关进程杀掉!! 导致一旦ssh关闭,执行中的任务就取消了。
守护进程不受此影响, 因为守护进程比较特殊, 不属于sshd这个进程组 而是单独的进程组,所以就算关闭了ssh,和他也没有任何关系。
解决办法:
1、使用nohup命令来运行程序
[es@localhost ~]$ nohup /opt/elasticsearch-6.5./bin/elasticsearch
nohup: 忽略输入并把输出追加到"nohup.out" 或者
[es@localhost ~]$ nohup /opt/elasticsearch-6.5./bin/elasticsearch &
[]
[es@localhost ~]$ nohup: 忽略输入并把输出追加到"nohup.out"
#使用tail -f来查看输出信息
[es@localhost ~]$ tail -f nohup.out
[--21T11::,][INFO ][o.w.a.d.Monitor ]
。。。 。。。
nohup命令比较简单,只能做到把程序放入后台运行,并且ssh关闭后不中断,无法实施查看运行情况(除了看nohup.out),无法进行人机交互操作。
如果要实现这些,中断ssh后,重新连上还能继续操作,则需要screen这个软件。
2、screen 是一款能够实现多窗口远程控制的开源服务程序,简单来说就是为了解决网络异常中断或为了同时控制多个远程终端窗口而设计的程序。
用户还可以使用 screen 服务程序同时在多个远程会话中自由切换和共享会话。
2.1、安装
检查一下REPO里cdrom的配置
[root@localhost yum.repos.d]# cat CentOS-Media.repo
# CentOS-Media.repo
#
# This repo can be used with mounted DVD media, verify the mount point for
# CentOS-. You can use this repo and yum to install items directly off the
# DVD ISO that we release.
#
# To use this repo, put in your DVD and use it with the other repos too:
# yum --enablerepo=c7-media [command]
#
# or for ONLY the media repo, do this:
#
# yum --disablerepo=\* --enablerepo=c7-media [command] [c7-media]
name=CentOS-$releasever - Media
baseurl=file:///media/CentOS/
file:///media/cdrom/
file:///media/cdrecorder/
gpgcheck=
enabled=
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
挂载centos7镜像来安装
[root@localhost yum.repos.d]# mkdir -p /media/cdrom [root@localhost yum.repos.d]# mount /dev/cdrom /media/cdrom
mount: /dev/sr0 写保护,将以只读方式挂载 [root@localhost yum.repos.d]# yum install screen 已安装:
screen.x86_64 :4.1.-0.25.20120314git3c2946.el7 完毕!
[root@localhost yum.repos.d]# screen --help
2.2、恢复session
[root@localhost ~]# screen -ls
No Sockets found in /var/run/screen/S-root. #创建一个session,名字es,屏幕一闪,实际已经进入screen
[root@localhost ~]# screen -S es [root@localhost ~]# screen -ls
There is a screen on:
.es (Attached)
Socket in /var/run/screen/S-root.
#执行ping,然后直接关闭ssh窗口
[root@localhost ~]# ping 192.168.1.103
PING 192.168.1.103 (192.168.1.103) () bytes of data.
bytes from 192.168.1.103: icmp_seq= ttl= time=0.648 ms
#重新登录ssh,使用screen -ls查看,有session是detached
[root@localhost ~]# screen -ls
There is a screen on:
.es (Detached)
Socket in /var/run/screen/S-root.
#恢复窗口,ping一直在运行
[root@localhost ~]# screen -r .es ... ...
bytes from 192.168.1.103: icmp_seq= ttl= time=0.904 ms
bytes from 192.168.1.103: icmp_seq= ttl= time=1.07 ms
bytes from 192.168.1.103: icmp_seq= ttl= time=0.773 ms
bytes from 192.168.1.103: icmp_seq= ttl= time=1.26 ms
^C
--- 192.168.1.103 ping statistics ---
packets transmitted, received, % packet loss, time 85134ms
rtt min/avg/max/mdev = 0.467/1.051/4.810/0.535 ms 打exit退出screen
[screen is terminating]
也可以不手动创建session,直接使用screen 命令执行要运行的命令,这样在命令中的一切操作也都会被记录下来,当命令执行结束后 screen 会话也会自动结束。
这种情况下,session名字是系统自动创建的。
[es@localhost ~]$ screen /opt/elasticsearch-6.5./bin/elasticsearch 断开ssh后,重新查看
[es@localhost ~]$ screen -ls
There is a screen on:
.pts-.localhost (Detached)
Socket in /var/run/screen/S-es. 用kill命令杀掉elasticsearch后,screen自动终止
2.3、共享session,可以多个终端连入同一个session,所有操作在连入的终端上都可以显示,所有终端都可以操作。
[root@localhost network-scripts]# screen -ls
There are screens on:
.ping3 (Detached)
.myping (Detached)
Sockets in /var/run/screen/S-root. [root@localhost network-scripts]# screen -x ping3
CentOS 不间断会话(ssh关闭后如何保证程序继续运行)(nohup和screen)的更多相关文章
- win7程序关闭后弹出 程序兼容性助手 这个程序可能安装不正确 如果此程序没有正确安装,处理方式
用WTL编写的程序,编译成release后,在win7上关闭后,弹出这个对话框 处理方法: 将这个xml文件命名成 xxx.exe.manifest,保存到指定目录下 <?xml version ...
- chrome关闭后还在进程中运行
1.网上搜到信息: 设置 “即使关闭浏览器也后台运行” 取消打勾 2.然后我找了一下,应该是这个选项:“关闭 Google Chrome 后继续运行后台应用” 3. 4. 5.
- 如何实现SSH断开后 进程仍然在后台运行
1.nohup命令功能:不挂断地运行命令,忽略HUP信号.语法:nohup command & 实例:nohup ping www.google.com & 转自: http://bl ...
- linux后台运行程序(secureCRT断掉或关闭后继续运行程序)
secureCRT断掉后想继续运行程序.也就是程序会在后台一直执行到结束. secureCRT通过ssh联接到client端运行程序,如果关掉窗口或者断开连接时,任务会被kill. 为了程序能够后台执 ...
- CentOS 6.0修改ssh远程连接端口
转自:系统运维 » CentOS 6.0修改ssh远程连接端口 实现目的:把ssh默认远程连接端口修改为2222 方法如下: 1.编辑防火墙配置:vi /etc/sysconfig/iptables ...
- centos 6.3安装ssh
centos 6.3安装ssh 安装ssh服务器端软件 yum install openssh-server 安装ssh客户端软件 yum install openssh-clients ...
- Java Web 浏览器关闭后Session就会被销毁吗?
浏览器关闭后Session就会被销毁吗? Session是JSP的九大内置对象(也称为隐含对象)中的一个,用于保存当前用户的状态信息,初学者可能认为Session的生命周期是从打开一个浏览器发送请求到 ...
- linux ------ 使用 screen 后 SSH 断开后程序依旧能在后台运行
为什么ssh断开后你运行的进程会退出呢? 因为所有进程都得有个父进程.当你ssh到一个服务器上时,打开的shell就是你所有执行命令的父进程. 当你断开ssh连接时,你的命令的父进程就没了.如果处理不 ...
- 微信WebView关闭后本地cookie无法清除问题
问题背景 在微信WebView下的页面中登录后,关闭WebView返回后再次进入页面,发现登录态还存在,原因是微信不会主动清除cookie以及其他的缓存. 期望是关闭窗口后会清除cookie,重新进入 ...
随机推荐
- 2 进程multiprocessing [mʌltɪ'prəʊsesɪŋ] time模块
1.multiprocessing模块 multiprocessing模块就是跨平台版本的多进程模块. multiprocessing模块提供了一个Process类来代表一个进程对象, 2.Proce ...
- Wireshark对HTTPS数据的解密
本文来自网易云社区 之前有介绍<wireshark抓包分析--TCP/IP协议>,然后某天有人问我,示例里是HTTP的,如果是HTTPS,你可以抓包分析吗?基于好奇,我查阅了下相关资料,把 ...
- MySQL连接本地数据库时报1045错误的解决方法
navicat for MySQL 连接本地数据库出现1045错误 如下图: 说明连接mysql时数据库密码错误,需要修改密码后才可解决问题: 解决步骤如下: .首先打开命令行:开始->运行 ...
- centos7下搭建django
安装环境:centos7.4 1 安装nginx yum install nginx 注:尝试过在本地和腾讯云上安装,使用同一条命令:在本地安装提示没有可用安装包,云上安装正常 启动nginx,并启用 ...
- 使用gitlab时候 fork仓库不会实时从主仓库更新解决方案
付费用户可以使用现成的方案,地址见 链接 但是私有gitlab时候,需要手动进行如下操作 1. Clone your fork: git clone git@github.com:YOUR-USERN ...
- MaxScript代码补全插件
MaxScript代码补全插件 作者Nik,原文发布于ScriptSpot 安装后max自带脚本编辑器会有自动补全,效果如下:
- 【WXS数据类型】Boolean
属性: 名称 值类型 说明 [Boolean].constructor [String] 返回值为“Boolean”,表示类型的结构字符串 方法: 原型:[Boolean].toString() 说明 ...
- Liunx 基本命令
find : find ./ -name "*instantiate_post_check.yml*" grep: openstack network show fe92bfcf- ...
- Halcon和visionPro的比较
很多朋友会问到visionpro和halcon这两款机器视觉软件,到底学哪个好呢,今天重码网就给大家讲一讲: 首先比较下两者的优缺点: halcon: 提供的图像算法要比Visionpro多,也就是说 ...
- SGU 326 Perspective(最大流)
Description Breaking news! A Russian billionaire has bought a yet undisclosed NBA team. He's plannin ...