简体   繁体   中英

how to run command line in linux from another user

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.

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

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