shell脚本-监控python进程是否在运行及启动python进程

shell脚本-监控python进程是否在运行,没有则重启jingdong_prices_run.py 并发送邮件告知重启时间,否则不重启不发送邮件

定时检测京东商品状态

*/5 * * * * sh /data/jingdong-app-spider-py/shell/check-jingdong-goods-run.sh >/store/logs/jingdong-app-spider-py/startpy.log 2>&1

shell监控python进程

check-jingdong-goods-run.sh

#!/bin/bash

echo "开始检查京东商品jingdong_prices_run程序是否运行..."

cd /data/jingdong-app-spider-py/test

processID=$(ps -ef | grep jingdong_prices_run.py | grep -v "grep" | awk "{print $2}")
echo $processID
# 判断$processID为空,此处意思为如果$processID为空,那么重启
if [ -z "$processID" ]
then
    # 启动京东商品jingdong_prices_run程序
    echo "京东商品jingdong_prices_run程序未运行, 重新启动中..."
    #nohup python3 jingdong_prices_run.py >/dev/null &
    nohup python3 jingdong_prices_run.py >/store/logs/jingdong-app-spider-py/nohup.out &
    echo "jingdong_prices_run京东商品重启成功..."
    # 发送邮件 获取当前ip
    ipAdress=$(curl httpbin.org/ip)
    echo $ipAdress
    nohup python3 /data/jingdong-app-spider-py/common/send_email.py "jingdong_prices_run京东商品程序重启成功..." "${ipAdress}"

else
    echo "jingdong_prices_run京东商品程序正在运行中..."

    #nohup python3 jingdong_prices_run.py >/store/logs/jingdong-app-spider-py/jingdong_data/date +%F/startpy.log &
    #echo "jingdong_prices_run京东商品程序重启成功..."
    # 发送邮件
    #ipAdress=$(curl httpbin.org/ip)
    #echo $ipAdress
    #python3 /opt/gh2/app/jingdong-app-spider-py/common/send_email.py "jingdong_prices_run京东商品同步程序重启成功..." "${ipAdress}"




fi





# ps -ef 展示进程
# | grep your_keywords 按关键字筛选
# | grep -v “grep” 屏蔽grep程序本身的进程
# | awk ‘{print $2}’ 只打印第二列
# process_id=${语句} 将语句执行的返回值赋值给process_id,注意等号前后不能有空
# [ -z "$process_id”] 判断$process_id为空
# [[ -z "$process_id”]] 判断$process_id不为空 , 注意这里前后有两个中括号,Shell的这个语法有点奇怪
# kill -9 $process_id 杀掉变量$process_id指定的进程

shell启动python进程

不需要手动 nohup python3 jing_app_run.py >/data/nohup.out/ 2>&1 & 启动,直接运行shell脚本
sh /data/scripts/spider-crawler.sh


#!/bin/bash
PROJECT=`basename $0 | awk -F '_' '{print $1}'`
APP_PATH="/data/app/$PROJECT"
APP_RUN_NAME=`ls $APP_PATH | grep py`
APP_PID=$(ps -ef | grep jing_app_run.py | grep -v grep | awk '{print $2}')
if [ -n "${APP_PID}" ]
then
kill -9 ${APP_PID}
sleep 5
fi
# 启动
cd ${APP_PATH}
nohup python3 ${APP_PATH}/${APP_RUN_NAME} > /data/logs/$PROJECT.out 2>&1 &
posted @ 2020-06-22 20:52  莫贞俊晗  阅读(2324)  评论(0编辑  收藏  举报