1

I need to execute a bash script from a python program (python2.7) in red hat, while this bash script has to run from another user account as splunk user.

In the Linux, I will switch to splunk user su - splunk first, then type the command ./mybashFile under as a splunk user

Here is what I tried:

import  subprocess
cmd1=subprocess.Popen(["su","-","splunk"],shell=True,stdout=subprocess.PIPE)
cmd2=subprocess.Popen(["./path/myBashFile.sh"],shell=True,stdin=cmd1.stdout,stdout=subprocess.PIPE)
cmd2.stdout

I still cannot run this bash file from as a splunk user. This command cannot pass to another account.

Field.D
  • 158
  • 1
  • 1
  • 9
  • 2
    See [this question](https://stackoverflow.com/questions/37586811/pass-commands-as-input-to-another-command-su-ssh-sh-etc) for the fundamental reason why this can't work as posted – that other guy Jan 16 '19 at 21:46

1 Answers1

4

cmd1 and cmd2 are two different processes and have no effect on each other. If you wanted to go the su route, you'd need to call you shell script through that:

...["su","-","splunk", "-c", "./path/myBashFile.sh"]...
Ondrej K.
  • 8,841
  • 11
  • 24
  • 39