#!/usr/bin/env python3 import os, shutil, sys from crontab import CronTab def search_str(file_path, word): with open(file_path, 'r') as file: # read all content of a file content = file.read() # check if string present in a file if word in content: return True else: return False user_bin = os.environ["HOME"] + os.sep + "bin" user_zshrc = os.environ["HOME"] + "/.zshrc" my_logfile = os.sep + "var" + os.sep + "log" + os.sep + "pull_and_push.log" basic_command = "* */17 * * * " + "'pull_and_push >> " + my_logfile + "'" print(basic_command) is_exist = False if not os.path.exists(user_bin): os.mkdir(user_bin) if not search_str(user_zshrc, 'PATH=$PATH:'): with open(user_zshrc, 'a') as zshrc: print('PATH=$PATH:' + user_bin, file=zshrc) else: print('PATH=$PATH:', "exist in", user_zshrc, sep=' ') try: copy_file = shutil.copyfile(os.path.dirname(os.path.realpath(sys.argv[0])) + os.sep + "pull_and_push.py", user_bin + os.sep + "pull_and_push") os.chmod(user_bin + os.sep + "pull_and_push",0o775) except FileExistsError: print("File konnte nicht kopiert werden") cron = CronTab(user=os.environ["USER"]) basic_iter = cron.find_command("pull_and_push >> " + my_logfile) for item in basic_iter: if str(item) == basic_command: print("crontab job already exist", item) is_exist=True break if not is_exist: cron_job = cron.new(command="'pull_and_push >> " + my_logfile + "'") cron_job.hours.every(17) cron.write()