美文网首页
python3 写入pidfile文件

python3 写入pidfile文件

作者: 小黑佬 | 来源:发表于2020-09-08 10:35 被阅读0次
    #!/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
    
    

    相关文章

      网友评论

          本文标题:python3 写入pidfile文件

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