2

I am using python 2.7

cmd = "sudo start service/newservice db=temp"
p = subprocess.Popen(shlex.split(cmd), stderr=subprocess.STDOUT)

Above command works fine and intented process is spawned.

cmd = "sudo stop service/newservice db=temp"
p = subprocess.Popen(shlex.split(cmd), stderr=subprocess.STDOUT)

In the same file when above is called. It gives error.

traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python2.7/subprocess.py", line 672, in __init__
        errread, errwrite)
      File "/usr/lib/python2.7/subprocess.py", line 1213, in _execute_child
        raise child_exception
    TypeError: must be encoded string without NULL bytes, not str
S-T
  • 479
  • 2
  • 4
  • 17
  • maybe you can try stopping the first process from python, instead of launching another process to kill the first. see this link for details: http://stackoverflow.com/questions/4084322/killing-a-process-created-with-pythons-subprocess-popen – symbiotech Nov 20 '13 at 09:58
  • could be some strange character inserted in there, see this: http://stackoverflow.com/questions/15203106/wxpython-how-do-we-remove-null-byte-from-string-when-using-textcontrol-getvalue – symbiotech Nov 20 '13 at 10:32
  • 1
    How does `cmd` look like with `print(repr(cmd))`? – glglgl Nov 20 '13 at 12:28

1 Answers1

1

I got it solved. I only converted cmd to str before passing it to shlex.split, this is good esp when you receive some input this way:

cmd = "sudo stop service/newservice db=" + db

Safer practice is to convert it to string anyway.

S-T
  • 479
  • 2
  • 4
  • 17
  • 1
    Please use format specifier in that.. for eg. cmd = "sudo stop service/newservice db=%s"%str(db) – Nilesh Nov 21 '13 at 10:45