Turning off services SOLVED
At various times I have wanted to turn off certain daemons without
uninstalling their packages. I couldn't find any good way to do this,
so I wrote a little script. I'm making it available under the GPL.
I've since discovered that the some packages also create a bunch of
crontab jobs, and they are still cluttering things up a bit.
If there's a better way to do this, I'd love to hear it.
#! /usr/bin/python
# switchDemon.py
#
# Usage: switchDemon.py (--on | --off) <list of demon names>
#
# This script will scan, turn off, or turn on selected
# demons in /etc/rc?.d/. It does so by renaming S* symbolic
# links so the services won't start.
# Scan mode (neither --on nor --off) simply reports the status
# of the demons.
# Note that demons are not actually started or stopped by this script;
# it just controls what will happen at system startup.
#
# (c) 2001 Ross Boylan RossBoylan@stanfordalumni.org
# Made available under the GPL (see www.gnu.org)
import glob, os.path, re, sys
class Demon:
"Information and actions on a demon"
gDisabled = "Disabled-"
def __init__(self, name):
"Name of demon"
if not os.path.exists("/etc/init.d/"+name):
raise name + " is not a known demon"
self._name = name
self._statuses = []
def checkStatus(self):
"Check status at various run levels"
self.doOverRunLevels(self._checkStatus)
def turnOff(self):
"Make it so won't run at next system start"
self.doOverRunLevels(self._turnOff)
self.checkStatus()
def turnOn(self):
"We will start on next run level change"
self.doOverRunLevels(self._turnOn)
self.checkStatus()
def doOverRunLevels(self, method):
"Invoke method over all run levels"
for rl in glob.glob("/etc/rc[0-9Ss].d"):
method(rl, rl[-3])
def _checkStatus(self, directory, runLevel):
"See if we are around and fill in _statuses if we are"
aRegex = re.compile("("+Demon.gDisabled+r")?S(\d\d)"+self._name);
for file in os.listdir(directory):
aMatch = aRegex.search(file)
if aMatch:
aStatus = DemonStatus(runLevel, aMatch.group(2), not aMatch.group(1))
self._statuses.append(aStatus)
return
def _turnOff(self, directory, runLevel):
"Disable demon for future reboots"
aRegex = re.compile(r"S(\d\d)"+self._name)
for file in os.listdir(directory):
aMatch = aRegex.match(file) # must be at start
if aMatch:
os.rename(os.path.join(directory, file),
os.path.join(directory, Demon.gDisabled+file))
# no return values documented for os.rename
return
def _turnOn(self, directory, runLevel):
"Enable demon for future reboots"
aRegex = re.compile(Demon.gDisabled+r"S(\d\d)"+self._name)
for file in os.listdir(directory):
aMatch = aRegex.match(file) # must be at start
if aMatch:
os.rename(os.path.join(directory, file),
os.path.join(directory, file[len(Demon.gDisabled):]))
# no return values documented for os.rename
return
def reportTo(self, file):
"Send human readable report to stream"
file.write("Demon %s:\n"%self._name)
for aStatus in self._statuses:
file.write("\t%s\n"%str(aStatus))
class DemonStatus:
"Status of a given demon"
def __init__(self, runlevel, priority, enabled=1):
"Priority (nn) of demon and whether it is enabled"
# runlevel is a single 0, 1, ...
self._runlevel = runlevel
self._priority = priority
self._enabled = enabled
def __str__(self):
if self._enabled:
msg = "Enabled"
else:
msg = "Turned off"
return "%s for run level %s (priority %s)"%(msg, self._runlevel, self._priority)
def isEnabled(self):
if self._enabled:
return "on"
else:
return "off"
aCommand = sys.argv[1]
if aCommand[0] == "-":
if aCommand[-1] == "f":
anAction = "turnOff"
elif aCommand[-1] == "n":
anAction = "turnOn"
else:
print "Command not understood"
names = sys.argv[2:]
else:
anAction = "checkStatus"
names = sys.argv[1:]
for aDemonName in names:
aDemon = Demon(aDemonName)
Demon.__dict__[anAction](aDemon)
aDemon.reportTo(sys.stdout)
Reply to: