Python Forum
OS command via python subprocess module
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
OS command via python subprocess module
#1
Can we use below OS commands (inside the file.txt) and then pass this file an argument to the subprocess.popen for execution?
OR is there a better way to execute all below OS commands inside python script?





export PATH=$ORACLE_HOME/bin:$ORACLE_HOME/OPatch:$PATH 
cd $ORACLE_HOME 
mv OPatch OPatch.pre6880880 
mkdir OPatch 

 
osTyp=`uname -s` 
if [ "$osTyp" = "Linux" ]; 
then 
cp /gfd/infr/DBA/oracle12102-Linux-X86-64/6880880_zipDir/p6880880_122010_Linux-x86-64.zip . 
unzip p6880880_122010_Linux-x86-64.zip 
elif [ "$osTyp" = "SunOS" ]; 
then 
cp /gfd/infr/DBA/oracle12102-Solaris64/6880880_zipDir/p6880880_122010_SOLARIS64.zip . 
unzip p6880880_122010_SOLARIS64.zip 
else 
echo "!!" echo "!! unable to determine OS type" echo "!!" 
fi
Reply
#2
I recently came across a nice way to convert such scripts: the plumbum module. Using this module, your python code could more or less resemble the following
from plumbum import local, cmd
ORACLE_HOME = local.env['ORACLE_HOME']
local.env['PATH'] = "{ohome}/bin:{ohome}/OPatch:{path}".format(
    ohome=ORACLE_HOME, path=local.env[PATH])
local.cwd.chdir(ORACLE_HOME)
cmd.mv('OPatch', 'OPatch.pre6880880')
cmd.mkdir('OPatch')

osTyp = cmd.uname('-s').strip()
if osTyp == 'Linux':
    cmd.cp(
        "/gfd/infr/DBA/oracle12102-Linux-X86-64/6880880_zipDir/p6880880_122010_Linux-x86-64.zip", ".")
    cmd.unzip("p6880880_122010_Linux-x86-64.zip")
elif osTyp == 'SunOs':
    cmd.cp("/gfd/infr/DBA/oracle12102-Solaris64/6880880_zipDir/p6880880_122010_SOLARIS64.zip", ".") 
    cmd.unzip("p6880880_122010_SOLARIS64.zip") 
else
    print("!!\n!! unable to determine OS type !!\n!!")
Reply
#3
Thanks. Will look into the plumbum module. Anyways any idea why I m not able to get cd via subprocess.run

subprocess.run('echo $ORACLE_HOME', shell=True)
subprocess.run('cd "$ORACLE_HOME"', shell=True)
subprocess.run('pwd', shell=True)
output:
/u01/app/oracle/product/12.1.0.2/db_1
/u02/scripts/Patching
Reply
#4
(Oct-20-2018, 09:53 AM)alinaveed786 Wrote: any idea why I m not able to get cd via subprocess.run
It is because subprocess.run('cd $ORACLE_HOME', shell=True) starts a new shell process and changes directory in this spawned process. It doesn't change the current directory of the calling process. For this you need to call os.chdir()
Reply
#5
Not working either with os.chdir.

#!/usr/bin/python
import os
import subprocess


subprocess.run('echo $ORACLE_HOME', shell=True)

os.chdir("'$ORACLE_HOME'")

subprocess.run('pwd', shell=True)
Error
/u01/app/oracle/product/12.1.0.2/db_1
Traceback (most recent call last):
  File "/u02/scripts/Patching/Ping.py", line 9, in <module>
    os.chdir("'$ORACLE_HOME'")
FileNotFoundError: [Errno 2] No such file or directory: "'$ORACLE_HOME'"
Reply
#6
Try os.chdir(os.environ['ORACLE_HOME'])
Reply
#7
thanks, it worked. Moreover, I installed the plumbum module and executed the code but got below error
Traceback (most recent call last):
  File "/u02/scripts/Patching/Test.py", line 8, in <module>
    local.env['PATH'] = "{ohome}/bin:{ohome}/OPatch:{path}".format(ohome=ORACLE_HOME, path=local.env[PATH])
NameError: name 'PATH' is not defined
Reply
#8
(Oct-20-2018, 10:22 AM)alinaveed786 Wrote: thanks, it worked. Moreover, I installed the plumbum module and executed the code but got below error
Traceback (most recent call last):
  File "/u02/scripts/Patching/Test.py", line 8, in <module>
    local.env['PATH'] = "{ohome}/bin:{ohome}/OPatch:{path}".format(ohome=ORACLE_HOME, path=local.env[PATH])
NameError: name 'PATH' is not defined

The source of you exception (posting code with lines broken to fit screen may be a good idea)
path=local.env[PATH]
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#9
Thanks, it worked. Additionally, I want to add below statement to the code but its throwing error. Basically, want to remove directory "OPatch.pre6880880" but it's not an empty directory

#!/usr/bin/python

from plumbum import local, cmd

ORACLE_HOME = local.env['ORACLE_HOME']
path=local.env['PATH']
local.env['PATH'] = "{ohome}/bin:{ohome}/OPatch:{path}".format(ohome=ORACLE_HOME, path=local.env['PATH'])
local.cwd.chdir(ORACLE_HOME)
cmd.rm -rf('OPatch.pre6880880')
cmd.mv('OPatch', 'OPatch.pre6880880')
cmd.mkdir('OPatch')
osTyp = cmd.uname('-s').strip()

if osTyp == 'Linux':
    cmd.cp("/u03/p6880880_121010_Linux-x86-64.zip", ".")
    cmd.unzip("p6880880_121010_Linux-x86-64.zip")
elif osTyp == 'SunOs':
    cmd.cp("/u03/p6880880_121010_Linux-x86-64.zip", ".")
    cmd.unzip("p6880880_121010_Linux-x86-64.zip")
else:
    print("!!\n!! unable to determine OS type !!\n!!")
Error
    cmd.rm -rf('OPatch.pre6880880')
NameError: name 'rf' is not defined
Reply
#10
This is python, not bash. Use cmd.rm('-rf', 'OPatch.pre6880880' )
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to pass a mongdb command to a module and execute it. cspower 0 312 Feb-03-2024, 09:54 PM
Last Post: cspower
  problem in using subprocess module akbarza 5 1,020 Sep-24-2023, 02:02 PM
Last Post: snippsat
  Using subprocess to execute complex command with many arguments medatib531 5 1,847 Apr-27-2023, 02:23 PM
Last Post: medatib531
  use subprocess on linux\pi wwith a "grep " command korenron 2 8,073 Oct-19-2021, 10:52 AM
Last Post: DeaD_EyE
  Question on subprocess module. knoxvilles_joker 3 2,683 Apr-11-2021, 12:51 AM
Last Post: knoxvilles_joker
  Error when running mktorrent subprocess command pythonnewbie138 4 3,838 Sep-16-2020, 01:55 AM
Last Post: pythonnewbie138
  Select correct item from list for subprocess command pythonnewbie138 6 3,292 Jul-24-2020, 09:09 PM
Last Post: pythonnewbie138
  python loop in subprocess vinothkumargomu 6 3,176 Jul-06-2020, 12:02 PM
Last Post: vinothkumargomu
  Subprocess command working for one cmd and for cmd one not wrking PythonBeginner_2020 0 4,132 Mar-25-2020, 01:52 PM
Last Post: PythonBeginner_2020
  How to parallel executing a command using subprocess? larkypython 0 2,142 Nov-28-2019, 03:49 PM
Last Post: larkypython

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020