4
3

More than 3 years have passed since last update.

pythonプログラムをdaemon化してサーバーで定期実行

Posted at

pythonプログラムをdaemon化して定期実行

crontabでやりたかったのですが、躓いてしまったので、ググってみるとなんと
pythonプログラムをdaemon化⇒定期実行できる!ということで。
自分メモ

pip install

以下のコマンドでpython-daemonモジュールをインストールします。

pip install python-daemon

最終的なソース

import pymsteams
import time
import schedule
import daemon
import sys


def good_morning():
 myTeamsMessage = pymsteams.connectorcard("{Teams Incoming Webhook URL}")
 myTeamsMessage.title("hogehoge")
 myTeamsMessage.text("fugafuga")
 myTeamsMessage.send()

// 毎日朝9時に"good_morning()"メソッドを実行する、の意
schedule.every().day.at("09:00").do(good_morning)
dc = daemon.DaemonContext(stdout=sys.stdout)

with dc:
 while True:
  schedule.run_pending()
  time.sleep(1)

PGの止め方

$ ps -aux | grep {動かしたpythonファイル名}
$ kill {process番号}

まとめ

馴染みのあるプログラミングで、サーバーで定期実行できるのは  
いいですね!

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3