美文网首页
Shell PID File

Shell PID File

作者: flycloud_hz | 来源:发表于2022-04-01 11:40 被阅读0次

问题

Linux cron配置定时任务启动脚本,如果运行时间覆盖cron下次的时间,需要做进程检测,否则会有多个脚本运行。

解决方法

shell脚本运行时创建pid.file

#!/bin/bash

set -e

PIDFILE=/var/run/test.pid

if [ -f $PIDFILE ]
then
  PID=$(cat $PIDFILE)
  ps -p $PID > /dev/null 2>&1
  if [ $? -eq 0 ]
  then
    echo "Process already running"
    exit 1
  else
    ## Process not found assume not running
    echo $$ > $PIDFILE
    if [ $? -ne 0 ]
    then
      echo "Could not create PID file2"
      exit 1
    fi
  fi
else
  echo $$ > $PIDFILE
  if [ $? -ne 0 ]
  then
    echo "Could not create PID file"
    exit 1
  fi
fi

echo "sleep 10"
sleep 10
echo "sleep 10 done"

rm $PIDFILE

相关文章

网友评论

      本文标题:Shell PID File

      本文链接:https://www.haomeiwen.com/subject/oehajrtx.html