在ubuntu中进入root模式后脚本停止

时间:2014-02-12 09:23:04

标签: python ubuntu

在ubuntu中进入root模式后脚本停止 我在python中编写了一个脚本,我需要成为root用户来安装一些软件包。发布“sudo -i”退出脚本而不进一步继续。不能弄清楚原因。 我发布了一部分代码。

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 个答案:

答案 0 :(得分: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]: