Use expect to realize screen multi-window boot operation under ubuntu

Under ubuntu, use expect to realize screen multi-window startup operation##

1. The installation and use of expect###

1.1. The installation and use of expect####

What is

expect is a tool for automated control and testing. It mainly interacts automatically with interactive software such as telnet ftp ssh.

how to install####

1.2. Check whether to install

ls /usr/bin |grep expect

Bash

If it does not exist, install it

1.3. installation####

apt-get install expect

Bash

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

Bash

Wiki explains about expect https://zh.wikipedia.org/wiki/Expect

2. Install screen

apt-get install screen

Screen usage is not introduced here too much, please google search

3. Use expect to realize screen multi-window operation###

3.1. Establish start.sh and start running

#! /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" #Create screen
screen -x -S $screen_name1 -p 0-X stuff $'\n' #Carriage return
# Nested expect
# send "\01"Analog input ctrl-a 
# send "d"Analog input d ctrl-a+d is to hang 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

Bash

Test script

sh start.sh

Bash

View screen window

screen -ls

Bash

# Output text, the typeface will start successfully
There is a screen on:168.baidu(06/08/1907:14:04)(Detached)158.google(06/08/1907:14:04)(Detached)1 Socket in/run/screen/S-root.

Bash

3.2 Create stop.sh file

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

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

Bash

3.3 crontab executes these two scripts regularly####

crontab  -e

010***/root/start.sh #10 o&#39;clock every morning
3023***/root/stop.sh #Every night 23.30

Bash

3.4 Boot operation

Set boot enable, only ubuntu18 version

# First check rc-Is local started?,If it is started, it must be restarted completely,The pit here,sandy stepped on it for half an hour
systemctl status rc-local.service

vim /lib/systemd/system/rc-local.service
# Join at the bottom
[ Install]
WantedBy=multi-user.target
Alias=rc-local.service

vim /etc/rc.local
# Add the following code
#! /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 defaultthis script does nothing.
bash /root/start.sh
exit 0

Bash

# Give permission
chmod +x /etc/rc.local
# Open service
systemctl enable rc-local
# Start the service and check the status
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service

# Restart the server
reboot

# Check if the server has been started
sudo systemctl status rc-local.service
# Output text, the typeface will start successfully
Active:active(running) since Fri 2019-06-0703:25:59 UTC; 19s ago
screen -ls
# Output text, the typeface will start successfully
There is a screen on:168.baidu(06/08/1907:14:04)(Detached)158.google(06/08/1907:14:04)(Detached)1 Socket in/run/screen/S-root.

Bash

Liao Liaojun tossing practical experience, does not accept other forwarding, this site has obtained independent authorization! :-)

Recommended Posts

Use expect to realize screen multi-window boot operation under ubuntu
Use mendeley to insert references under ubuntu
Summary of methods to use Evernote under Ubuntu 14.04
Use rsync to remotely backup data under Ubuntu
Use doxygen to generate API documentation under Ubuntu 12.04
Use iptables under ubuntu
Use Ubuntu to boot U disk to crack Windows boot password
Use sublime tex under ubuntu
Use of mediawiki under ubuntu
Use virtualbox to deploy ubuntu
How to realize file sharing between Ubuntu and Windows under VMware
Use QQ under Ubuntu 13.10, suitable for 14.10
How to compile ijkplayer-android under ubuntu
Ubuntu terminal multi-window split screen Terminator
How to install Audacious under Ubuntu
How to install Tensorflow under ubuntu 16.04
How to use Samba server on Ubuntu 16.04
Use python3 to install third on ubuntu
Three ways to install software under Ubuntu
How to change MySQL password under Ubuntu
Realize adding/deleting file permissions under Linux (ubuntu)
windows use ubuntu to start linux service