export与source(附带多个服务器一起启动服务器脚本)
linux环境中,
A=1这种命名方式,变量作用域为当前线程
export命令出的变量作用域是当前进程及其子进程。
可以通过source 脚本,将脚本里面的变量放在当前进程中
附带自己写的tomcat启动脚本
#!/bin/bash
cat <<EOF >startupTomcat.sh
ck=\`ps -ef | grep tomcat1 | grep java | wc -l\`
if [ \$ck = 0 ]; then /home/wdj/tomcat1/bin/startup.sh
exit 1
fi
EOF
#ֱ直接执行该命令是不行的,因为tomcat启动需要JAVA_HOME变量,A=1这种方式,变量作用域为当前线程,export命令出的变量作用域是当前进程及其子进程。source 脚本,将里面的变量放在当前进程中
cat <<EOF >shutdownTomcat.sh
ck=\`ps -ef | grep tomcat1 | grep java | wc -l\`
if [ \$ck = 1 ]; then /home/wdj/tomcat1/bin/shutdown.sh
exit 1
fi
EOF
if [ "$1" = "0" ]
then SHELL=shutdownTomcat.sh
elif [ "$1" = "1" ]
then SHELL=startupTomcat.sh
else
echo "you must input param 0 or 1"
exit 1
fi
SERVERS="192.168.10.150 192.168.10.151"
for SERVER in $SERVERS
do
/usr/bin/expect <<-EOF
set timeout -1
spawn scp $SHELL $SERVER:/
expect {
spawn scp $SHELL $SERVER:/
expect {
"*yes/no*" {send "yes\r"}
"*password*" {send "1234567\r"}
}
spawn ssh $SERVER "source /etc/profile;chmod +x /$SHELL;/$SHELL;rm -rf /$SHELL"
expect {
"*yes/no*" {send "yes\r"}
"*password*" {send "1234567\r"}
}
sleep 1
EOF
done

浙公网安备 33010602011771号