Skip to content

Instantly share code, notes, and snippets.

@quasoft
Created October 9, 2016 16:36
Show Gist options
  • Save quasoft/2b7a1b11be70540dfe396019f2bdd758 to your computer and use it in GitHub Desktop.
Save quasoft/2b7a1b11be70540dfe396019f2bdd758 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import signal
import daemon
import lockfile
import time
def start():
print("Initial setup")
def stop():
print("Cleanup")
def reload():
print("Reload config")
def run():
while True:
print("Main program")
time.sleep(10)
context = daemon.DaemonContext(
working_directory='/',
umask=2,
pidfile=lockfile.FileLock('/tmp/mylockfile')
)
context.signal_map = {
signal.SIGTERM: stop,
signal.SIGHUP: 'terminate',
signal.SIGUSR1: reload,
}
# No need to change gid, as we want our service to run without root
#context.gid = grp.getgrnam('nogroup').gr_gid
# No files to preserve
#context.files_preserve = [important_file, interesting_file]
start()
with context:
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment