expect is a tool for automated control and testing. It mainly interacts automatically with interactive software such as telnet ftp ssh.
ls /usr/bin |grep expect
Bash
If it does not exist, install it
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
apt-get install screen
Screen usage is not introduced here too much, please google search
#! /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
#! /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
crontab -e
010***/root/start.sh #10 o'clock every morning
3023***/root/stop.sh #Every night 23.30
Bash
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