2

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...
asked Aug 9, 2013 at 17:37

1 Answer 1

3

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.

answered Aug 10, 2013 at 20:30
Sign up to request clarification or add additional context in comments.

4 Comments

reload error fixed, thank you. my process does not fork, i can hardly yet understand whats that mean, type set as recommended As i understand forking is like child or subprocess, but i have just a loop inside my script. so what type to set in my case? if i set type to "notify" it fails with this: Aug 11 10:34:08 localhost systemd[1]: Starting TPR Daemon... Aug 11 10:35:38 localhost systemd[1]: tpr.service operation timed out. Terminating. Aug 11 10:35:38 localhost systemd[1]: Failed to start TPR Daemon. Aug 11 10:35:38 localhost systemd[1]: Unit tpr.service entered failed state.
Yes. Whith sysv init system a process should fork itself twice to become daemon. With systemd it does not need it. I hardly understand how systemd works in daemonizing processes (I should take a mocre comprehensive look on its sources). Surely it is timeout because you don't send notification to the systemd with sd_notify: freedesktop.org/software/systemd/man/sd_notify.html If you don't use notifications you may set Type=simple.
And one more thing: what does that import daemon string do?
Well, it seems nothing.. app works without being "daemon" or whatever.. it just works now, with "simple" type, thank you!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.