Skip to content

Instantly share code, notes, and snippets.

@jmiserez
Last active June 12, 2019 16:54
Show Gist options
  • Save jmiserez/5a9a9e59f4c0ba63aacc172f42c72447 to your computer and use it in GitHub Desktop.
Save jmiserez/5a9a9e59f4c0ba63aacc172f42c72447 to your computer and use it in GitHub Desktop.
Bash on WSL or Cygwin: suspend and resume Windows processes by name and optionally command line part
# Add this to your Cygwin or WSL Ubuntu .bashrc
# Args:
# - process name
# - optional: commandline (use '%' as wildcards)
# Example: suspendps name commandline
# - suspendps notepad.exe %mytextfile.txt%
# - resumeps notepad.exe
# Note: pssuspend64.exe must be installed and available (https://docs.microsoft.com/en-us/sysinternals/downloads/pssuspend)
getwin32processid() {
# WSL / Windows 10 suitable version without spawning a shell
wmic.exe /interactive:off process where "caption like '$1' and commandline like '%$2%'" get processid /FORMAT:list | grep -oP '(?<=ProcessId=)[0-9]+'
# Old version using PowerShell (works in Cygwin, but messes up terminal in Win 10 WSL)
# echo $(powershell.exe "Get-WmiObject Win32_Process -Filter \"Name = '$1' and CommandLine like '$2'\" | Select-Object -Expand ProcessId" | tr -c -d '0123456789\n')
}
suspendps() {
for i in $(getwin32processid "$1" "$2"); do pssuspend64.exe -nobanner "$i"; done
}
resumeps() {
for i in $(getwin32processid "$1" "$2"); do pssuspend64.exe -nobanner -r "$i"; done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment