相关文章推荐
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
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
                maybe you can try stopping the first process from python, instead of launching another process to kill the first. see this link for details: stackoverflow.com/questions/4084322/…
– symbiotech
                Nov 20, 2013 at 9:58
                could be some strange character inserted in there, see this: stackoverflow.com/questions/15203106/…
– symbiotech
                Nov 20, 2013 at 10:32

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.

Please use format specifier in that.. for eg. cmd = "sudo stop service/newservice db=%s"%str(db) – Nilesh Nov 21, 2013 at 10:45

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

 
推荐文章