4

I am having trouble running a .sh script in Oracle Linux 6.8 using a python script I have developed. I've developed these scripts in Windows OS and they run without a problem, now I am trying to modify these scripts to run them on Linux but I am having problems since I am not familiar with Linux OS.

I run a .bat file in Windows using the Popen command in python, for example: p = Popen("StartScript.bat", cwd=r"My path")

Where StartScript.bat is the .bat file, and My Path is the path where the .bat file is located. This works very good in Windows OS, but how do I do the same for Linux OS, if I want to run a StartScript.sh file.

2 Answers 2

5

execute a shell-script from Python subprocess

How to execute a shell script through python

Please take a look at these two threads. Hope it helps.

Sign up to request clarification or add additional context in comments.

7 Comments

Gaurav Pathak, according to the links you have provided, there is no need to enter a path (where the .sh file is located) to execute the .sh file when using Popen? Is this right?
@viganqerimi You need to provide the path of the shell script. If Popen("StartScript.bat", cwd=r"My path") is not working for you. May I know the error that you are gettting?
FileNotFoundError: [Errno 2] No such file or directory: 'start_stageconv.sh'
What I need is the equivalent of my popen command that I have used in Windows OS, but for LINUX OS. So I can execute my .sh script using python. Is this going to work: p = Popen(['./start_stageconv.sh'], stdout=PIPE, stderr=PIPE)
@viganqerimi Use p = Popen(["sh", "./start_stageconv.sh"], stdout=PIPE, stderr=PIPE) You can also provide the path of the shell script instead of ./start_stageconv.sh if you don't want to put your python script in the same directory also I suggest to check the presence of file using os.path.exists() before passing it to subprocess.Popen()
|
4

use this lines:

import os
os.popen('sh command')

for example

import os
os.popen('sh /home/oracle/scripts/start.sh')

enjoy

1 Comment

how to pass arguments?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.