python3调用shell文件_python3调用shell处理log文件

判断/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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值