システムサービスはPythonコードを実行し、エラーコードステータス1で終了します。

システムサービスはPythonコードを実行し、エラーコードステータス1で終了します。

_note:もともと投稿された質問が編集され、外観が大幅に変更されました!

要約:

apshcedulerを使用するPythonスクリプトは、終了コードstaus1を使用してサービスにロードされません。while True:同じタスクを実行するには、apschedulerを使用せずに10秒ごとに実行されるように設計された同じスクリプトの構文がデーモン(サービス)でうまく実行されることに注意することが重要です。


詳細

私は簡単なtest.pyプログラムを書いた。 10秒ごとに「some」文字列を含むいくつかのa.txtファイルを追加します。デーモンサービスとして実行しようとするとエラーコードが発生します。 。コード自体はデーモン(サービス)として使用しなくてもうまく機能します。また、test.pyファイルの作成時にapschedulerを使用しないと、サービスはスムーズに実行されます。

以下を含む詳細については、ここにすべてのコードを入力します。systemctl status

#!/usr/bin/env python3
import os, glob, shutil
from datetime import datetime
from apscheduler.schedulers.blocking import BlockingScheduler
sched = BlockingScheduler()
def test():
    appendFile = open(r'/home/sangharsh/code/a.txt', 'a')
    appendFile.write("Jai Bhim \n" )
    appendFile.close()
sched.add_job(test, 'interval', seconds=10)

sched.start()

デーモンとして実行するには、/home/sangharsh/code/workingWithFiles/ サービスファイルを生成します。

[Unit]
Description=Test Service
After=multi-user.target
[email protected]

[Service]
Type=simple
ExecStart=/usr/bin/env python3 /home/sangharsh/code/workingWithFiles/test.py
StandardInput=tty-force

[Install]
WantedBy=multi-user.target

ファイルはにあります/lib/systemd/system/

デーモンを再起動します。

sudo systemctl daemon-reload

次に、test.serviceファイルを有効にします -

sudo systemctl enable test.service

test.serviceを起動します。

sudo systemctl start test.service

そして状態を確認してください

sudo systemctl status test.service

パフォーマンスsystemctl status:

● test.service - Test Service
   Loaded: loaded (/lib/systemd/system/test.service; enabled; vendor preset: ena
   Active: failed (Result: exit-code) since Tue 2019-07-09 22:57:31 IST; 8min ag
  Process: 4750 ExecStart=/usr/bin/env python3 /home/sangharsh/code/workingWithF
 Main PID: 4750 (code=exited, status=1/FAILURE)

Jul 09 22:57:31 sangharsh-HP-240-G4-Notebook-PC systemd[1]: Started Test Service
Jul 09 22:57:31 sangharsh-HP-240-G4-Notebook-PC systemd[1]: test.service: Main p
Jul 09 22:57:31 sangharsh-HP-240-G4-Notebook-PC systemd[1]: test.service: Failed

「sudo Journalctl -xe」の出力は次のとおりです。

Subject: Unit failed
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
-- 
-- The unit test.service has entered the 'failed' state with result 'exit-code'.
Jul 13 02:12:23 sangharsh-HP-240-G4-Notebook-PC sudo[18333]: sangharsh : TTY=pts/0 ; PWD=/home/sangharsh ; USER=root ; COMMAND=/bin/systemctl status test.service
Jul 13 02:12:23 sangharsh-HP-240-G4-Notebook-PC sudo[18333]: pam_unix(sudo:session): session opened for user root by (uid=0)
Jul 13 02:12:38 sangharsh-HP-240-G4-Notebook-PC systemd-resolved[856]: Server returned error NXDOMAIN, mitigating potential DNS violation DVE-2018-0001, retrying transaction with reduced feature level UDP.
Jul 13 02:13:11 sangharsh-HP-240-G4-Notebook-PC sudo[18333]: pam_unix(sudo:session): session closed for user root
Jul 13 02:13:53 sangharsh-HP-240-G4-Notebook-PC sudo[18364]: sangharsh : TTY=pts/0 ; PWD=/home/sangharsh ; USER=root ; COMMAND=/usr/bin/pip3 install apscheduler
Jul 13 02:13:53 sangharsh-HP-240-G4-Notebook-PC sudo[18364]: pam_unix(sudo:session): session opened for user root by (uid=0)
Jul 13 02:13:55 sangharsh-HP-240-G4-Notebook-PC sudo[18364]: pam_unix(sudo:session): session closed for user root
Jul 13 02:14:13 sangharsh-HP-240-G4-Notebook-PC systemd-resolved[856]: Server returned error NXDOMAIN, mitigating potential DNS violation DVE-2018-0001, retrying transaction with reduced feature level UDP.
Jul 13 02:15:13 sangharsh-HP-240-G4-Notebook-PC sudo[18378]: sangharsh : TTY=pts/0 ; PWD=/home/sangharsh ; USER=root ; COMMAND=/bin/journalctl -xe
Jul 13 02:15:13 sangharsh-HP-240-G4-Notebook-PC sudo[18378]: pam_unix(sudo:session): session opened for user root by (uid=0)
Jul 13 02:16:02 sangharsh-HP-240-G4-Notebook-PC sudo[18378]: pam_unix(sudo:session): session closed for user root
Jul 13 02:16:32 sangharsh-HP-240-G4-Notebook-PC sudo[18388]: sangharsh : TTY=pts/0 ; PWD=/home/sangharsh ; USER=root ; COMMAND=/bin/journalctl -xe test.service
Jul 13 02:16:32 sangharsh-HP-240-G4-Notebook-PC sudo[18388]: pam_unix(sudo:session): session opened for user root by (uid=0)
Jul 13 02:16:32 sangharsh-HP-240-G4-Notebook-PC sudo[18388]: pam_unix(sudo:session): session closed for user root
Jul 13 02:16:44 sangharsh-HP-240-G4-Notebook-PC sudo[18390]: sangharsh : TTY=pts/0 ; PWD=/home/sangharsh ; USER=root ; COMMAND=/bin/journalctl -xe
Jul 13 02:16:44 sangharsh-HP-240-G4-Notebook-PC sudo[18390]: pam_unix(sudo:session): session opened for user root by (uid=0)
~
~

私は言及しましたこれ質問。このオプションを試しましたが、役に立ちませんでした。

ご案内ください。

いくつかの説明

混乱を避けるためのいくつかの説明 -

  1. test.pyスクリプトを実行してみましたが、うまくいきます。 「a.txt」ファイルにメッセージを書き込むことができます。したがって、コードは成功します。
  2. 私も別の方法を試しました。 「apscheduler」を削除し、代わりに構文を試してみましたwhile True。このようにしてデーモンを実行できました。この場合、出力がsystemctl status有効になります。 「a.txt」ファイルでugoにrwx権限を付与した後、サービスファイルとして実行することができました(上記の要約で述べたように)。

ベストアンサー1

システム単位ファイルの次の行はまだ間違っています。

ExecStart=/usr/bin/env/python3 /home/sangharsh/code/workingWithFiles/test.py

/usr/bin/env/python3私はそれがあなたのシステムに存在しないと確信しています。これはMuluの最初のコメントのような質問です。

おすすめ記事