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
#11
unzip command seems to have an issue while using the absolute path

#!/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'])

osTyp = cmd.uname('-s').strip()

if osTyp == 'Linux':
    cmd.unzip("/u03/p21463894_121020_Linux-x86-64.zip")
elif osTyp == 'SunOs':
    cmd.unzip("/u03/p21463894_121020_Linux-x86-64.zip")
else:
    print("!!\n!! unable to determine OS type !!\n!!")
Error:
Traceback (most recent call last):
  File "/u02/scripts/Patching/ApplyPatch.py", line 12, in <module>
    cmd.unzip("/u03/p21463894_121020_Linux-x86-64.zip")
  File "/usr/local/lib/python3.6/site-packages/plumbum/commands/base.py", line 103, in __call__
    return self.run(args, **kwargs)[1]
  File "/usr/local/lib/python3.6/site-packages/plumbum/commands/base.py", line 240, in run
    return p.run()
  File "/usr/local/lib/python3.6/site-packages/plumbum/commands/base.py", line 201, in runner
    return run_proc(p, retcode, timeout)
  File "/usr/local/lib/python3.6/site-packages/plumbum/commands/processes.py", line 232, in run_proc
    return _check_process(proc, retcode, timeout, stdout, stderr)
  File "/usr/local/lib/python3.6/site-packages/plumbum/commands/processes.py", line 23, in _check_process
    proc.verify(retcode, timeout, stdout, stderr)
  File "/usr/local/lib/python3.6/site-packages/plumbum/machines/base.py", line 26, in verify
    stderr)
plumbum.commands.processes.ProcessExecutionError: Command line: ['/u01/app/oracle/product/12.1.0.2/db_1/bin/unzip', '/u03/p21463894_121020_Linux-x86-64.zip']
Exit code: 1
Stdout:  | Archive:  /u03/p21463894_121020_Linux-x86-64.zip
Stderr:  | replace 21463894/etc/config/actions.xml? [y]es, [n]o, [A]ll, [N]one, [r]ename:  NULL
         | (EOF or read error, treating as "[N]one" ...)

Process finished with exit code 1
Reply
#12
Type unzip -h in a terminal to see the doc of the unzip command. There is a -o option to overwrite files without prompting. Use it in your script.
Reply
#13
thanks for the response, helped me a lot. How can I pass list "patch" value as variable to below code?

#!/usr/local/bin/python3.6
import subprocess
from plumbum import local, cmd

patch = [27923320, 27547329, 21463894]

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'])

osTyp = cmd.uname('-s').strip()

for i in patch:
    if osTyp == 'Linux':
        cmd.unzip('-o',"/u03/p[i]_121020_Linux-x86-64.zip")
        local.cwd.chdir('/u03/[i]')
        subprocess.run('pwd', shell=True)

        subprocess.run('opatch apply -silent', shell=True)
        print('Patch applied')

    elif osTyp == 'SunOs':
        cmd.unzip("p[i]_121020_Solaris-x86-64.zip")
    else:
        print("!!\n!! unable to determine OS type !!\n!!")
Error:
   cmd.unzip('-o',"/u03/p[i]_121020_Linux-x86-64.zip")
  File "/usr/local/lib/python3.6/site-packages/plumbum/commands/base.py", line 103, in __call__
    return self.run(args, **kwargs)[1]
  File "/usr/local/lib/python3.6/site-packages/plumbum/commands/base.py", line 240, in run
    return p.run()
  File "/usr/local/lib/python3.6/site-packages/plumbum/commands/base.py", line 201, in runner
    return run_proc(p, retcode, timeout)
  File "/usr/local/lib/python3.6/site-packages/plumbum/commands/processes.py", line 232, in run_proc
    return _check_process(proc, retcode, timeout, stdout, stderr)
  File "/usr/local/lib/python3.6/site-packages/plumbum/commands/processes.py", line 23, in _check_process
    proc.verify(retcode, timeout, stdout, stderr)
  File "/usr/local/lib/python3.6/site-packages/plumbum/machines/base.py", line 26, in verify
    stderr)
plumbum.commands.processes.ProcessExecutionError: Command line: ['/u01/app/oracle/product/12.1.0.2/db_1/bin/unzip', '-o', '/u03/p[i]_121020_Linux-x86-64.zip']
Exit code: 9
Stderr:  | unzip:  cannot find or open /u03/p[i]_121020_Linux-x86-64.zip, /u03/p[i]_121020_Linux-x86-64.zip.zip or /u03/p[i]_121020_Linux-x86-64.zip.ZIP.
         | 
         | No zipfiles found.

Process finished with exit code 1     
Reply
#14
Use "/u03/p{}_121020_Linux-x86-64.zip".format(i)
Reply
#15
Thanks. Seems like now unzip is not working and hence chdir too.
It works only when dir /u03/21171382 and /u03/21463894 exist

#!/usr/local/bin/python3.6
import subprocess
from plumbum import local, cmd

patch = [21171382,21463894]

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'])

osTyp = cmd.uname('-s').strip()

for i in patch:
    if osTyp == 'Linux':
        cmd.unzip('-o',"/u03/p{}_*.zip".format(i))
        local.cwd.chdir('/u03/{}'.format(i))
        subprocess.run('pwd', shell=True)

        #subprocess.run('opatch apply -silent', shell=True)
        print('Patch applied')

    elif osTyp == 'SunOs':
        cmd.unzip("p{}_121020_Solaris-x86-64.zip".format(i))
    else:
        print("!!\n!! unable to determine OS type !!\n!!")
Content of /u03:

[oracle@cdb1 u03]$ ls -ltr
total 97380
drwxr-xr-x. 3 oracle oinstall     4096 Jun 19  2017 app
-rwxrwx---. 1 oracle oinstall 99183505 Oct 17 20:57 p6880880_121010_Linux-x86-64.zip
-rwxrwx---. 1 oracle oinstall   451814 Oct 17 21:01 p21171382_12102180417_Generic.zip
-rwxrwx---. 1 oracle oinstall    73502 Oct 17 21:10 p21463894_121020_Linux-x86-64.zip
Error:
  File "/usr/local/lib/python3.6/site-packages/plumbum/path/local.py", line 354, in chdir
    os.chdir(str(newdir))
FileNotFoundError: [Errno 2] No such file or directory: '/u03/21171382'
Reply
#16
(Oct-20-2018, 04:14 PM)alinaveed786 Wrote: Seems like now unzip is not working and hence chdir too.
Then you need to determine if unzip failed or not. It seems very likely to me that you'd get an error message if unzip failed. I don't know the contents of the zip file. Don'tyou have a typo in the new directory name? (such as a missing 'p' for example). You could list the directory before and after unzipping in order to see the changes. You could also pass the -v switch to unzip to get verbose output. Do
print(cmd.unzip('-o', '-v', "/u03/p{}_*.zip".format(i)))
Reply
#17
Nope. Still, unzip is not working. I have listed the directory in the code before and after the unzip
Moreover, after unzipping the directory name to be 21171382 and 21463894 without prefix 'p', hence I gave "local.cwd.chdir('/u03/{}'.format(i))"

#!/usr/local/bin/python3.6
import subprocess
from plumbum import local, cmd

patch = [21171382,21463894]

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'])

osTyp = cmd.uname('-s').strip()
local.cwd.chdir('/u03/')
subprocess.run('ls -ltr', shell=True)

for i in patch:
    if osTyp == 'Linux':
        print(cmd.unzip('-o','-v',"/u03/p{}_*.zip".format(i)))
        
        subprocess.run('ls -ltr', shell=True)
        local.cwd.chdir('/u03/{}'.format(i))

        print('Patch applied')

    elif osTyp == 'SunOs':
        cmd.unzip("p{}_121020_Solaris-x86-64.zip".format(i))
    else:
        print("!!\n!! unable to determine OS type !!\n!!")
Error:
total 97380
drwxr-xr-x. 3 oracle oinstall     4096 Jun 19  2017 app
-rwxrwx---. 1 oracle oinstall 99183505 Oct 17 20:57 p6880880_121010_Linux-x86-64.zip
-rwxrwx---. 1 oracle oinstall   451814 Oct 17 21:01 p21171382_12102180417_Generic.zip
-rwxrwx---. 1 oracle oinstall    73502 Oct 17 21:10 p21463894_121020_Linux-x86-64.zip
Archive:  /u03/p21171382_12102180417_Generic.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
       0  Defl:N        2   0% 06-27-2018 10:48 00000000  21171382/
       0  Defl:N        2   0% 06-27-2018 10:48 00000000  21171382/files/
       0  Defl:N        2   0% 06-27-2018 10:48 00000000  21171382/files/sqlpatch/
       0  Defl:N        2   0% 06-27-2018 10:48 00000000  21171382/files/sqlpatch/21171382/
       0  Defl:N        2   0% 06-27-2018 10:48 00000000  21171382/files/sqlpatch/21171382/22292308/
       0  Defl:N        2   0% 06-27-2018 10:48 00000000  21171382/files/sqlpatch/21171382/22292308/rollback_files/
       0  Defl:N        2   0% 06-27-2018 10:48 00000000  21171382/files/sqlpatch/21171382/22292308/rollback_files/rdbms/
       0  Defl:N        2   0% 06-27-2018 10:48 00000000  21171382/files/sqlpatch/21171382/22292308/rollback_files/rdbms/admin/
  290535  Defl:N   220895  24% 06-27-2018 10:48 29b949e5  21171382/files/sqlpatch/21171382/22292308/rollback_files/rdbms/admin/prvtstat.plb
     935  Defl:N      474  49% 06-27-2018 10:48 9793a371  21171382/files/sqlpatch/21171382/22292308/rollback_files/rdbms/admin/execstat.sql
     627  Defl:N      335  47% 06-27-2018 10:48 162e19b2  21171382/files/sqlpatch/21171382/22292308/21171382.xml
    1980  Defl:N      682  66% 06-27-2018 10:48 508c13d9  21171382/files/sqlpatch/21171382/22292308/21171382_rollback.sql
    1968  Defl:N      675  66% 06-27-2018 10:48 c21d3bbe  21171382/files/sqlpatch/21171382/22292308/21171382_apply.sql
       0  Defl:N        2   0% 06-27-2018 10:48 00000000  21171382/files/rdbms/
       0  Defl:N        2   0% 06-27-2018 10:48 00000000  21171382/files/rdbms/admin/
  291062  Defl:N   221314  24% 06-27-2018 10:48 b75a453e  21171382/files/rdbms/admin/prvtstat.plb
    2714  Defl:N      718  74% 06-27-2018 10:48 17defd59  21171382/files/rdbms/admin/execstat.sql
    6089  Defl:N     2212  64% 10-01-2018 03:42 f86d4ac8  21171382/README.txt
       0  Defl:N        2   0% 06-27-2018 10:48 00000000  21171382/etc/
       0  Defl:N        2   0% 06-27-2018 10:48 00000000  21171382/etc/config/
    1535  Defl:N      656  57% 06-27-2018 10:48 4360dd6c  21171382/etc/config/inventory.xml
    1242  Defl:N      297  76% 06-27-2018 10:48 c0f3cde0  21171382/etc/config/actions.xml
--------          -------  ---                            -------
  598687           448282  25%                            22 files

total 97380
drwxr-xr-x. 3 oracle oinstall     4096 Jun 19  2017 app
-rwxrwx---. 1 oracle oinstall 99183505 Oct 17 20:57 p6880880_121010_Linux-x86-64.zip
-rwxrwx---. 1 oracle oinstall   451814 Oct 17 21:01 p21171382_12102180417_Generic.zip
-rwxrwx---. 1 oracle oinstall    73502 Oct 17 21:10 p21463894_121020_Linux-x86-64.zip
Traceback (most recent call last):
  File "/u02/scripts/Patching/Test.py", line 21, in <module>
    local.cwd.chdir('/u03/{}*'.format(i))
  File "/usr/local/lib/python3.6/site-packages/plumbum/path/local.py", line 354, in chdir
    os.chdir(str(newdir))
FileNotFoundError: [Errno 2] No such file or directory: '/u03/21171382*'

Process finished with exit code 1
Reply
#18
The error message doesn't match the code. There is an extra * in local.cwd.chdir('/u03/{}*'.format(i)). Where does it come from?

By the way, the error message seems to indicate that unzip worked. The chdir failed.
Reply
#19
Yes. It is now working. The issue was with chdir outside for loop

How can I combine both for loops into one so that I should have only one list variable instead of two(patch1 and patch2)? The reason I used two list variables is because patches of patch1 are inside /u02/28317232 and patches of patch2 are inside /u02/ after unzipping

#!/usr/local/bin/python3.6
import subprocess
from plumbum import local, cmd

patch1 = [27923320,27547329]
patch2 = [21171382,21463894,18961555,28432129]


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'])

osTyp = cmd.uname('-s').strip()

print('Applying PSU patches')
for j in patch1:
    if osTyp == 'Linux':
        local.cwd.chdir('/u02/')
        cmd.unzip('-o', "/u02/p28317232*.zip".format(j))
        local.cwd.chdir('/u02/28317232/{}'.format(j))
        subprocess.run('opatch apply -silent', shell=True)
        print('Patch',j, 'applied')

    elif osTyp == 'SunOs':
        cmd.unzip("p{}_121020_Solaris-x86-64.zip".format(j))
    else:
        print("!!\n!! unable to determine OS type !!\n!!")

print('Applying one-off patches')
for i in patch2:
    if osTyp == 'Linux':
        local.cwd.chdir('/u02/')
        cmd.unzip('-o', "/u02/p{}_*.zip".format(i))
        local.cwd.chdir('/u02/{}'.format(i))
        subprocess.run('opatch apply -silent', shell=True)
        print('Patch',i, 'applied')

    elif osTyp == 'SunOs':
        cmd.unzip("p{}_121020_Solaris-x86-64.zip".format(i))
    else:
        print("!!\n!! unable to determine OS type !!\n!!")
Contents of /u02
[oracle@cdb1 u02]$ pwd
/u02
[oracle@cdb1 u02]$ ls -ltr
total 789832
drwxrwxr-x.  4 oracle oinstall      4096 Aug  6  2015 21463894
drwxrwxr-x.  4 oracle oinstall      4096 Apr 25 15:18 18961555
drwxrwxr-x.  4 oracle oinstall      4096 Jun 27 10:48 21171382
drwxrwxr-x.  4 oracle oinstall      4096 Aug  7 18:32 28432129
-rw-rw-r--.  1 oracle oinstall      4596 Aug  8 07:01 PatchSearch.xml
-rwxrwx---.  1 oracle oinstall     30301 Oct 17 20:59 p18961555_12102180417_Linux-x86-64.zip
-rwxrwx---.  1 oracle oinstall    212238 Oct 17 21:00 p28432129_12102180717_Linux-x86-64.zip
-rwxrwx---.  1 oracle oinstall 807969208 Oct 17 21:01 p28317232_121020_Linux-x86-64.zip
-rwxrwx---.  1 oracle oinstall    451814 Oct 17 21:01 p21171382_12102180417_Generic.zip
-rwxrwx---.  1 oracle oinstall     73502 Oct 17 21:10 p21463894_121020_Linux-x86-64.zip
drwxr-xr-x. 10 oracle oinstall      4096 Oct 18 08:52 pycharm-community-2018.2.4
drwxr-xr-x.  3 oracle oinstall      4096 Oct 18 09:23 scripts
drwxr-xr-x.  4 oracle oinstall      4096 Oct 21 09:11 28317232
[oracle@cdb1 u02]$ 

[oracle@cdb1 u02]$ cd 28317232
[oracle@cdb1 28317232]$ pwd
/u02/28317232
[oracle@cdb1 28317232]$ ls -ltr
total 40
-rw-r--r--.  1 oracle oinstall    21 Jul 10 18:59 README.txt
-rw-rw-r--.  1 oracle oinstall 24710 Jul 12 07:27 README.html
drwxr-xr-x. 18 oracle oinstall  4096 Oct 21 09:11 27547329
drwxr-xr-x.  4 oracle oinstall  4096 Oct 21 09:11 27923320
Reply
#20
You can write a function
#!/usr/local/bin/python3.6
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'])
 
osTyp = cmd.uname('-s').strip()

def apply_patches(msg, patches, zipfile, dir):
    print(msg)
    for j in patches:
        if osTyp == 'Linux':
            local.cwd.chdir('/u02/')
            cmd.unzip('-o', zipfile.format(j))
            local.cwd.chdir(dir.format(j))
            cmd.opatch('apply', '-silent')
            print('Patch',j, 'applied')
        elif osTyp == 'SunOs':
            cmd.unzip("p{}_121020_Solaris-x86-64.zip".format(j))
        else:
            print("!!\n!! unable to determine OS type !!\n!!")

apply_patches(
    'Applying PSU patches',
    [27923320,27547329],
    "/u02/p28317232*.zip",
    "/u02/28317232/{}")
apply_patches(
    'Applying one-off patches',
    [21171382,21463894,18961555,28432129],
    "/u02/p{}_*.zip",
    "/u02/{}")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to pass a mongdb command to a module and execute it. cspower 0 333 Feb-03-2024, 09:54 PM
Last Post: cspower
  problem in using subprocess module akbarza 5 1,062 Sep-24-2023, 02:02 PM
Last Post: snippsat
  Using subprocess to execute complex command with many arguments medatib531 5 1,923 Apr-27-2023, 02:23 PM
Last Post: medatib531
  use subprocess on linux\pi wwith a "grep " command korenron 2 8,154 Oct-19-2021, 10:52 AM
Last Post: DeaD_EyE
  Question on subprocess module. knoxvilles_joker 3 2,717 Apr-11-2021, 12:51 AM
Last Post: knoxvilles_joker
  Error when running mktorrent subprocess command pythonnewbie138 4 3,896 Sep-16-2020, 01:55 AM
Last Post: pythonnewbie138
  Select correct item from list for subprocess command pythonnewbie138 6 3,347 Jul-24-2020, 09:09 PM
Last Post: pythonnewbie138
  python loop in subprocess vinothkumargomu 6 3,240 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,156 Mar-25-2020, 01:52 PM
Last Post: PythonBeginner_2020
  How to parallel executing a command using subprocess? larkypython 0 2,168 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