I want to add a line(such as '*/data/mod/myservice start some_parameter*'.) to /etc/rc.d/rc.local file in shell script. If there exists a line start with '*/data/mod/myservice start*', then replace it by new one.
In my script, it execute the next python method.
def excuteCmd(cmd):
import commands
output = commands.getoutput(cmd)
def setTask(cmd, installFlag):
print cmd, installFlag
excuteCmd('cat /etc/rc.d/rc.local > oldTask')
input = open('oldTask','r')
emptyFile = False
lines = input.readlines()
input.close()
taskNum = len(lines)
output = open('newTask', 'w')
if (taskNum == 0):
if (installFlag):
output.write(cmd + '\n')
else:
for i in range(taskNum):
if (lines[i].find(cmd) == -1):
output.write(lines[i])
if (installFlag):
output.write(cmd + '\n')
output.close()
excuteCmd('sudo cat newTask > /etc/rc.d/rc.local')
excuteCmd('rm -f oldTask')
excuteCmd('rm -f newTask')
But when i execute sudo cat newTask > /etc/rc.d/rc.local, it raise the following error.
-bash: /etc/rc.d/rc.local: Permission denied
This means that you don’t have permission to either write to or delete the file. Also, you won’t be able to run the
sudocommand like that without typing in a password, so ideally the script itself would be run usingsudo python scriptname.