Bash: Get PID of windows application and detect when it is closed

trymeout

New Member
Joined
Sep 28, 2019
Messages
1
Reaction score
0
Credits
0
How do I launch a wine program and get the PID of that wine program and then detect when the wine program closes?

This is how I need to run the wine program in order for it to work

Code:
env WINEPREFIX="/usr/share/Applications/Hoyle Board Games" wine "C:\windows\command\start.exe" /Unix "/usr/share/Applications/Hoyle Board Games/drive_c/SIERRA/HCBG2/hcbg2.exe"

I know this can get the PIDs of the specific wine program hcbg2.exe

Code:
ps aux | grep hcbg2.exe | awk '{print $2}'

Please help, all I want is a simple bash script that when executed it will run the wine app and detect when that wine app is closed and continue executing more code.
 


So something like this:
Bash:
#!/usr/bin/env bash

# Start your program
env WINEPREFIX="/usr/share/Applications/Hoyle Board Games" wine "C:\windows\command\start.exe" /Unix "/usr/share/Applications/Hoyle Board Games/drive_c/SIERRA/HCBG2/hcbg2.exe" &

# loop infinitely
# (or until the wine process running hcbg2 has ended)
while true
do
    if [[ -z "$(pgrep -a wine | \grep -i hcbg2 | awk '{print $1}')" ]];
    then
        break # hcbg2 is no longer running
    else
        sleep(5) #sleep for 5 seconds - change this value
    fi
done

# Now run your other commands
echo "Put your other commands here....."

Quite a simple script
- We start your wine program running in the background.
- We set up an infinite while loop

Inside the loop:
- We use a pgrep|\grep|awk combo to check if the process is still running.
The pgrep command searches for any wine processes.
\grep filters the list, to leave only those that are using hcbg2
awk yields the PID/PIDS of any wine processes using hcbg2.

The if [[ -z ]]; test checks whether the value returned by our pgrep|grep|awk combo is empty.

If our pgrep/grep/awk combo returns any PIDS - we sleep for 5 seconds before reiterating through the loop and checking again.

And if no results are returned - we know that the program has finished, so we break out of the infinite loop and can execute some other commands.

You can change the sleep time to whatever suits you. If you want it to check every second put it down to 1. If you want to check every minute - change it to

The only minor caveat to this script is if you have more than one instance of wine running hcbg2.exe - the script will wait until ALL running instances have finished.

So if you are likely to have multiple instances of hcbg2 running and you want the script to work with a specific instance of wine/hcbg2 - we will have to make some modifications.

But if you're only ever going to have a single instance running - then this solution should be fine.
 
Stupid question here : why don't you simply put commands after the wine call in a script shell ?
Code:
#!/bin/bash
# things to do before
env WINEPREFIX="/usr/share/Applications/Hoyle Board Games" wine "C:\windows\command\start.exe" /Unix "/usr/share/Applications/Hoyle Board Games/drive_c/SIERRA/HCBG2/hcbg2.exe"
# things to do after
 
Stupid question here : why don't you simply put commands after the wine call in a script shell ?
Code:
#!/bin/bash
# things to do before
env WINEPREFIX="/usr/share/Applications/Hoyle Board Games" wine "C:\windows\command\start.exe" /Unix "/usr/share/Applications/Hoyle Board Games/drive_c/SIERRA/HCBG2/hcbg2.exe"
# things to do after

Good point - I've overthought things a little in my post haven't I?
Your solution would make much more sense!
:: Slaps forehead ::
 

Members online


Top