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


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

1. expect的安装与使用

1.1. expect的安装与使用

是什么

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

如何安装

1.2. 检测是否安装

ls /usr/bin |grep expect

如果不存在,则进行安装

1.3.安装

apt-get install tcl tk 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 * * * bash /root/start.sh #每天早晨10点
30 23 * * * bash /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.

声明:Sandy'Blog|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - ubuntu下利用expect实现screen多窗口开机运行


一流的吃貨,二流的廚藝,三流的技術,下流的人品