Z
Z
zelsky2014-10-22 14:50:57
linux
zelsky, 2014-10-22 14:50:57

Create a daemon in python 2.7?

There is a script that calls norify os on while True:
So here's how to create a daemon that starts when the OS starts. And it works in the background without a terminal and other things. Ie only pokazyvet message?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
B
bromzh, 2014-10-22
@zelsky

I always use supervisord . Just a great thing, also written in python. Flexible configs, restart on crash. and other goodies.
How to use:
1) Install it system-wide sudo apt-get install supervisor (you can install it via pip, but you will need to configure more).
2) You need to configure the supervisor itself: where will it get the configs from, what rights are needed, etc. If you have ubuntu, then the settings of the supervisor itself will be in order. They are stored in /etc/supervisor/supervisord.conf. This file says that the settings for the daemons will be read from the /etc/supervisor/conf.d folder.
If you do not have ubuntu, or you installed the supervisor via pip, then here is the standard config. The settings are obvious.
3) When the supervisor is installed and configured, run it:

sudo service supervisor start
или
sudo supervisord

4) Now you need to make a config to run your script. Create a file in the config folder with the following content:
[program:ОТОБРАЖАЕМОЕ_ИМЯ]
command = КОМАНДА_ДЛЯ_ЗАПУСКА (например python ИМЯ_СКРИПТА)
autorestart = true # автостарт при запуске супервизора
stderr_logfile = /var/log/ИМЯ_ЛОГФАЙЛА.err.log # тут перенаправляем stderr в файл
stdout_logfile = /dev/null # тут глушим весь вывод со stdout

Everything! You can manage your daemons with the supervisorctl command . First you need to reread the config, then start the application:
supervisorctl reread
supervisorctl ОТОБРАЖАЕМОЕ_ИМЯ start

It is possible to omit DISPLAY_NAME, in which case the command will be applied to everyone.
You can also start the CLI by typing supervisorctl with no parameters. There, watch the taboo commands.
Read this section to understand the configs of launched applications.

V
Vladimir Zhurkin, 2014-10-22
@icCE

> And it works in the background without a terminal and other things. Ie only pokazyvet message?
Excuse me, but where will he show you the message? Only if you write to the log.
Or are you displaying information in X?
In any case, here's how to write daemons
stackoverflow.com/questions/473620/how-do-you-crea...
habrahabr.ru/post/176067
To run at system startup, you need to write a script to start your program. In theory, you can also play a demon on bash. If you are interested in this option, I will give an example script.

S
Snow Dimon, 2014-10-22
@Snowdimon

What is the script written in?
If on the bash, then add & at the end and throw it into autoload. The script will run in the background as a daemon.

A
Alexander Karabanov, 2014-10-22
@karabanov

For example something like this https://github.com/serverdensity/python-daemon/blo...

Z
zelsky, 2014-12-14
@zelsky

Here's an example for you on my message notification daemon from VK.

L
Lev Bezborodov, 2016-06-21
@EnderTeszla

In my opinion, the ideal implementation is:
web.archive.org/web/20131017130434/http://www.jeji...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question