Go Premium for a chance to win a PS4. Enter to Win


killall tor  && service tor start  in Python

Posted on 2017-05-08
Medium Priority
Last Modified: 2017-05-08
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 ?
Question by:Mehran Goudarzi
  • 2
LVL 17

Accepted Solution

gelonida earned 2000 total points
ID: 42124771
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 Comment

by:Mehran Goudarzi
ID: 42124869
what is different between  

Open in new window


Open in new window

LVL 17

Expert Comment

ID: 42125848
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.

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question