1

I've coded a script and am wondering about it's reliability / portability.

#!/bin/bash
threadsPlease() {
  ps -mo lwp,c -p $1 | sort -gk 2 | uniq -f 1 
}

THREAD_LINE=$( threadsPlease $1 | grep -v - | tail -1)

THREAD10=$(echo $THREAD_LINE | awk '{print $1}')
THREAD16=$(printf '%x\n' $THREAD10)

JAVA_LINE=$(jstack $1 | grep $THREAD16)
echo "OS data (thread, CPU usage %): " $THREAD_LINE
echo "Java thread: " $JAVA_LINE

Script displays system and Java thread data, given Java PID. Sample execution:

./whichThreadIsIt.sh 6814
OS data (thread, CPU usage %):  6838 3
Java thread:  "AWT-EventQueue-0" #19 prio=6 os_prio=0 tid=0x00007f50f4090800 nid=0x1ab6 waiting on condition [0x00007f510dfbb000]

Q1: Aside from the obvious ("jstack/java/awk/... is not available or not in path" or "input data given wrongly"), when this will not work the intended way (Ubuntu/Debian)?

Q2: "It's easier to port a shell, than a shell-script", my friend likes to say. So, how much more effort this would need to work in Fedora / CentOS, Free BSD and Solaris? Am I wrong expecting it to run without troubles (assuming Bash present on those boxes)?

  • take a look at some larger 3rd-party software installation scripts, if you can put your hands on them. Ones that support multiple target OS's like the ones you're interested in. See the gyrations they go through to find the programs they're looking for -- even basic ones like ""ps" and "sort" can have non-overlapping flags to do what you want to do. Some OS's will have them in different paths altogether. – Jeff Schaller Aug 30 '15 at 19:50
  • Are you talking about Nix or about Unix? If you're talking about Unix, please write “Unix”. – Gilles 'SO- stop being evil' Aug 30 '15 at 23:32
  • @Gilles, thanks, in my circles when you speak of Unix&Linuxes you usually say Nixes. – LAFK says Reinstate Monica Aug 31 '15 at 16:17
1
#!/usr/bin/env bash

May or may not increase the portability, as if bash is actually installed on a *BSD system, it usually exists at /usr/local/bin/bash. However, env may not be portable, though that would only be for really old or weird unix systems.

ps is an especially not-portable command, especially if you intend to use BSD/GNUisms on a SysV system (e.g. Solaris). Installing the GNU tools somewhere and then fixing PATH to include those would be one solution.

| improve this answer | |
  • GNU tools don't include ps. There's no alternative to the OS's native ps command — it parses some data structures or files that vary a lot between Unix variants. – Gilles 'SO- stop being evil' Aug 30 '15 at 23:31
  • So I take it that while on Debian and derivatives the location for bash would be as in the script, in BSD and Solaris it would fail (no ps, bash). Anything in the script itself that could cause failure on Debian/Ubuntu/Mint/...? Anything not so large that I can do to make script portable to Solaris (preferred more than BSD)? – LAFK says Reinstate Monica Aug 31 '15 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.