diff options
author | dec05eba <dec05eba@protonmail.com> | 2020-01-01 14:31:31 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-07-06 07:12:34 +0200 |
commit | d1369b4ee1bdce45b903e99e413f3feec1ecc364 (patch) | |
tree | 8cfdaede4804360fb3a5b98f6b4f61f0ffa0c13b | |
parent | 3586c8d003077ee32b541f00d7690ae179448963 (diff) |
Only allow one instance of automedia to run at once (pid file)
-rw-r--r-- | README.md | 1 | ||||
-rwxr-xr-x | automedia.py | 28 |
2 files changed, 28 insertions, 1 deletions
@@ -7,7 +7,6 @@ Run automedia without any options to see all options. ## TODO 1. Periodically check and remove in_progress files and their directories. This can happen if the computer crashes while adding rss. 2. Automatically remove torrents that have finished seeding, to reduce memory usage and startup time of transmission. -3. Only allow one instance of automedia to run at a time. This is to prevent the config from being corrupt. # Requirements ## System transmission-cli, notify-send (optional) diff --git a/automedia.py b/automedia.py index 272f853..2d8f300 100755 --- a/automedia.py +++ b/automedia.py @@ -7,6 +7,8 @@ import sys import time import json import uuid +import errno +import signal import transmissionrpc from domain import url_extract_domain @@ -19,6 +21,7 @@ script_dir = os.path.dirname(os.path.realpath(sys.argv[0])) config_dir = os.path.expanduser("~/.config/automedia") rss_config_dir = os.path.join(config_dir, "rss") html_config_dir = os.path.join(config_dir, "html") +automedia_pid_path = "/tmp/automedia.pid" only_show_finished_notification = True @@ -721,6 +724,31 @@ def command_sync(args): if not download_dir: usage_sync() + pid_file = None + while True: + try: + pid_file = os.open(automedia_pid_path, os.O_CREAT | os.O_EXCL | os.O_WRONLY) + break + except OSError as e: + if e.errno != errno.EEXIST: + raise + + running_automedia_pid = get_file_content_or_none(automedia_pid_path) + if running_automedia_pid: + cmdline = get_file_content_or_none("/proc/%s/cmdline" % running_automedia_pid) + if cmdline and "automedia.py" in cmdline and "sync" in cmdline: + print("AutoMedia is already running with sync") + exit(0) + os.remove(automedia_pid_path) + + def signal_handler(signum, frame): + os.unlink(automedia_pid_path) + exit(1) + signal.signal(signal.SIGINT, signal_handler) + + os.write(pid_file, ("%s" % os.getpid()).encode()) + os.close(pid_file) + os.makedirs(rss_config_dir, exist_ok=True) os.makedirs(html_config_dir, exist_ok=True) |