killall tor  && service tor start  in Python

Mehran Goudarzi
Mehran Goudarzi used Ask the Experts™
on
i want use this command on my Python script :

 killall tor  && service tor start 

Open in new window

  How can i do it on Python ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
import subprocess
subprocess.call(["killall", "tor"])
subprocess.call(["service", "tor", "start"])

Open in new window


or if you want to implement the '&&' as well
import subprocess
returncode = subprocess.call(["killall", "tor"])
if returncode == 0:
    subprocess.call(["service", "tor", "start"])

Open in new window

Author

Commented:
what is different between  
subprocess.Popen

Open in new window

 and
subprocess.call

Open in new window

If you use subprocess.Popen, then the process is started, but python does not wait for it to be finished.

The equivalen with subprocess Popen would be.

import subprocess
proc1 = subprocess.Popen(["killall", "tor"])
returncode = proc1.wait()
if returncode == 0:
    proc2 = subprocess.Popen(["service", "tor", "start"])
    returncode = proc2.wait()

Open in new window


subprodcess.call() is just a shortcut.

if you use subprocess Popen without the wait function, then you might call
"service tor start" before "killall tor" has been finished.
and the python script would continue executing other python commands before any of the subprocesses had been finished.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial