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 😀