Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

killall tor  && service tor start  in Python

Posted on 2017-05-08
3
Medium Priority
?
122 Views
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 ?
0
Comment
Question by:Mehran Goudarzi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 17

Accepted Solution

by:
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

1
 

Author Comment

by:Mehran Goudarzi
ID: 42124869
what is different between  
subprocess.Popen

Open in new window

 and
subprocess.call

Open in new window

0
 
LVL 17

Expert Comment

by:gelonida
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.
0

Featured Post

A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

Question has a verified solution.

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

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
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…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

730 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