Skip to content

Instantly share code, notes, and snippets.

@valin4tor
Last active June 6, 2022 06:01
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save valin4tor/004da18b89fff0534edd9b6f6082bcaf to your computer and use it in GitHub Desktop.
Save valin4tor/004da18b89fff0534edd9b6f6082bcaf to your computer and use it in GitHub Desktop.
Workaround for microsoft/WSL#1614 (place in /usr/bin/winrun)
#!/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 $?
@valin4tor
Copy link
Author

Installation:

  1. Copy script contents to /usr/bin/winrun
  2. Make the file executable using sudo chmod +x /usr/bin/winrun

Usage:

winrun <command> [arguments...]

Example:

echo hello > myfile.txt
winrun notepad myfile.txt

@Zeturic
Copy link

Zeturic commented Aug 27, 2019

This doesn't handle spaces correctly. For example:

echo hi > "multispace  .txt"
winrun notepad "multispace  .txt"

Will result in Notepad complaining about not be able to find multispace .txt (note the whitespace difference).

I was able to get it to work by changing args=${@:2} to args=$(printf '"`"%s`"",' "${@:2}") ; args=${args%,} and argumentlist="-ArgumentList \"$args\"" to argumentlist="-ArgumentList $args".

@mieber
Copy link

mieber commented Sep 12, 2019

This is not showing the log in wsl? e.g. i started a java process with this script and i don't see the logs.

@josejsarmento
Copy link

josejsarmento commented Nov 6, 2020

This is very cool though I can't seem to have it work on ssh: had to change 'powershell.exe' to the full path from wsl to the powershell and same thing for taskkill.exe, though this last one doesn't seem to kill the process.

Update: I changed both paths correctly (I think the path to taskkill was wrong), /mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe and /mnt/c/Windows/System32/taskkill.exe, and now I can terminate the task with CTRL+C. Letting a program run in the background and trying to terminate with "kill -9 pid" doesn't work though.

@valin4tor
Copy link
Author

Letting a program run in the background and trying to terminate with "kill -9 pid" doesn't work though.

This is to be expected as SIGKILL is not something that can be intercepted and immediately kills a process without cleanup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment