2014-02-12 18 views
0

腳本在ubuntu下進入root模式後停止 我在python中編寫腳本,我需要成爲root用戶才能安裝一些軟件包。發出「sudo -i」會退出腳本,無法繼續進行。無法找出原因。 我已經發布了一部分代碼。腳本在ubuntu下進入root模式後停止

def install_SOFTWARE(): 
    print" SOFTWARE DIRECTORY\n"+SOFTWARE_dir+"\n\n" #SOFTWARE_dir is global variable 
    subprocess.call(['sudo','-i'])     #Code exits here   
    os.chdir(SOFTWARE_dir) 
    subprocess.call(['sudo','make']) 
    subprocess.call(['sudo','make','install']) 

回答

1

man sudo

-i [command] 
       The -i (simulate initial login) option runs the shell 
       specified in the passwd(5) entry of the target user as a 
       login shell. This means that login-specific resource files 
       such as .profile or .login will be read by the shell. If a 
       command is specified, it is passed to the shell for 
       execution. Otherwise, an interactive shell is executed. 
       sudo attempts to change to that user's home directory 
       before running the shell. It also initializes the 
       environment, leaving DISPLAY and TERM unchanged, setting 
       HOME, MAIL, SHELL, USER, LOGNAME, and PATH, as well as the 
       contents of /etc/environment on Linux and AIX systems. All 
       other environment variables are removed. 

你需要一個命令傳遞給-i否則sudo將嘗試打開一個交互式shell。試試菊花鏈接命令:

In [7]: import subprocess 

In [8]: subprocess.call(['sudo', '-i', 'echo "hi"']) 
Password: 
hi 
Out[8]: 0 

In [9]: 
+0

更好地使用'echo「密碼正確」';-) – Alfe

+0

嘿,謝謝,泡吧指令確實奏效。 :) – Ani