title: Changing user in python
body: <p>I am writing a simple script which restarts a hadoop slave. In the script, I have to do some initial changes as a root user. After that I have to change to user "hadoop" and perform set of commands. I was using os.system to run commands but I doubt whether it works well. For example: </p>
  
  <pre><code>uid=pwd.getpwnam('hadoop')[2]
  os.setuid(uid)
  os.system('whoami')
  os.chdir('/home/hadoop/hadoop/')
  os.system('bin/hadoop-daemon.sh stop tasktracker')
  </code></pre>
  
  <p>Again I have to perform some commands as root after this and again become user "hadoop" and execute : </p>
  
  <pre><code>os.system('bin/hadoop-daemon.sh stop tasktracker')
  </code></pre>
  
  <p>I have three questions here , </p>
  
  <ol>
  <li><p>Is os.system is the best command that I can use to issue linux commands ?</p></li>
  <li><p>I am able to change from root user to user hadoop by the commands above but I am not able to change to root user (I can understand there will be security issues if they permit this, I want to know is there any possibility to do that , atleast by passing password) ?</p></li>
  <li><p>Does os.setuid() work ? whoami prints user hadoop but the process "tasktracker" is not stopped using those command, but if i perform the same commands manually it works fine (I use "su hadoop" instead of setuid while trying it out manually).</p></li>
  </ol>
  
  <p>Thanks for all your help. </p>
  
  <ul>
  <li>Sethu </li>
  </ul>