为什么在首次启动时从rc.local运行时Python脚本不可靠?

时间:2010-09-13 05:34:32

标签: python boot

以root身份登录并从中运行时,下面的脚本运行良好 命令行,但在Ubuntu中使用/etc/rc.local首次启动时运行 10.04,它失败了大约25%的时间 - 系统根,mysql root和 一些mysql用户密码设置正确,但一个将失败 控制台日志报告标准mysql登录错误:“ERROR 1045(28000): 用户'root'@'localhost'拒绝访问(使用密码:YES)“

是否存在关于从init作业运行python脚本的问题 应该考虑,比如环境变量?

#!/usr/bin/env python 
# Randomizes and outputs to files the system root and mysql user passwords 
files = ['/home/ubuntu/passwords','/opt/data1/alfresco/extensions/ 
extension/alfresco-global.properties','/opt/data/etc/mysql/ 
debian.cnf','/home/ubuntu/duncil'] 
userpasswords = {'root':'ROOTPASSWORD'} 
mysqlpasswords = 
{'root':'MYSQLPASSWORD','alfresco':'alfrescoPASSWORD','debian-sys- 
maint':'debian-sys-maintPASSWORD'} 
otherpasswords = ['OTHERPASSWORD'] 
log = '/var/log/firstrun' 
import random, string 
import crypt 
import re 
from subprocess import PIPE, Popen 
def getsalt(chars = string.letters + string.digits): 
    # generate a random 2-character 'salt' 
    return random.choice(chars) + random.choice(chars) 
def getpwd(chars = string.letters + string.digits, len = 12): 
    retval = ""; 
    for i in range(0, len): 
    # generate 12 character alphanumeric password 
        retval += random.choice(chars) 
    return retval 
def replace_pass(filename): 
    handle = open(filename, 'r') 
    hbuf = handle.read() 
    handle.close() 
    for placeholder, password in pdict.iteritems(): 
        hbuf = re.sub(placeholder, password, hbuf) 
    try: 
        # Output file 
        handle = open(filename, 'w') 
        handle.write(hbuf) 
        handle.close() 
    except: 
        pass 
        #logh.write('failed to update ' + filename  + "\n") 
        #logh.write('maybe you don\'t have permision to write to it?\n') 
logh = open(log, "a") 
logh.write("Starting...\n") 
# Generate passwords 
pdict = {} 
for user, placeholder in userpasswords.iteritems(): 
    syspass = getpwd() 
    Popen(['usermod', '--password', crypt.crypt(syspass, getsalt()), user]) 
    logh.write(placeholder + ": User " + user + " --> " + syspass + "\n") 
    pdict[placeholder] = syspass 
# Whats the MySQL Root password placeholder? 
mplace = mysqlpasswords['root'] 
for user, placeholder in mysqlpasswords.iteritems(): 
    mpass = getpwd() 
    if (("root" in mysqlpasswords) and (mysqlpasswords['root'] in pdict)): 
        mrootpass = pdict[mysqlpasswords['root']] 
    else: 
        mrootpass = "" 
    Popen(['mysql', '-uroot', "--password=" + mrootpass, "-e", "UPDATE user SET Password = PASSWORD('" + mpass + "') WHERE User = '" + user + "';FLUSH PRIVILEGES;","mysql"]) 
    logh.write(placeholder + ": MySQL " + user + " --> " + mpass + "\n") 
    pdict[placeholder] = mpass 
for placeholder in otherpasswords: 
    opass = getpwd() 
    logh.write(placeholder + ": " + opass + "\n") 
    pdict[placeholder] = opass 
# Update passwords 
for file in files: 
    logh.write("Replacing placeholders in " + file + "\n") 
    replace_pass(file) 
logh.write("Finished\n") 
logh.close 

1 个答案:

答案 0 :(得分:2)

Popen不能异步执行吗?

似乎在启动期间,负载很高,并且您在设置root密码和使用它设置下一个密码(下一个命令)之间遇到竞争条件。

尝试

p = Popen(['mysql', '-uroot', "--password=" + mrootpass, "-e", "UPDATE user SET Password = PASSWORD('" + mpass + "') WHERE User = '" + user + "';FLUSH PRIVILEGES;","mysql"])
p.wait()

并查看是否可以。