add functions to only run once and catch keyboard interrupt
This commit is contained in:
parent
9157b67dd3
commit
5d93bd80fe
|
@ -0,0 +1,16 @@
|
|||
import sys
|
||||
import fcntl
|
||||
import os
|
||||
|
||||
_run_once_file_handle = 0
|
||||
|
||||
|
||||
def run_once():
|
||||
global _run_once_file_handle
|
||||
|
||||
host_script_file_path = os.path.realpath(sys.argv[0])
|
||||
_run_once_file_handle = open(host_script_file_path, 'r')
|
||||
try:
|
||||
fcntl.flock(_run_once_file_handle, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
except:
|
||||
sys.exit('Another instance already running')
|
|
@ -0,0 +1,6 @@
|
|||
import signal
|
||||
import sys
|
||||
|
||||
def sigint_handler(signal, frame):
|
||||
sys.exit(0)
|
||||
signal.signal(signal.SIGINT, sigint_handler)
|
28
main.py
28
main.py
|
@ -1,4 +1,6 @@
|
|||
import argparse, time
|
||||
import feedparser
|
||||
import f_run_once, f_sigint
|
||||
from settings import *
|
||||
from mastodon import Mastodon
|
||||
from os.path import exists
|
||||
|
@ -83,6 +85,28 @@ def main():
|
|||
for lines in send_data:
|
||||
f.write('%s\n' %lines)
|
||||
f.close()
|
||||
|
||||
def startup():
|
||||
f_run_once.run_once()
|
||||
print("ok")
|
||||
parser = argparse.ArgumentParser(description = "Mastodon robot posting RSS-Feeds")
|
||||
parser.add_argument('--daemon', dest='daemon', action='store_true', help='run in daemon mode and repeat after a delay')
|
||||
parser.add_argument('--delay', dest='daemon_delay', action='store', type=int, help='number of seconds to wait for next run default=3600')
|
||||
args = parser.parse_args()
|
||||
run_as_daemon = False
|
||||
if args.daemon or args.daemon_delay:
|
||||
run_as_daemon = True
|
||||
if run_as_daemon:
|
||||
daemon_delay = 3600
|
||||
if args.daemon_delay:
|
||||
daemon_delay = args.daemon_delay
|
||||
if daemon_delay == 0 or not isinstance(daemon_delay, int):
|
||||
daemon_delay = 3600
|
||||
|
||||
if run_as_daemon:
|
||||
while True:
|
||||
main()
|
||||
time.sleep(daemon_delay)
|
||||
else:
|
||||
main()
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
startup()
|
||||
|
|
Loading…
Reference in New Issue