Linux, PowerShell, Java & hot coffee
TL;DR
- List Java processes with arguments on a separate line: ( $( Get-Process | Where-Object { $_.Name -Like '*java*' } ).Id ) | ForEach-Object { ( Get-Content -LiteralPath "/proc/$($_)/cmdline" ) -Replace "`0", "`n" }
- Terminate all Java processes: Get-Process | Where-Object { $_.Name -Like '*java*' } | Stop-Process
“Isn't that a Windows thing?”
“Oh, &%!*@#!” Zoe exclaimed as the hot coffee spilt under her new MacBook M1. Jane quietly rolled her eyes. Z was quite stubborn when it came to taking advice. Jane saw the danger earlier but knew that her warning would fall on deaf ears. Sometimes you gotta learn the hard way, huh.
After the fourth paper towel had completely soaked up the remnant mess, Zoe turned to their team lead, Gina, for help regarding production monitoring. There were some legacy Spring Framework apps running on their staging environment but it was hard to tell from ps aux/top/htop what parameters they had received. She wanted to check that the correct parameters were sent to them, but, &^@!$%!!, there was no line wrap option in her terminal.
And yes, as this was a government agency, they were back in the office. Social distancing, huh. The small meeting room was packed.
“Zoe,” Gina said as the sprint planning session concluded and colleagues filed out. She opened an ssh session on her ThinkPad and entered the CentOS tmux session. The java processes had some incredibly long lines to them, no thanks to the very, very long classpath variable. They had yet to modularise their Java 8 apps – split packages were still a problem and no one wanted to refactor the packages.
“I have already installed dotnet core so we can just pull up a PowerShell, er, shell.”
“PowerShell? Wait, isn’t that a Windows thing?” said an amused Zoe.
“Yes. But it’s incredibly useful for looking at the process parameters.” Gina, as usual, was talking to the screen. She typed pwsh and voila, a “PS /home/gina>” prompt appeared. She then keyed in:
( $( Get-Process | Where-Object { $_.Name -Like '*java*' } ).Id ) | ForEach-Object { ( Get-Content -LiteralPath "/proc/$($_)/cmdline" ) -Replace "`0", "`n" }
...which printed a nicely formatted list of Java processes and the accompanying arguments.
“You could further customise it and break up the classpath,” she said, finally looking up and smiling a bit. "And, now, the really meaty bit is, when you want to terminate all your Java processes, for whatever reason... you can try this."
Get-Process | Where-Object { $_.Name -Like '*java*' } | Stop-Process
“Nice!” exclaimed Zoe. “Can you paste it into Sla…”
The fire alarm announcement went off. “Warning. Warning. Warning…”
Gina chuckled and closed her laptop, and they headed for the exits with everyone else.
Credits:
Special thanks to mklement0 for his incredibly useful StackOverflow post.
This is a work of fiction. Names, characters, businesses, places, events, locales, and incidents are either the products of the author’s imagination or used in a fictitious manner. Any resemblance to actual persons, living or dead, or actual events is purely coincidental.