D
D
Denis2016-12-01 15:49:52
linux
Denis, 2016-12-01 15:49:52

How to write a daemon to work with files in python?

You need to write a daemon to work with files, read and write. I use the code from https://github.com/serverdensity/python-daemon as a daemon , I also tried python-daemon. Everything works without the daemon. When the daemon starts, no file is created.

import os
from daemon import Daemon

class workDaemon(Daemon):
    def run(self):
        f = open('some123456.txt', 'w')
        f.write('some text')
        f.close()

if __name__ == '__main__':
    work = workDaemon('/path/to/pid.pid')
    work.start()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2016-12-01
@D3Nd3R

1) Daemon - a process/thread that runs all the time while the main process/thread is running. Why do you need a daemon if it just writes to a file and does nothing else
2) '/path/to/pid.pid' - Is this a joke or what? You just don't have the right to put the PID there. Put at least in '/tmp/idontneedadaemon.pid'
3) Use context managers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question