Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python2
- # coding:utf-8
- import subprocess
- import os
- class ServiceFile(object):
- def __init__(self):
- self._path = '/etc/systemd/system'
- self.fn = 'cpugovernor.service'
- self.lines = ['[Unit]\n', 'Description=CPU Performance\n', '\n'
- '[Service]\n', 'Type=oneshot\n', 'ExecStart=/usr/bin/set-perf\n', '\n',
- '[Install]\n', 'WantedBy=multi-user.target\n']
- def create_executable(self):
- current_path = os.path.join(os.getcwd(), 'set-perf')
- r = subprocess.Popen(['sudo', 'ln', '-s', current_path, '/usr/bin/set-perf', '2>&1'], stdout=subprocess.PIPE).communicate()[0]
- def create_file(self):
- self.create_executable()
- with open(os.path.join(self._path, self.fn), 'w') as f:
- for line in self.lines:
- f.write(line)
- def enable_service(self):
- subprocess.Popen(['sudo', 'systemctl', 'start', self.fn])
- subprocess.Popen(['sudo', 'systemctl', 'enable', self.fn], stdout=subprocess.PIPE)
- subprocess.Popen(['sudo', 'systemctl', 'daemon-reload'])
- r = subprocess.Popen(['sudo', 'systemctl', 'status', os.path.join(self._path, self.fn)], stdout=subprocess.PIPE).communicate()[0]
- r = r[r.find('Loaded'):]
- if not 'not loaded' in r:
- return True
- else:
- return False
- if __name__ == '__main__':
- if os.getuid() != 1000:
- service = ServiceFile()
- service.create_file()
- print('Serviço está rodando: {}'.format(service.enable_service()))
- else:
- print('Script rodando sob nível usuário. Por-favor execute novamente como superusuário.')
- exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement