经过一段时间的执行时间后,该进程被终止

时间:2014-12-21 13:26:03

标签: python python-2.7

crontab.py

# -*- coding: utf-8 -*-
import time
import daemon
import logging
import os, sys
import traceback
import commands, re

sys.path.insert(0, '/usr/local/qs-project/prometheus/')
# set default setting file, change app global config
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "prometheus.settings")
from django.conf import settings

log = logging.getLogger('daemon')
hdlr = logging.FileHandler(os.path.join(settings.BASE_DIR, "logs/exec_task.log"))
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
log.addHandler(hdlr)
log.setLevel(logging.DEBUG)

from schedule.task.crontab.check_in import main as check_in_main
from schedule.task.crontab.check_out import main as check_out_main
from schedule.task.crontab.operate_dns import sync, delete
from schedule.task.crontab.cluster_site_manage import main as cluster_site_main
from schedule.task.crontab.update_redis import main as update_redis_main

def main(): 
    #daemon.daemonize(noClose=True)
    update_redis_count = 0
    scheduler_count = 0
    while True:
        try:
            update_redis_count += 1
            btime = time.time()
            # sync dns
            log.info("Exec sync start")
            sync()
            log.info("Exec sync end")
            etime = time.time()
            log.info("Exec sync Use time: %f", etime - btime)

            btime = time.time()
            # delete dns
            log.info("Exec delete start")
            delete()
            log.info("info............." + str(process_info()))
            log.info("Exec delete end")
            etime = time.time()
            log.info("Exec delete Use time: %f", etime - btime)

            btime = time.time()
            # cluster
            log.info("Exec cluster site main start")
            cluster_site_main()
            log.info("info............." + str(process_info()))
            log.info("Exec cluster site main end")
            etime = time.time()
            log.info("Exec cluster_site_main Use time: %f", etime - btime)
           if update_redis_count == 4:
                scheduler_count += 1
                update_redis_count = 0
                btime = time.time()
                log.info("Exec update redis start")
                update_redis_main()
                log.info("info............." + str(process_info()))
                log.info("Exec update redis end")
                etime = time.time()
                log.info("Exec update_redis_main Use time: %f", etime - btime)


            if scheduler_count == 2:
                scheduler_count = 0
                btime = time.time()
                # check in
                log.info("Exec checkin start")
                check_in_main()
                log.info("info............." + str(process_info()))
                log.info("Exec checkin end")
                etime = time.time()
                log.info("Exec check_in_main Use time: %f", etime - btime)
                btime = time.time()
                # check out
                log.info("Exec checkout start")
                check_out_main()
                log.info("Exec checkout end")
                etime = time.time()
                log.info("Exec check_out_main Use time: %f", etime - btime)
            log.info("udc var value is : %d", update_redis_count)
            log.info("sc value is : %d", scheduler_count)
        except Exception, e:
            traceback.print_exc()

        log.info("sleep start")
        time.sleep(15)
        log.info("sleep end")


def process_info():
    pid = os.getpid()
    res = commands.getstatusoutput('ps aux|grep '+str(pid))[1].split('\n')[0]

    p = re.compile(r'\s+')
    l = p.split(res)
    info = {'user':l[0],
        'pid':l[1],
        'cpu':l[2],
        'mem':l[3],
        'vsa':l[4],
        'rss':l[5],
        'start_time':l[6]}
    return info

if __name__ == "__main__":
    main()

执行一段时间后,此过程将被终止。

在记录器文件中:

2014-12-21 18:55:43,237 INFO Exec sync start
2014-12-21 18:55:43,450 INFO Exec sync end
2014-12-21 18:55:43,451 INFO Exec sync Use time: 0.213755
2014-12-21 18:55:43,451 INFO Exec delete start
2014-12-21 18:55:45,208 INFO info.............{'mem': '0.4', 'start_time': 'pts/2', 'pid': '47562', 'vsa': '85580', 'user': 'root', 'cpu': '12.0', 'rss': '30884'}
2014-12-21 18:55:45,208 INFO Exec delete end
2014-12-21 18:55:45,208 INFO Exec delete Use time: 1.757016
2014-12-21 18:55:45,208 INFO Exec cluster site main start
2014-12-21 18:55:45,429 INFO info.............{'mem': '0.4', 'start_time': 'pts/2', 'pid': '47562', 'vsa': '85580', 'user': 'root', 'cpu': '12.0', 'rss': '30884'}
......
2014-12-21 18:55:28,217 INFO sleep start
2014-12-21 18:55:43,236 INFO sleep end
2014-12-21 18:55:43,237 INFO Exec sync start
2014-12-21 18:55:43,450 INFO Exec sync end
2014-12-21 18:55:43,451 INFO Exec sync Use time: 0.213755
2014-12-21 18:55:43,451 INFO Exec delete start
2014-12-21 18:55:45,208 INFO info.............{'mem': '0.4', 'start_time': 'pts/2', 'pid': '47562', 'vsa': '85580', 'user': 'root', 'cpu': '12.0', 'rss': '30884'}
2014-12-21 18:55:45,208 INFO Exec delete end
2014-12-21 18:55:45,208 INFO Exec delete Use time: 1.757016
2014-12-21 18:55:45,208 INFO Exec cluster site main start
2014-12-21 18:55:45,429 INFO info.............{'mem': '0.4', 'start_time': 'pts/2', 'pid': '47562', 'vsa': '85580', 'user': 'root', 'cpu': '12.0', 'rss': '30884'}
2014-12-21 18:55:45,429 INFO Exec cluster site main end
2014-12-21 18:55:45,429 INFO Exec cluster_site_main Use time: 0.221503
2014-12-21 18:55:45,430 INFO udc var value is : 1
2014-12-21 18:55:45,430 INFO sc value is : 1
2014-12-21 18:55:45,430 INFO sleep start
2014-12-21 18:56:00,449 INFO sleep end
2014-12-21 18:56:00,449 INFO Exec sync start
2014-12-21 18:56:00,653 INFO Exec sync end
2014-12-21 18:56:00,654 INFO Exec sync Use time: 0.204558
2014-12-21 18:56:00,654 INFO Exec delete start
2014-12-21 18:56:02,377 INFO info.............{'mem': '0.4', 'start_time': 'pts/2', 'pid': '47562', 'vsa': '85580', 'user': 'root', 'cpu': '12.0', 'rss': '30884'}

2014-12-21 19:31:35,457 INFO info.............{'mem': '0.0', 'start_time': 'pts/2', 'pid': '9817', 'vsa': '4188', 'user': 'root', 'cpu': '0.0', 'rss': '576'}
2014-12-21 19:31:35,458 INFO Exec update redis end
2014-12-21 19:31:35,458 INFO Exec update_redis_main Use time: 0.490641
2014-12-21 19:31:35,458 INFO udc var value is : 0
2014-12-21 19:31:35,458 INFO sc value is : 1
2014-12-21 19:31:35,458 INFO sleep start
2014-12-21 19:31:50,477 INFO sleep end
2014-12-21 19:31:50,477 INFO Exec sync start
2014-12-21 19:31:50,693 INFO Exec sync end
2014-12-21 19:31:50,693 INFO Exec sync Use time: 0.215589
2014-12-21 19:31:50,693 INFO Exec delete start
2014-12-21 19:31:52,417 INFO info.............{'mem': '0.0', 'start_time': 'pts/2', 'pid': '9820', 'vsa': '4188', 'user': 'root', 'cpu': '0.0', 'rss': '580'}
2014-12-21 19:31:52,417 INFO Exec delete end
2014-12-21 19:31:52,417 INFO Exec delete Use time: 1.724497
2014-12-21 19:31:52,417 INFO Exec cluster site main start
2014-12-21 19:31:52,645 INFO info.............{'mem': '0.0', 'start_time': 'pts/2', 'pid': '9823', 'vsa': '4188', 'user': 'root', 'cpu': '0.0', 'rss': '576'}
2014-12-21 19:31:52,646 INFO Exec cluster site main end
2014-12-21 19:31:52,646 INFO Exec cluster_site_main Use time: 0.228365
2014-12-21 19:31:52,646 INFO udc var value is : 1
2014-12-21 19:31:52,646 INFO sc value is : 1
2014-12-21 19:31:52,646 INFO sleep start

两个问题:

  1. 执行一段时间后,该进程被终止。为什么?
  2. 为什么不同的pid号码?
  3. 这是一个面子项目。

0 个答案:

没有答案