简体   繁体   中英

Script stops after entering root mode in ubuntu

Script stops after entering root mode in ubuntu I was writing a script in python where I need to be a root user to install a few packages. Issuing "sudo -i" exits the script without proceeding further.Could not figure out the reason. I have posted a part of code where it happens.

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'])

man sudo says

  -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.

You'll need to pass a command to -i otherwise sudo will try to open an interactive shell. Try daisy chaining the commands:

In [7]: import subprocess

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

In [9]:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM