How do I write a shell script to get a given process idle time?

user3680477

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.

AnOldSoul

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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Given the function `decode`, how can I do the inverse and write a function to `encode`?

来自分类Dev

How to get the process start time in PowerShell

来自分类Dev

How do I write a custom module for AngularJS?

来自分类Dev

How do I get Time Machine working again after changing my hard drive?

来自分类Dev

Bash Shell Script Process Each Directory in Home

来自分类Dev

how do I find the closest value to a given number in an array?

来自分类Dev

How do I write a Solr FunctionQuery to boost documents with future dates?

来自分类Dev

How do i write the integers that in this file to the mynumbers.txt file?

来自分类Dev

How do I use backend variable in ejs script section?

来自分类常见问题

How do I find all installed packages that depend on a given package in NPM?

来自分类Dev

How do I open up a random url from a list of url's in a new time each time it is clicked?

来自分类Dev

I have a few function overloads, each of which takes a different subclass; how do I check which function to use for a given object?

来自分类Dev

How do I process broadcasts that were sent while my activity was stopped?

来自分类Dev

GIT: How do I add a file to the first commit (and rewrite history in the process)?

来自分类Dev

How do I find out which process is using so much of my RAM?

来自分类Dev

how to write a script to read double quotes in powershell

来自分类Dev

How to select read/write time and again?

来自分类Dev

How to stop at in IDLE breakpoint

来自分类Dev

How do I get the nth derivative in my scheme program?

来自分类Dev

Swift: How do I get access to the navigationController in AppDelegate

来自分类Dev

How do I get the Area (or the path) of where a View is being scaffolded?

来自分类Dev

How do I get my aliases to have correct completion?

来自分类Dev

How do i get MS EXCEL to Connect to MYSQL Database

来自分类Dev

How do I get MVC to find my controller path?

来自分类Dev

How do I get Avg and Sum in Spark RDD

来自分类Dev

How do I get my nested if statement to work in jQuery

来自分类Dev

How do I get Ruby to search for a pattern on the tail of a local file?

来自分类Dev

How do I get the computer's current language in Go?

来自分类Dev

How do I get a value from an associative array using Swift

Related 相关文章

  1. 1

    Given the function `decode`, how can I do the inverse and write a function to `encode`?

  2. 2

    How to get the process start time in PowerShell

  3. 3

    How do I write a custom module for AngularJS?

  4. 4

    How do I get Time Machine working again after changing my hard drive?

  5. 5

    Bash Shell Script Process Each Directory in Home

  6. 6

    how do I find the closest value to a given number in an array?

  7. 7

    How do I write a Solr FunctionQuery to boost documents with future dates?

  8. 8

    How do i write the integers that in this file to the mynumbers.txt file?

  9. 9

    How do I use backend variable in ejs script section?

  10. 10

    How do I find all installed packages that depend on a given package in NPM?

  11. 11

    How do I open up a random url from a list of url's in a new time each time it is clicked?

  12. 12

    I have a few function overloads, each of which takes a different subclass; how do I check which function to use for a given object?

  13. 13

    How do I process broadcasts that were sent while my activity was stopped?

  14. 14

    GIT: How do I add a file to the first commit (and rewrite history in the process)?

  15. 15

    How do I find out which process is using so much of my RAM?

  16. 16

    how to write a script to read double quotes in powershell

  17. 17

    How to select read/write time and again?

  18. 18

    How to stop at in IDLE breakpoint

  19. 19

    How do I get the nth derivative in my scheme program?

  20. 20

    Swift: How do I get access to the navigationController in AppDelegate

  21. 21

    How do I get the Area (or the path) of where a View is being scaffolded?

  22. 22

    How do I get my aliases to have correct completion?

  23. 23

    How do i get MS EXCEL to Connect to MYSQL Database

  24. 24

    How do I get MVC to find my controller path?

  25. 25

    How do I get Avg and Sum in Spark RDD

  26. 26

    How do I get my nested if statement to work in jQuery

  27. 27

    How do I get Ruby to search for a pattern on the tail of a local file?

  28. 28

    How do I get the computer's current language in Go?

  29. 29

    How do I get a value from an associative array using Swift

热门标签

归档