我是Linux新手。我不了解shell脚本。我需要获得给定Linux进程的空闲进程时间。我正在写一个Java程序。但是我没有Linux命令。如何编写可以执行此操作的shell脚本?然后我可以从Java执行这个脚本。
答案 0 :(得分:2)
你在这里:
#! /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