python3 写入pidfile文件

#!/usr/bin/python3
import os
import time
import sys
import psutil

#check_logs_pid.py
def write_pid_to_pidfile(pidfile_path):
    """ Write the PID in the named PID file.

        Get the numeric process ID (“PID”) of the current process
        and write it to the named file as a line of text.

        """
    open_flags = (os.O_CREAT | os.O_EXCL | os.O_WRONLY)
    open_mode = 0o644
    pidfile_fd = os.open(pidfile_path, open_flags, open_mode)
    pidfile = os.fdopen(pidfile_fd, 'w')

    # According to the FHS 2.3 section on PID files in /var/run:
    #
    #   The file must consist of the process identifier in
    #   ASCII-encoded decimal, followed by a newline character. For
    #   example, if crond was process number 25, /var/run/crond.pid
    #   would contain three characters: two, five, and newline.

    pid = os.getpid()
    pidfile.write("%s\n" % pid)
    pidfile.close() 



#
def check_pid_file(pidfile_path):
    if not os.path.exists(pidfile_path):
        return True
    else:
        print("文件已经存在,退出")
        os._exit(0)

try:
    check_pid_file('/tmp/1.pid')
    write_pid_to_pidfile('/tmp/1.pid')
    disk_usage=psutil.disk_usage('/home')
    disk_usage=int(disk_usage.percent)
    if disk_usage > 80:
        status = os.system("/bin/sh /opt/shell/scp_log_to_backupserver_mminxxx.sh >/dev/null 2>&1")
    os.remove('/tmp/1.pid')
except:
    os.remove('/tmp/1.pid')

scp_log_to_backupserver_mminxxx.sh

#!/bin/sh
#日志打包并传输到BACKUP_IP服务器,然后删除源文件和源打包文件
#set -e
LOCAL_LOGPATH_LIST=("/home/linux/logs/abc/1" "/home/linux/logs/abc/2" "/home/linux/logs/abc/3")
REMOTE_LOGPATH_LIST=("/backup_log/1_abc/1" "/backup_log/1_abc/2" "/backup_log/1_abc/3")
BACKUP_IP=192.168.1.100


LEN_LOCAL_LIST=${#LOCAL_LOGPATH_LIST[@]}

for ((i=0; i<LEN_LOCAL_LIST; i++))
do
   local_path=${LOCAL_LOGPATH_LIST[$i]}
   remote_path=${REMOTE_LOGPATH_LIST[$i]}
   ssh root@${BACKUP_IP} "[ -d ${remote_path} ]" >/dev/null 2>&1
   if [ $? != 0 ]
   then
       ssh root@${BACKUP_IP}  "mkdir -p ${remote_path}"
   fi
   cd ${local_path}
   for file_name in `find ./ -mmin +360|awk -F '/' '{print $2}'`
     do
         HIS_DATE=`echo ${file_name}|awk -F '_' '{print $2}'`
         tar -zcf ${HIS_DATE}.tar.gz ${file_name}
         scp ${HIS_DATE}.tar.gz root@${BACKUP_IP}:${remote_path}
         rm -f ${HIS_DATE}.tar.gz ${file_name}
     done
done
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

少陽君

谢谢老板的拿铁

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值