脚本解析头:

#!/bin/sh

检测进程列表中存在的特定进程名的个数:

Proc_NUM=`ps aux | grep 'proc_name' | grep -v grep |wc -l`
# echo $Proc_NUM
# 条件状态1:少于1
if [ "${Proc_NUM}" -lt "1" ];then
# Do your task here, like restart it.
# Add task here...

# 条件状态2:大于1
elif [ "${Proc_NUM}" -gt "1" ];then
# Do your task here, like kill it in force or reboot.
# Add task here...

fi

循环执行任务,条件状态下可使用 sleep 实现定时效果:

while true
#循环运行
do
#Do your task here, example check the net status...
ping -c 3 www.baidu.com > /dev/null
#-c 3 是指ping执行3次后结束。
#-w 3 是指ping执行3秒后结束。
#$?是用来获取函数返回值或者上一个命令的退出状态。

if [ $? -eq 0 ];then
#条件状态1,任务执行成功, like: the net is ok.
#Do the task in the status1.
#Add your task here..

#任务执行完成后可通过 sleep 命令实现定时的效果。
else
#条件状态2:任务执行失败,like: the net is not ok.
#Do the task in the status1.
#Add your task here..

#任务执行完成后可通过 sleep 命令实现定时的效果。
fi
done

各种服务检测及配置:


#Start the ftp if it was colsed.
ps -fe|grep vsftpd|grep -v grep
if [ $? -ne 0 ] ; then
/mnt/platform/sbin/vsftpd /etc/vsftpd.conf
fi

#Open the wifi hotspot.
ifconfig wlan0 192.168.9.1    #Set route IP.
hostapd -B /etc/hostapd/hostapd.conf    #Configure the client IP range.
udhcpd /etc/udhcpd/udhcpd_wlan0.conf &    #Automatic distribute IP.

获取网络时间并同步系统时间1(待实测):

wget -q http://time.tianqi.com/beijing
 
time_ch=$(cat ./beijing |grep “当地时间” |awk -F ">" '{print $5}' |awk -F "<" '{print $1}' )
year=$(echo $time_ch |awk -F "年" '{print $1}' )
month=$(echo $time_ch |awk -F "年" '{print $2}' |awk -F "月" '{print $1}' )
day=$(echo $time_ch |awk -F "月" '{print $2}'|awk -F "日" '{print $1}')
time_now=$(echo $time_ch |awk '{print $3}')
time_a=$(echo "$year-$month-$day $time_now")

date -s "$time_a"

rm ./beijing

获取网络时间并同步本地时间2(实测有效):

ntpdate ntp.ntsc.ac.cn [n_hour] &
说明:[n_hour]根据不同的时区进行配置,国内常用的是 0 或 8,需要根据实际情况确定。
其中 ntpdate 需要根据实际的平台下载源码进行编译。
同步阿里时间服务器:ntpdate -u ntp.aliyun.com

标签: shell, grep, 实用, task, your, Do, here, 汇集, like

相关文章推荐

添加新评论,含*的栏目为必填