小沨的天空

  • 首页
  • Code
  • Dns
  • 图床
  • 店铺
私人小天地
谈天说地,学习笔记
  1. 首页
  2. Linux
  3. 正文

ubuntu下利用expect实现screen多窗口开机运行

2019年7月31日 2311点热度 0人点赞 1条评论

ubuntu下利用expect实现screen多窗口开机运行

1. expect的安装与使用

1.1. expect的安装与使用

是什么

expect 是用来进行自动化控制和测试的工具。主要是和交互式软件telnet ftp ssh 等进行自动化的交互。

如何安装

1.2. 检测是否安装

ls /usr/bin |grep expect

如果不存在,则进行安装

1.3.安装

apt-get install expect
ls /usr/bin |grep expect
autoexpect
expect
expect_autoexpect
expect_autopasswd
expect_cryptdir
expect_decryptdir
expect_dislocate
expect_ftp-rfc
expect_kibitz
expect_lpunlock
expect_mkpasswd
expect_multixterm
expect_passmass
expect_rftp
expect_rlogin-cwd
expect_timed-read
expect_timed-run
expect_tknewsbiff
expect_tkpasswd
expect_unbuffer
expect_weather
expect_xkibitz
expect_xpstat

wiki对于expect说明https://zh.wikipedia.org/wiki/Expect

2. 安装screen

apt-get install screen

screen用法这里不做过多的介绍,请大家google搜索

3. 利用expect实现screen多窗口运行

3.1. 建立start.sh开机运行

#!/usr/bin/env bash
screen_name1=$"baidu"
screen -dmS $screen_name1
cmd1=$"ping www.baidu.com";
screen -x -S $screen_name1 -p 0 -X stuff "$cmd1" #创建screen
screen -x -S $screen_name1 -p 0 -X stuff $'\n' #回车
# 嵌套expect
# send "\01" 模拟输入 ctrl-a 
# send "d" 模拟输入d  ctrl-a+d是挂起screen
/usr/bin/expect <<EOF
send "\01"
send "d"
expect eof
EOF

screen_name2=$"google"
screen -dmS $screen_name2
cmd2=$"ping www.baidu.com";
screen -x -S $screen_name2 -p 0 -X stuff "$cmd2"
screen -x -S $screen_name2 -p 0 -X stuff $'\n'
/usr/bin/expect <<EOF
send "\01"
send "d"
expect eof
EOF

测试脚本

sh start.sh

查看screen窗口

screen -ls
#输出文字,字样就成功启动
There is a screen on:
    168.baidu   (06/08/19 07:14:04) (Detached)
    158.google  (06/08/19 07:14:04) (Detached)
1 Socket in /run/screen/S-root.

3.2 创建stop.sh文件

#!/usr/bin/env bash
screen_name1=$"baidu"
screen -X -S $screen_name1 quit #退出screen

screen_name2=$"google"
screen -X -S $screen_name2 quit

3.3 crontab定时执行这两个脚本

crontab  -e

0 10 * * * /root/start.sh #每天早晨10点
30 23 * * * /root/stop.sh #每天晚上23.30

3.4 开机运行

设置开机启用,仅限ubuntu18版本

#首先检查rc-local是不是启动了,如果启动最后完全还要restart,这里的坑,sandy踩了半个小时
systemctl status rc-local.service

vim /lib/systemd/system/rc-local.service
#最下加入
[Install]
WantedBy=multi-user.target
Alias=rc-local.service

vim /etc/rc.local
#加入下面代码
#!/bin/bash
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
bash /root/start.sh
exit 0
#给予权限
chmod +x /etc/rc.local
#开启服务
systemctl enable rc-local
#启动服务并检查状态
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service

#重新启动服务器
reboot

#检测服务器是不是已经启动
sudo systemctl status rc-local.service
#输出文字,字样就成功启动
Active: active (running) since Fri 2019-06-07 03:25:59 UTC; 19s ago
screen -ls
#输出文字,字样就成功启动
There is a screen on:
    168.baidu   (06/08/19 07:14:04) (Detached)
    158.google  (06/08/19 07:14:04) (Detached)
1 Socket in /run/screen/S-root.

寥寥君折腾实践经验所得,不接受其他转发,本站已经取得独立授权!:-)

标签: expect screen ubuntu 多窗口
最后更新:2019年7月31日

mikj

这个人很懒,什么都没留下

打赏 点赞
< 上一篇
下一篇 >

文章评论

  • repostone

    非技术的路过。

    2019年8月23日
    登录以回复
  • 您需要 登录 之后才可以评论
    小程序
    广告
    最新 热点 随机
    最新 热点 随机
    实践Google Voice号码转移到Ultra Mobile Paygo套餐的实体sim卡 Giffgaff英国+44手机SIM卡免费领取 Server酱和Qmsg酱两个好用的推送服务 linux的ssh服务升级后无法启动修复 linux服务器的硬盘使用时间查询 启用chrome浏览器内置的二维码生成插件 宝塔面板或者LNMP下安装php的mongodb扩展 ShareX 对接tu.my图床实现截图和图片自动上传 帝国CMS的sitemap.xml设置和生成 hetzner服务器购买和安装debian系统
    Giffgaff英国+44手机SIM卡免费领取实践Google Voice号码转移到Ultra Mobile Paygo套餐的实体sim卡
    用SSH(Shell)上传文件到远程服务器的方法(转香草吧噗) GoIndex谷歌网盘神器,结合CloudFlare Workers CentOS虚拟机添加单个IP和批量添加多个IP地方法 使用Google Drive定时备份WordPress和国外VPS挂载 利用VPS配置搭建frp/frps内网穿透/转寥寥 通过Teredo快速设置Windows ipv6,连接只有ipv6 ip的VPS CentOS5 升级php和mysql到最新版本转 win系统下ERR_CONNECTION_RESET解决尝试 CentOS 5.3 下快速安装配置 PPTP VPN 服务器 转张宴BLOG crontab编辑器选错后的解决方法
    标签聚合
    升级 系统 vps centos php wordpress linux DirectAdmin 网盘 服务器 免费 ubuntu
    开源相关
    • fedoraproject
    • owncloud
    • CentOS mirrors
    • linode mirrors
    • centos vault
    • RepoForge
    • elrepo.org
    • do mirrors
    • Ubuntu Mirrors
    • Debian Mirror
    • Packages Search
    • 阿里开源镜像站
    友情链接
    • vzone
    • logdns.free.fr
    • 豆博
    • 寥寥后花园
    • 麦麦同学
    • 2.int.ru
    • 我喔喔喔
    • semnew
    • QQTM论坛
    • LiCEO
    • 娃娃博客
    • 撸羊毛
    • 夜狐
    • 天下无鱼
    • 李子博客
    • 轨迹博客
    • 回到未来博客
    • xiaofengsky.free.fr
    • QQPCC
    • 爱购啦
    • 我的图库
    • 陈否否
    • 傻子-跸西的blog
    • 死老鬼
    • 朱志瑞
    • 小沨记事本
    • 午夜客
    • 怪鸟博客

    COPYRIGHT © 2020 小沨的天空. ALL RIGHTS RESERVED. @ 网站运行: @ 服务器运行状态

    THEME KRATOS MADE BY VTROIS