crontab执行脚本的时候,有可能会因为环境变量的问题而不能执行。如手动执行sh /root/aa.sh时,是成功的,在另一台虚拟机也是成功的。但在某一台就没成功。此时,可以添加PATH到aa.sh里。

vi /root/aa.sh

#!/bin/bash

PATH="/usr/local/jdk1.7.0_79//bin:/usr/local/jdk1.7.0_79/bin:JRE_HOME/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"

i1=`nmap -sS 127.0.0.1 -p 8080 | grep $8080 | awk '{printf $2}'`

i2=`nmap -sS 127.0.0.1 -p 8081 | grep $8081 | awk '{printf $2}'`

if [ "$i1" == "closed" ]; then

/home/work/tomcat-background/bin/startup.sh

fi

if [ "$i2" == "closed" ]; then

/home/work/tomcat-zjq-front/bin/startup.sh

fi



调试时可以在脚本里加echo来调试


#!/bin/bash

echo 1111 > /tmp/abc


PATH="/usr/local/jdk1.7.0_79//bin:/usr/local/jdk1.7.0_79/bin:JRE_HOME/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"

i1=`nmap -sS 127.0.0.1 -p 8080 | grep $8080 | awk '{printf $2}'`

i2=`nmap -sS 127.0.0.1 -p 8081 | grep $8081 | awk '{printf $2}'`

if [ "$i1" == "closed" ]; then

/home/work/tomcat-background/bin/startup.sh

fi

if [ "$i2" == "closed" ]; then

/home/work/tomcat-zjq-front/bin/startup.sh

fi