Launch ZAP using a script if not already running

Question:

I launch ZAP with a Python script using subprocess:

filePath=r"C:\Program Files\OWASP\Zed Attack Proxy\zap-2.11.1.jar"
subprocess.Popen(filePath, shell=True, stdout=subprocess.PIPE)

This script works fine and launches ZAP. However, I’d like to have a check whether the app is already running and if so, not to launch it again. I took a look how could this be achieved in Python and realized I could use a check for running processes. Problem is that the process runs as Java(TM) Platform SE binary in Task Manager so checking for that one might not be the best solution.

Asked By: povit

||

Answers:

A simple workaround would be to simply grab all titles of current processes and if it happens to find zap amongst them then break

or just have a flag set if it finds it as follows:

import subprocess
import pyautogui
global marker
marker = False

for window in pyautogui.getAllWindows():  
    if window.title == "OWASP ZAP":
        marker = True
if marker:
    filePath=r"C:\Program Files\OWASP\Zed Attack Proxy\zap-2.11.1.jar"
    subprocess.Popen(filePath, shell=True, stdout=subprocess.PIPE)
Answered By: bido whab
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.