User Tools

Site Tools


check_service_and_restart_if_failed

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
check_service_and_restart_if_failed [2020/07/27 01:28]
k2patel
check_service_and_restart_if_failed [2020/08/10 02:35] (current)
Line 15: Line 15:
    ​systemctl start nginx.service    ​systemctl start nginx.service
 fi fi
 +</​code>​
 +
 +for python
 +
 +<code python>
 +#​!/​usr/​bin/​env python3
 +import os
 +import logging
 +from systemd import journal
 +
 +def main(serviceName):​
 +  status = os.system('​systemctl is-active --quiet ' + serviceName)
 +  finisher(status)
 +
 +def finisher(status):​
 +  if status is not 0:
 +    status = os.system('​systemctl start --quiet ' + serviceName)
 +    print(status)
 +    if status is not 0:
 +      log.error('​Failed to start: ' + serviceName)
 +    else:
 +      log.info('​Started service: ' + serviceName)
 +
 +if __name__ == "​__main__":​
 +
 +  # Initialize and handle options
 +  log = logging.getLogger(__file__)
 +  log.addHandler(journal.JournaldLogHandler())
 +  log.setLevel(logging.INFO)
 +
 +  # Define service array
 +  serviceName = ["​nginx"​]
 +  for service in serviceName:​
 +    main(service)
 </​code>​ </​code>​
check_service_and_restart_if_failed.1595813299.txt.gz ยท Last modified: 2020/08/10 02:30 (external edit)