Question

I have python app with loop, which generates some files, saves video images and some other stuff. i installed it on Fedora (17) PC and want it to run "forever", ie if it hangs (i can put some keep_alive in file in loop) - it should be restarted. It also should be started on reboot. As i understand, python-daemon help to do this, and systemd in Fedora. I have the following config file for systemd (im not sure on some parameters though as documentation is too complicated for my level of linux knowledge):

[Unit]
Description=TPR Daemon

[Service]
Type=forking
Restart=always
WorkingDirectory=/home/igor/tpr
PIDFile=/var/run/tpr.pid
ExecStart=/usr/bin/python /home/igor/tpr/testd.py

[Install]
WantedBy=default.target

and here is my testd.py:

import daemon
import time, sys

class MyDaemon(object):
    def __init__(self):
        pass

    def run(self):
        while True:
            print 'I am alive!'
            time.sleep(1)

if __name__ == '__main__':
    with daemon.DaemonContext(stdout=sys.stdout):
        check = MyDaemon()
        check.run()

when i run it with "sudo systemctl start tpr.service", it hangs for a while and then cancels out with this meesage:

Warning: Unit file of tpr.service changed on disk, 'systemctl --system daemon-reload' recommended. Job for tpr.service failed. See 'systemctl status tpr.service' and 'journalctl -xn' for details.

and here are some logs from /var/log/messages:

Aug  9 21:32:27 localhost systemd[1]: Unit tpr.service entered failed state.
Aug  9 21:32:27 localhost systemd[1]: tpr.service holdoff time over, scheduling restart.
Aug  9 21:32:27 localhost systemd[1]: Stopping TPR Daemon...
Aug  9 21:32:27 localhost systemd[1]: Starting TPR Daemon...
Aug  9 21:33:57 localhost systemd[1]: tpr.service operation timed out. Terminating.
Aug  9 21:33:57 localhost python[28702]: I am alive!
Aug  9 21:33:57 localhost python[28702]: I am alive!
Aug  9 21:33:57 localhost python[28702]: I am alive!
Aug  9 21:33:57 localhost python[28702]: I am alive!
...
Aug  9 21:33:57 localhost python[28702]: I am alive!
Aug  9 21:33:57 localhost python[28702]: I am alive!
Aug  9 21:33:57 localhost python[28702]: I am alive!
Aug  9 21:33:57 localhost python[28702]: I am alive!
Aug  9 21:33:57 localhost systemd[1]: tpr.service: control process exited, code=exited status=1
Aug  9 21:33:57 localhost systemd[1]: Failed to start TPR Daemon.
Aug  9 21:33:57 localhost systemd[1]: Unit tpr.service entered failed state.
Aug  9 21:33:57 localhost systemd[1]: tpr.service holdoff time over, scheduling restart.
Aug  9 21:33:57 localhost systemd[1]: Stopping TPR Daemon...
Aug  9 21:33:57 localhost systemd[1]: Starting TPR Daemon...

so it should be running, but what this error about? And maybe there is some simple convenient way to accomplish my task, and im inventing bicycle?

Update:

It seems the daemon should somehow let systemd know it has started..but how?

Aug 10 01:15:36 localhost systemd[1]: Starting TPR Daemon...
Aug 10 01:17:06 localhost systemd[1]: tpr.service operation timed out. Terminating.
Aug 10 01:17:06 localhost systemd[1]: tpr.service: control process exited, code=exited status=1
Aug 10 01:17:06 localhost systemd[1]: Failed to start TPR Daemon.
Aug 10 01:17:06 localhost systemd[1]: Unit tpr.service entered failed state.
Aug 10 01:17:06 localhost systemd[1]: tpr.service holdoff time over, scheduling restart.
Aug 10 01:17:06 localhost systemd[1]: Stopping TPR Daemon...
Aug 10 01:17:06 localhost systemd[1]: Starting TPR Daemon...
Aug 10 01:18:36 localhost systemd[1]: tpr.service operation timed out. Terminating.
Aug 10 01:18:36 localhost systemd[1]: tpr.service: control process exited, code=exited status=1
Aug 10 01:18:36 localhost systemd[1]: Failed to start TPR Daemon.
Aug 10 01:18:36 localhost systemd[1]: Unit tpr.service entered failed state.
Aug 10 01:18:36 localhost systemd[1]: tpr.service holdoff time over, scheduling restart.
Aug 10 01:18:36 localhost systemd[1]: Stopping TPR Daemon...
Aug 10 01:18:36 localhost systemd[1]: Starting TPR Daemon...
Was it helpful?

Solution

The error about changing on the disk means just that the file is changed. After you run systemctl daemon-reload the file will be reread and hten you'll be able to start the service. You can use notifications as said in this manual pages. The type for the service is notify. Next thing is: you say your service type as forking. Does your process really forks? It is recommended to have PIDfile option set up if you use forking. With systemd it is not necessary to fork your process to become daemon.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top