美文网首页
python3调用shell处理log文件

python3调用shell处理log文件

作者: 小黑佬 | 来源:发表于2020-07-17 11:11 被阅读0次

判断/home目录是否超过80%,如果超过则执行shell脚本,通过pid文件进行判断进程是否正在执行。

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


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_backupxxxx.sh >/dev/null 2>&1")
    os.remove('/tmp/1.pid')
except:
    os.remove('/tmp/1.pid')

scp_log_to_backupxxxx.sh

#!/bin/sh
#set -e
#scp_log_to_backupxxxx.sh
LOCAL_LOGPATH_LIST=("/home/logs/123" "/home/logs/12345" "/home/logs/abc" "/home/logs/a" )
REMOTE_LOGPATH_LIST=("/backup_log/123" "/backup_log/12345" "/backup_log/abc" "/backup_log/a")
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 +180|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调用shell处理log文件

    判断/home目录是否超过80%,如果超过则执行shell脚本,通过pid文件进行判断进程是否正在执行。 scp_...

  • php文件上传

    单文件上传实现: 文件上传代码参考:文件上传 多文件上传实现: 前台调用: shell方式 浏览器方式: 后台处理...

  • shell-11 调用文件,处理文件

    云平台开发运维解决方案@george.sre个人主页:https://geekgoogle.comGitHub: ...

  • Logging 模块配置

    1.新建log.conf 文件 2.调用log.conf文件

  • Jmeter4 BeanShell 使用json校验

    在Jmeter中,使用Bean shell是,内置变量:log:写入信息到jmeber.log文件,可以使用log...

  • shell判断文件

    shell判断文件,目录是否存在或者具有权限 #!/bin/sh myPath="/var/log/httpd/"...

  • mongodb

    shell使用Git Bash Windows 下载 目录和日志 一个文件夹和日志文件(.log后缀)文件夹和文件...

  • Zend Framework 日志系统

    在需要调用的方法中加入如下的调用即可: log的存放位置:在public目录下边的log.txt文件中。

  • 广告ID(GAID)获取方式

    方法一 1.打开open_logcat.bat文件。内容如下:adb shell setprop log.tag ...

  • Hadoop Shell命令

    FS Shell 调用文件系统(FS)Shell命令应使用 bin/hadoop fs 的形式。 所有的的FS ...

网友评论

      本文标题:python3调用shell处理log文件

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