Reputation: 2816
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 😀
Upvotes: 0
Views: 169
Reputation: 2816
another simple way of killing multiple ports is
from subprocess import call
call('pkill 50000 , 50001 , 50002 , 50003', shell=True)
print 'ports are closed'
Upvotes: -1
Reputation: 2615
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
Upvotes: 1
Reputation: 57033
" cd .." is not a command; the command is "cd", and ".." is its argument. Consider passing ["cd", ".."]
. Same with lsof
.
Upvotes: 1