3
  • python-daemon-1.5.2-1.el6.noarch

以下は、開発者から受け取ったスクリプトです。

import threading
import multiprocessing, os, signal, time, Queue
import time
from suds.client import Client
from hotqueue import HotQueue
from config import config

queue = HotQueue(config['redis_hotqueue_list'], host=config['redis_host'], port=int(config['redis_port']),password=config['redis_pass'], charset="utf-8",db=0)
@queue.worker()
def sendMail(item):    
    key = item[0]        
    domain = item[1]
    fromemail = item[2]
    fromname = item[3]
    subject = item[4]
    content = item[5]
    toemail = item[6]            
    cc = item[7]
    bcc = item[8]
    replyto = item[9]

    # Convert to string variable
    url = config['sendmail_tmdt_url']
    client = Client(url)        
    client.service.send_mail(key,domain, fromemail,subject, content, toemail,fromname, '','','');               
for i in range(10):
    t = threading.Thread(target=sendMail)
    t.setDaemon(True)
    t.start()
while True:
    time.sleep(50)

ご覧のとおり、彼はthreadingモジュールを使用して、デーモンとして実行できるようにしています。

このブログ投稿に従って、デーモンライブラリを使用するように切り替えます。

これが私の最初の試みです:

from daemon import runner
import logging
import time
import threading
import multiprocessing, os, signal, time, Queue
import time
from suds.client import Client
from hotqueue import HotQueue
from config import config

class Mail():
    def __init__(self):
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/tty'
        self.stderr_path = '/dev/tty'
        self.pidfile_path = '/var/run/sendmailworker/sendmailworker.pid'
        self.pidfile_timeout = 1

    def run(self):    
        while True:
            queue = HotQueue(config['redis_hotqueue_list'], host=config['redis_host'], port=int(config['redis_port']), password=config['redis_pass'], charset=r"utf-8", db=0)
            @queue.worker()
            def sendMail(item):
                key = item[0]        
                domain = item[1]
                fromemail = item[2]
                fromname = item[3]
                subject = item[4]
                content = item[5]
                toemail = item[6]            
                cc = item[7]
                bcc = item[8]
                replyto = item[9]

                # Convert to string variable
                url = config['sendmail_tmdt_url']
                client = Client(url)        
                client.service.send_mail(key,domain, fromemail,subject, content, toemail, fromname, '', '', '');            
                logger.debug("result")
            #sleep(50)

mail = Mail()

logger = logging.getLogger("sendmailworker")
logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler = logging.FileHandler("/var/log/sendmailworker/sendmailworker.log")
handler.setFormatter(formatter)
logger.addHandler(handler)

daemon_runner = runner.DaemonRunner(mail)
daemon_runner.daemon_context.files_preserve=[handler.stream]
daemon_runner.do_action()

動作しますが、起動後にシェルプロンプトを表示するにはCtrl-を押す必要があります:C

/etc/init.d/sendmailworker start

Starting server
# started with pid 2586
^C
#

どうすればこの問題を解決できますか?


アンパサンドを追加しても役に立ちません:

# /etc/init.d/sendmailworker start &
[1] 4094
# Starting server
started with pid 4099
^C
[1]+  Done                    /etc/init.d/sendmailworker start
#

@Celada が指摘したように、実際には、シェル プロンプトは既にありましたが、[root@hostname ~]#通常どおり表示されず、カーソルが点滅するだけです。簡単に押すEnterと、シェルプロンプトが再表示されます。したがって、問題は次のとおりです。started with pid xxxxx最初に、と同じ行に来てStarting server、シェルプロンプトを表示する方法は?


stop関数は正常に動作しています:

[root@hostname ~]# /etc/init.d/sendmailworker stop
Stopping server
Terminating on signal 15
[root@hostname ~]# 

start関数に対して同様のことを行うにはどうすればよいですか? このようなもの:

[root@hostname ~]# /etc/init.d/sendmailworker start
Starting server
started with pid 30624
[root@hostname ~]# 
4

1 に答える 1

4

あなたは変化によってあなたの期待される行動を得ることができます

self.stdout_path = '/dev/tty'
self.stderr_path = '/dev/tty'

に:

self.stdout_path = '/dev/null'
self.stderr_path = '/dev/null'

あなたの場合、シェルスクリプトを使用してinitスクリプトを書くことをお勧めします。

参考までに、ソースコードrunner以外のドキュメントは見つかりません。

于 2013-01-28T06:18:30.153 に答える