#!/usr/bin/env python # -*- coding: utf-8 -*- import time import subprocess timeSleep = 15 def run_command(command, bg=False): if bg: command += ' &' process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) output, error = process.communicate() return output.decode(), error.decode() # 定时执行进程 def process(): command = 'php /www/wwwroot/reapi/public/index.php gateway/api.task/cron 1>/dev/null 2>&1 &' check = 'ps -ef | grep gateway/api.task/cron | grep -v grep | wc -l' while True: value = int(run_command(check)[0].strip()) if value == 0: run_command(command) time.sleep(timeSleep) process()