_note: the original posted question is edited and its faced has changed quite a bit!
Summary:
A python script using apshceduler fails to load as service with exit code staus1. Important to note that same script designed to run every 10 seconds using while True:
(without using apscheduler to do the same task) syntax runs very well as daemon (service).
Details
I wrote a simple test.py program. It appends some a.txt file every 10 seconds with "some" string. When I try to run it as daemon service throws error code. . In itself, without using it as daemon (service), the code works fine. Also if I don't use apscheduler in writing the test.py file then the service runs smoothly.
I will put all the code here for details including the 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()
Its located at /home/sangharsh/code/workingWithFiles/
To run it as daemon I create service file -
[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
This file is located at /lib/systemd/system/
.
I restart the daemon:
sudo systemctl daemon-reload
Then enable the test.service file -
sudo systemctl enable test.service
and to start the test.service
sudo systemctl start test.service
and to scheck the status
sudo systemctl status test.service
The systemctl status
shows:
● 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
The output of 'sudo journalctl -xe' is as below:
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)
~
~
I referred this question. I tried the options but it doesn't help.
Please guide.
Some Clarifications
some clarification to avoid any confusion -
- I tried running the test.py script and it works fine. It can write a message to 'a.txt' file. So its successful code.
- I also tried another way. I tried removing 'apscheduler' and instead use
while True
syntax. That way I was able to run it daemon. The output ofsystemctl status
in that case is active. After giving rwx permission to ugo for "a.txt" file, I was able to run it as service file (as said in summary above)
/home/sangharsh/code
exist? Try writing to a file in/tmp
. (3) You say “if I don't use apscheduler in writing the test.py file then the service runs smoothly.” Can you clarify that? (4) You mention awhile True:
syntax. What are you talking about? Is this the answer to question #3? … … … … … … … … … Please do not respond in comments; edit your question to make it clearer and more complete.-x
when posting logs anywhere. Usejournalctl -e -u test.service
to fetch relevant log lines.