Skip to content

Instantly share code, notes, and snippets.

@piraz
Last active April 6, 2022 02:58
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save piraz/de85b67e95132b5cf84e to your computer and use it in GitHub Desktop.
Save piraz/de85b67e95132b5cf84e to your computer and use it in GitHub Desktop.
Script that monitors a service running on systemd. If service is not running the script will try to start the service.
#!/bin/python
#
# Copyright 2016 Flavio Garcia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Usage: monitor_process.py <service_name>
#
# Example(crontab, every 5 minutes):
# */5 * * * * /root/bin/monitor_service.py prosody > /dev/null 2>&1
#
import sys
import subprocess
class ServiceMonitor(object):
def __init__(self, service):
self.service = service
def is_active(self):
"""Return True if service is running"""
cmd = '/bin/systemctl status %s.service' % self.service
proc = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,encoding='utf8')
stdout_list = proc.communicate()[0].split('\n')
for line in stdout_list:
if 'Active:' in line:
if '(running)' in line:
return True
return False
def start(self):
cmd = '/bin/systemctl start %s.service' % self.service
proc = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE)
proc.communicate()
if __name__ == '__main__':
# TODO: Show usage
monitor = ServiceMonitor(sys.argv[1])
if not monitor.is_active():
monitor.start()
@piraz
Copy link
Author

piraz commented Nov 27, 2019

https://gist.github.com/aleshkashell/b4aaa8a6e528732648746990059e3761#gistcomment-2761745 fix for 36 line

I think the issue happens on python 3.4+ right?

@Viacheslav-Wir
Copy link

my current version python 3.5

@hacktarus
Copy link

Hello,
i tried your script under centos8 python 3.8 and hit the
stdout_list = proc.communicate()[0].split('\n')
TypeError: a bytes-like object is required, not 'str'" issue

so i just replaced the line 35
proc = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE)
by
proc = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE,encoding='utf8')

and it works smoothly :-)

thanks for all
Ludo, hacktarus, lovelyplatform and more ;-)

@piraz
Copy link
Author

piraz commented Aug 17, 2020

Did you changed only the line 35 or do you think we need to change 45 also, @hacktarus?

@hacktarus
Copy link

@piraz, i only changed the line 35 because the list at line 36 needed it
don't need to change line 45 because proc.communicate() accept the original code

@piraz
Copy link
Author

piraz commented Aug 18, 2020

Done

@simbuawsassociate
Copy link

@hacktarus You are a lifesaver, Its helped me to fix my issue too. Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment