Last active
June 6, 2022 06:01
-
-
Save vtenfys/004da18b89fff0534edd9b6f6082bcaf to your computer and use it in GitHub Desktop.
Workaround for microsoft/WSL#1614 (place in /usr/bin/winrun)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
command=$1 | |
args=${@:2} | |
winrun_pid=$$ | |
pidfile="/tmp/winrun-pid-$(date +%s)" | |
if [[ $args != '' ]]; then | |
argumentlist="-ArgumentList \"$args\"" | |
fi | |
powershell_command=" | |
\$process = Start-Process -NoNewWindow -PassThru \"$command\" $argumentlist | |
if (\$process) { | |
echo \$process.id | |
Wait-Process \$process.id | |
exit \$process.ExitCode | |
} else { | |
# startup failure | |
echo -1 | |
}" | |
powershell.exe -Command "$powershell_command" > $pidfile & | |
linux_pid=$! | |
# Use tail to wait for the file to be populated | |
while read -r line; do | |
windows_pid=$(echo $line | tr -d '\r\n') | |
break # we only need the first line | |
done < <(tail -f $pidfile) | |
rm $pidfile | |
if [[ $windows_pid == -1 ]]; then | |
exit 127 | |
fi | |
term() { | |
taskkill.exe -pid $windows_pid > /dev/null | |
} | |
trap term SIGTERM | |
trap term SIGINT | |
while ps -p $linux_pid > /dev/null; do | |
wait $linux_pid | |
done | |
exit $? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is to be expected as SIGKILL is not something that can be intercepted and immediately kills a process without cleanup