I am new to Linux. I don't know about shell scripts. I need to get the idle process time of a given Linux process. I am writing a Java program. But there are no Linux commands for my need. How do I write a shell script that could do this? Then I can execute this script from Java.
Here you are:
#! /bin/bash
# Assumptions:
# Process is attached to a tty.
#
[[ -z "$1" ]] && echo "Usage: $0 pid" && exit 1
[[ "$1" != +([0-9]) ]] && echo "$1 is not a valid pid" && exit 1
PID="$1"
W=$(which w)
PS=$(which ps)
SED=$(which sed)
AWK=$(which awk)
TTY=$($PS -o tty4 $PID)
TTNo=$(echo "$TTY" | $SED -e '/TTY/d')
TIME=$($W | $SED -n -e "/pts\/$TTNo/p" | $AWK '{ print $5 }')
echo $PID has been idle for $TIME
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句