#!/usr/bin/python3 """Implementation of CloudSync launcher and management script""" import os import signal import sys import time import logging from subprocess import Popen DELAY = 0.5 USAGE_FORMAT = "Usage: ./cs.py [debug|info|warning|error]" arguments = sys.argv logging_level = logging.INFO def get_pid(): proc_list = list(os.popen("ps ax | grep cloud_sync.py | grep -v grep")) if len(proc_list) > 0: pid = proc_list[0].split()[0] return pid else: return None def start(): cs_proc = get_pid() if cs_proc: print("CloudSync app already running") else: print("Starting CloudSync app with logging level {0}".format(logging_level)) time.sleep(DELAY) Popen(['python3', 'cloud_sync.py', str(logging_level)]) def stop(): cs_proc_pid = get_pid() if cs_proc_pid: print("Stopping CloudSync app...") time.sleep(DELAY) os.kill(int(cs_proc_pid), signal.SIGKILL) else: print("CloudSync app is not running.") if len(arguments) == 2 or len(arguments) == 3: if len(arguments) == 3: if arguments[2] == "debug": logging_level = logging.DEBUG elif arguments[2] == "info": logging_level = logging.INFO elif arguments[2] == "warning": logging_level = logging.WARNING elif arguments[2] == "error": logging_level = logging.ERROR else: print(USAGE_FORMAT) if arguments[1] == "start": start() elif str(arguments[1]) == "stop": stop() elif arguments[1] == "restart": print("Restarting CloudSync app...") time.sleep(DELAY) stop() start() elif arguments[1] == "status": cs_proc = get_pid() if cs_proc: print("CloudSync app IS running") else: print("CloudSync app IS NOT running") else: print(USAGE_FORMAT) else: print(USAGE_FORMAT)