1. 程式人生 > >shell巢狀expect執行screen命令

shell巢狀expect執行screen命令

start.sh

#!/usr/bin/env bash
screen_name1=$"online"
screen -dmS $screen_name1
cmd1=$"php pushOnlineNum.php";
screen -x -S $screen_name1 -p 0 -X stuff "$cmd1" #建立screen
screen -x -S $screen_name1 -p 0 -X stuff $'\n' #回車
/usr/bin/expect <<EOF #巢狀expect
send "\01" #模擬輸入 ctrl-a
send "d" #模擬輸入d  ctrl-a+d是掛起screen
expect eof EOF screen_name2=$"sougou" screen -dmS $screen_name2 cmd2=$"php sougou.php"; 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 screen_name3=$"uc" screen -dmS $screen_name3 cmd3=$"php uc.php"
; screen -x -S $screen_name3 -p 0 -X stuff "$cmd3" screen -x -S $screen_name3 -p 0 -X stuff $'\n' /usr/bin/expect <<EOF send "\01" send "d" expect eof EOF screen_name4=$"dange" screen -dmS $screen_name4 cmd4=$"php dange.php"; screen -x -S $screen_name4 -p 0 -X stuff "$cmd4" screen -x -S $screen_name4
-p 0 -X stuff $'\n' /usr/bin/expect <<EOF send "\01" send "d" expect eof EOF

stop.sh

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

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

screen_name3=$"uc"
screen -X -S $screen_name3 quit

screen_name4=$"dange"
screen -X -S $screen_name4 quit

使用crontab定時執行這兩個指令碼

crontab  -e

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