shell
#!/bin/bash
pgrep -x mysqld &> /dev/null
if [ $? -ne 0 ]
then
echo “At time: `date` :MySQL is stop .”>> /日志路径
/etc/init.d/mysqld start
else
echo “MySQL server is running .”
fi
#!/bin/bash
?
3、判断/tmp/run目录是否存在,如果不存在就建立,如果存在就删除目录里所有文件
#!/bin/bash
dir=/tmp/run
[ -f $dir ] && mv $dir $dir.bak
[ -d $dir ] && rm -rf $dir/* || mkdir $dir
4、 输入一个文件的绝对路径,判断路径是否存在,而且输出是文件还是目录,如果是字符连接,还得输出是有效的连接还是无效的连接
#!/bin/bash
read -p "Input a path:" path
if [ -L $path -a -e $path ];then
echo "this is effective link"
elif [ -L $path -a ! -e $path ];then
echo "this is not effective link"
elif [ -d $path ];then
echo "this is a director"
elif [ -f $path ];then
echo "this is file"
elif [ -e $path ];then
echo "this is a other type file"
else
echo "the file is not exist"
fi
##5、交互模式要求输入一个ip,然后脚本判断这个IP 对应的主机是否 能ping 通,输出结果类似于:
Server 10.1.1.20 is Down! 最后要求把结果邮件到本地管理员root@localhost和mail01@localhost
方法一:
#!/bin/bash
read -p "输入IP地址:" ip
ping -c 2 $ip > /dev/null 2>&1
if [ $? -eq 0 ];then
echo "Server $ip is OK. " |mail -s ‘check server‘ root@localhost
else
echo "Server $ip is Down!" |mail -s ‘check server‘ root@localhost
fi
方法二:
#!/bin/bash
read -p "Input your ip:" ip
ping -c 1 $ip &>/dev/null
[ $? -eq 0 ] && echo "server $ip is ok"|mail -s "check server" root@localhost || echo "server $ip is down" |mail -s "check server" root@localhost
?
方法三:
#!/bin/bash
tmpfile=`mktemp`
mailaddr="root@localhost mail@localhost"
read -p "输入IP地址:" ip
ping -c 2 $ip > /dev/null 2>&1
if [ $? -eq 0 ];then
echo "Server $ip is Up! " >> $tmpfile
else
echo "Server $ip is Down!" >> $tmpfile
fi
cat $tmpfile
mail -s "ping server" $mailaddr < $tmpfile
rm -rf $tmpfile
?
方法四:
#!/bin/bash
?
rootmail="root@localhost"
tmpfile=`mktemp`
read -p "Input a ip : " ip
?
ping -c 1 "$ip" &>/dev/null
?
retval=$?
?
if [ $retval -eq 0 ];then
echo "Server $ip is up" >