3

I have a python script that needs to run as a daemon on startup. The process detaches from tty(and pdb), but the code doesn't run.

I've narrowed it down to a minimal example

import daemon
from time import sleep
f1 = open('out.txt','a')
with daemon.DaemonContext():
   while(1):
       f1.write('this is a test')
       sleep(5)

I expect the script to keep runiing and adding a line to out.txt every 5 seconds, but the script just detaches from tty(or pdb) and ps -ax shows that the python interpreter isn't running anymore. out.txt is created, but stays empty

p0te
  • 31
  • 2
  • I think a better approach would be to use [supervisord](http://supervisord.org/) to [execute the script](http://supervisord.org/running.html#adding-a-program) instead of using the daemon module – new-dev-123 Jul 05 '19 at 08:52
  • Please explain what makes you think that @new-dev-123 – Reblochon Masque Jul 05 '19 at 08:55
  • 1
    You may want to consider using systemd. Take a look at e.g. https://stackoverflow.com/questions/13069634/python-daemon-and-systemd-service – Stanowczo Jul 05 '19 at 08:56
  • 1
    Your operating system already gives you capabilities to run scripts as daemons - most likely one of sysv, upstart or systemd. There is no need to write demonisation yourself. – MisterMiyagi Jul 05 '19 at 08:56
  • The script is to be deployed on FreeBSD, so systemd isn't an option, unfortunately. I've gotten it to show up in ps, but it still doesn't print to the file. – p0te Jul 05 '19 at 09:15
  • 1
    @p0te: `it doesn't print to the file` because it's buffered. Put `f1.flush()` after `f1.write ...` to see the content of the file immediately. – Vladimir Botka Jul 05 '19 at 11:44
  • 1
    @p0te: FWIW, [Here](https://stackoverflow.com/questions/51833364/how-to-debug-rc-d-scripts-in-freebsd/) is a simple example of how to use rc.d. – Vladimir Botka Jul 05 '19 at 11:47
  • @Vladimir Botka thanks, that fixed the printing – p0te Jul 06 '19 at 12:10
  • So is your problem solved, or are you getting printing now but then the process is still dying? I agree with the others in saying that you are best off not having your Python script know anything about how it will run, and using a standard mechanism for running daemon processes, like systemd or supervisord. – CryptoFool Jul 06 '19 at 17:31
  • The problem is solved, thanks. The issue was an error that caused the program(not this minimal example) to die in the while(1). – p0te Jul 22 '19 at 10:29

1 Answers1

0

You may want to use a process supervisor.

To simplify the process and have a portable solution not depending for example on systemd (Linux only) you could install for example immortal, in FreeBSD just need to do:

pkg install immortal

Then create a your-script.yml with something like this:

cmd: sleep 3

And daemonize it with:

$ immortal -c test.yml

To check the status you could use immortalctl:

$ immortalctl    
  PID     Up   Down   Name      CMD
29993   0.0s          test   sleep 3

If want to have it always up even when rebooting, just move your script (in FreeBSD) to /usr/local/etc/immortal/your-script.yml, check more about immortaldir

You can add more option for exaple:

cmd: iostat 3
log:
    file: /tmp/iostat.log
    age: 10  # seconds
    num: 7   # int
    size: 1  # MegaBytes
require_cmd: test -f /tmp/foo

For more examples check: https://immortal.run/post/run.yml/

nbari
  • 25,603
  • 10
  • 76
  • 131