0

I am having an issue running bash commands from a python script. some of them works while others don't

ls command works

from subprocess import call
call(["ls"])

but I want to close some ports, but below command, don't seem to work

from subprocess import call
call(['lsof -ti:9938,9955,9910,9988 | xargs kill'])

gives an error

   /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Users/amar/Documents/vKey_XenServer/vkey/architectures/Reactive/deamons/test.py
Traceback (most recent call last):
  File "/Users/amar/Documents/vKey_XenServer/vkey/architectures/Reactive/deamons/test.py", line 2, in <module>
    call(['lsof -ti:a9938,9955,9910,9988 | xargs kill'])
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 522, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

so I tried some of the commands, few were working and others were not

For instance, top command works

from subprocess import call
call(['top'])

but, cd .. does not

from subprocess import call
call(['cd ..'])

gives an error

Traceback (most recent call last):
  File "/Users/amar/Documents/vKey_XenServer/vkey/architectures/Reactive/deamons/test.py", line 2, in <module>
    call(['cd ..'])
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 522, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

I may be asking a rudimentary question, but how do I differentiate which shell command will work in python.

Thanks in advance guys 😀

1
  • everything should work , what error are you getting? Commented Jan 10, 2018 at 5:12

3 Answers 3

1

For the command using PIPE , you need to redirect the output of one command to the input of other subprocess Popen class.

Below example should work for you

from subprocess import Popen
import shlex
input="lsof -ti:9938,9955,9910,9988"
command=shlex.split(input) #will split the command in list format
p1 = Popen(command, stdout=PIPE)
p2 = Popen(["xargs", "kill"], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close()  # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0]

For detailed information you can refer here https://docs.python.org/2/library/subprocess.html

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

Comments

1

" cd .." is not a command; the command is "cd", and ".." is its argument. Consider passing ["cd", ".."]. Same with lsof.

2 Comments

it worked, thanks for this update but how do I make lsof -ti:9938,9955,9910,9988 | xargs kill work ?
You can not do this with call because there is a pipe involved.
-1

another simple way of killing multiple ports is

from subprocess import call
call('pkill 50000 , 50001 , 50002 , 50003', shell=True)
print 'ports are closed'

Comments

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.