有时候需要一些python脚本去常链接一些东西,就需要监控是否运行正常。
#!/bin/bash
project1='/root/1.py'
project2='/root/2.py'
for Pro in $project1 $project2
do
PythonPid=`ps -ef | grep $Pro | grep -v grep | wc -l `
echo $Pro
if [ $PythonPid -eq 0 ];
then
echo "`date "+%Y-%m-%d %H:%M:%S"`:$Pro is not running" >> /data/logs/python.log
cd /data
nohup python $Pro >output 2>&1 &
echo "`date "+%Y-%m-%d %H:%M:%S"`:$Pro is starting" >> /data/logs/python.log
sleep 5
CurrentPythonPid=`ps -ef | grep $Pro | grep -v grep | wc -l`
if [ $CurrentPythonPid -ne 0 ];
then
echo "`date "+%Y-%m-%d %H:%M:%S"`:$Pro is running" >> /data/logs/python.log
fi
fi
done