Posts

Showing posts with the label python

Running Python Functions like a Batch Program

 When you need to automatize some batch operations via Python, you can create a function array and loop through it in order to run them from the start. Here is an example: from functions import * def main():     functions=[(BackupDB,(backupname,dbtype)),(ZipBackup,(backupname,)),(RemoveOldBackups,())]     for func,args in functions:          result=func(*args)          function_name=func.__name__          if result == 0:               print(f"{function_name} step has been completed")          else:               print(f"{function_name} step has errors. Program has been stopped.")               return main() As you can see, you can define parameters by opening another bracket after comma(,) in the function array. If no parameters, then only open a...

Warn yourself when your linux server isn't restarted for a while after an update (Python)

 You can see the python code below. It's suitable for RHEL/Centos/OEL servers: import subprocess import datetime import sys output = subprocess.check_output(["rpm","-qa","kernel-uek*"]) output = output.decode().strip().split("\n") output.sort(key=lambda x: subprocess.check_output(["rpm","-q","--queryformat","%{INSTALLTIME}",x])) output.reverse() active_kernel=subprocess.check_output(["uname","-r"]).decode().strip() active_kernel="kernel-uek-"+active_kernel #print(active_kernel) #print(output[0]) if active_kernel == output[0]:     print("Aktif kernel en güncel sürümdedir.")     sys.exit(0) else:     most_recent_install_date = subprocess.check_output(["rpm","-q", "--queryformat","%{INSTALLTIME}",output[0]])     most_recent_install_date = datetime.datetime.fromtimestamp(int(most_recent_install_date))     active_install_date = s...