linux脚本定时任务,使用Linux脚本执行定时任务
#!/bin/sh
#
#cli根目錄
#以#開頭的行是注釋,但注意,第一行#!不是注釋,是指定腳本的解釋文件
clidir=/home/dev/web
#定義了一個名為clidir變量,注意=兩端不能有空格
#啟動短信
function start_sms()
#定義函數(shù)
{
number=`ps -eaf | grep sendsms.sh | grep -vc grep`
#檢測守護(hù)進(jìn)程是否已經(jīng)運(yùn)行
#ps -eaf | grep sendsms.sh | grep -vc grep 統(tǒng)計(jì)包含sendsms.sh字符且不包含grep字符的進(jìn)程有多少條
#grep -vc v 顯示不包含匹配文本的行 c 統(tǒng)計(jì)符合條件的行
#檢測是否已經(jīng)開啟
if [ $number -eq 0 ]; then
#判斷守護(hù)進(jìn)程的數(shù)量是否等于0
# -eq 等于
# 注意:數(shù)值判斷和字符判斷使用的操作符不一樣
# 字符串判斷
# str1 = str2 當(dāng)兩個串有相同內(nèi)容、長度時為真
# str1 != str2 當(dāng)串str1和str2不等時為真
# -n str1 當(dāng)串的長度大于0時為真(串非空)
# -z str1 當(dāng)串的長度為0時為真(空串)
# str1 當(dāng)串str1為非空時為真
# 數(shù)字的判斷
# int1 -eq int2 兩數(shù)相等為真
# int1 -ne int2 兩數(shù)不等為真
# int1 -gt int2 int1大于int2為真
# int1 -ge int2 int1大于等于int2為真
# int1 -lt int2 int1小于int2為真
# int1 -le int2 int1小于等于int2為真
# 文件的判斷
# -r file 用戶可讀為真
# -w file 用戶可寫為真
# -x file 用戶可執(zhí)行為真
# -f file 文件為正規(guī)文件為真
# -d file 文件為目錄為真
# -c file 文件為字符特殊文件為真
# -b file 文件為塊特殊文件為真
# -s file 文件大小非0時為真
# -t file 當(dāng)文件描述符(默認(rèn)為1)指定的設(shè)備為終端時為真
# 邏輯判斷
# -a 與
# -o 或
# ! 非
/bin/sh ${clidir}/cli/queue/sendsms.sh &
#啟動守護(hù)進(jìn)程
# & 符號 表示在后臺運(yùn)行
echo 'service queue to sendsms start running'
else
echo 'service queue to sendsms already running'
fi
return $?;
}
#停止短信
function stop_sms()
{
number=`ps -eaf | grep sendsms.sh | grep -vc grep`
#檢測守護(hù)進(jìn)程是否已經(jīng)關(guān)閉
if [ $number -eq 0 ]; then
echo 'service queue to sendsms already stopped'
else
#關(guān)閉守護(hù)進(jìn)程
for pid in $(ps -ef | grep sendsms.sh | grep -v grep | awk '{print $2}'); do
#ps -ef | grep sendsms.sh | grep -v grep | awk '{print $2}' 逐行讀取包含sendsms.sh且不包含grep的進(jìn)程id
#awk 逐行讀取文本,并以空格(默認(rèn))分割每行,$1表示分割出的第一個元素,$2表示第二個... $0表示所有元素
kill -9 $pid &> /dev/null
#kill -9 $pid 殺死指定進(jìn)程id的進(jìn)程
done
echo 'service queue to sendsms has stopped'
fi
pid_sms=`ps -ef | grep sendsmsAction | grep -vc grep`
if [ $pid_sms -gt 0 ]; then
kill -9 $(ps -ef | grep sendsmsAction | grep -v grep } awk '{print $2}') &> /dev/null
fi
return $?
}
#查看狀態(tài)
function status()
{
echo "sendsms :" `ps -ef | grep sendsmsAction | grep -v grep | awk '{print $2 , $8, $9, $10, $11}'`
echo "sendemail :" `ps -ef | grep sendemailAction | grep -v grep | awk '{print $2 , $8, $9, $10, $11}'`
return $?
}
#查看守護(hù)進(jìn)程狀態(tài)
function sh_status()
{
echo -e "sendsms :" `ps -eaf | grep sendsms.sh | grep -v grep | awk '{print $2 , $8, $9, $10, $11}'`
echo "sendemail :" `ps -eaf | grep sendemail.sh | grep -v grep | awk '{print $2 , $8, $9, $10, $11}'`
return $?
}
case "$1" in
start)
if [[ "$2" == "sendsms" ]]; then
start_sms
elif [[ "$2" == "sendemail" ]]; then
start_email
else
start_sms
start_email
fi
;;
stop)
if [[ "$2" == "sendsms" ]]; then
stop_sms
elif [[ "$2" == "sendemail" ]]; then
stop_email
else
stop_sms
stop_email
fi
;;
shstatus)
sh_status
;;
status)
status
;;
*)
echo $"Usage: $0 {start|stop|status|shstatus}"
exit 2
esac
exit $?
2.[代碼]守護(hù)腳本#!/bin/bash
#守護(hù)進(jìn)程腳本
#守護(hù)腳本比較簡單 每隔一段時間執(zhí)行一次任務(wù) 每次執(zhí)行前 檢測是否有還在執(zhí)行的任務(wù)
selfpath=`dirname $0`
#$0 特殊變量 文件路徑
cd $selfpath;
cd ../../
phpExec=/opt/app/php5/bin/php
while [[ true ]]; do
number=`ps -ef | grep 'sendsmsAction' | grep -vc grep`
if [ $number -eq 0 ]; then
#每次執(zhí)行任務(wù) 判斷是否還有正在執(zhí)行的任務(wù)
$phpExec cli.php queue sendsmsAction &
fi
sleep 1m
#間隔時間
done
聲明:本文原創(chuàng)發(fā)布php中文網(wǎng),轉(zhuǎn)載請注明出處,感謝您的尊重!如有疑問,請聯(lián)系admin@php.cn處理
總結(jié)
以上是生活随笔為你收集整理的linux脚本定时任务,使用Linux脚本执行定时任务的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux脚本日期时间,Linux 日期
- 下一篇: linux查看java运行日志,Linu