-
Notifications
You must be signed in to change notification settings - Fork 36
Create pid file to avoid having two daemons. #126
Conversation
Adds the --pid-file option in the parser. Add method to create and remove the pid file. Set the signal handler and atexit to remove the pid file when the daemon finishes.
Codecov Report
@@ Coverage Diff @@
## master #126 +/- ##
=========================================
- Coverage 69.67% 68.78% -0.9%
=========================================
Files 12 12
Lines 1672 1701 +29
=========================================
+ Hits 1165 1170 +5
- Misses 507 531 +24
Continue to review full report at Codecov.
|
ospd/misc.py
Outdated
|
||
pid = str(os.getpid()) | ||
|
||
if os.path.isfile(pidfile): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use pathlib instead
if os.path.isfile(pidfile): | |
if Path(pidfile).is_file(): |
ospd/misc.py
Outdated
|
||
def remove_pidfile(pidfile, signum=None, frame=None): | ||
""" Removes the pidfile before ending the daemon. """ | ||
if os.path.isfile(pidfile): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use the pathlib here too. Maybe it is even better to already pass pidfile as a Path object to the functions but i am not 100% sure about that.
ospd/misc.py
Outdated
""" Removes the pidfile before ending the daemon. """ | ||
if os.path.isfile(pidfile): | ||
LOGGER.debug("Finishing daemon process") | ||
os.remove(pidfile) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be done via a Path instance
pidpath = Path(pidfile)
if pidfile.is_file():
...
pidfile.unlink()
Now the pid in the pid file is the right one. Also initialize the daemon after going to background, to not hang in the console during initialization.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Thank you @bjoernricks ! |
Adds the --pid-file option in the parser.
Add method to create and remove the pid file.
Set the signal handler and atexit to remove the pid file when the daemon finishes.
How to:
by default, the pid path is /run/ospd/ospd.py. You will have to create the directory and set the right permissions.
Or you can run ospd-daemon with the option --pid-file and use an existing directory with rw permissions:
This is also valid to do it with the config file, adding under the right section the option. E.g.: