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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

How do I write shell script for Redhat Linux bash shell

From Dev

Bash - How do I write a script that will ssh into a given ip?

From Dev

How do I make a shell script that sends output to a process

From Dev

How do I write an application install shell script?

From Dev

How do I write a shell script to flatten directories?

From Dev

How do I add time to a date in unix shell script?

From Dev

Given a DateTime object, how do I get DateTimeZone in Joda Time?

From Dev

How do I get my shell script to automatically accept prompts?

From Dev

How do I get (only) the http status of a site in a shell script?

From Dev

How do I get screen resolution in a shell script on Linux

From Dev

How do I get current Unix time in nanoseconds in Android Shell?

From Dev

How do I write this using function in shell

From Dev

How do I write a shell script to issue commands and see the output on the screen

From Dev

How do I write a shell script to issue commands and see the output on the screen

From Dev

How do i run a shell script, in a shell script with the menu dialog?

From Dev

how do i get this program to write each line with a given word to a file?

From Dev

Customizing Python IDLE: how do I get tabs and a file browser?

From Dev

How to get one or more elements with a given ID and process once at a time?

From Dev

How can I write a function that does nothing for a given length of time?

From Dev

How do I script a filecount in android shell?

From Dev

How do I use a regex in a shell script?

From Dev

How do I close stdin in a shell script?

From Dev

How do I script a filecount in android shell?

From Dev

How do I wait for a file in the shell script?

From Dev

How do I handle switches in a shell script?

From Dev

How do I add a line to the shell script?

From Dev

How do i write a terminal script to setup ubuntu for me instead of spending days every time?

From Dev

How do I write a script that runs on startup?

Related Related

  1. 1

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

  2. 2

    How do I write shell script for Redhat Linux bash shell

  3. 3

    Bash - How do I write a script that will ssh into a given ip?

  4. 4

    How do I make a shell script that sends output to a process

  5. 5

    How do I write an application install shell script?

  6. 6

    How do I write a shell script to flatten directories?

  7. 7

    How do I add time to a date in unix shell script?

  8. 8

    Given a DateTime object, how do I get DateTimeZone in Joda Time?

  9. 9

    How do I get my shell script to automatically accept prompts?

  10. 10

    How do I get (only) the http status of a site in a shell script?

  11. 11

    How do I get screen resolution in a shell script on Linux

  12. 12

    How do I get current Unix time in nanoseconds in Android Shell?

  13. 13

    How do I write this using function in shell

  14. 14

    How do I write a shell script to issue commands and see the output on the screen

  15. 15

    How do I write a shell script to issue commands and see the output on the screen

  16. 16

    How do i run a shell script, in a shell script with the menu dialog?

  17. 17

    how do i get this program to write each line with a given word to a file?

  18. 18

    Customizing Python IDLE: how do I get tabs and a file browser?

  19. 19

    How to get one or more elements with a given ID and process once at a time?

  20. 20

    How can I write a function that does nothing for a given length of time?

  21. 21

    How do I script a filecount in android shell?

  22. 22

    How do I use a regex in a shell script?

  23. 23

    How do I close stdin in a shell script?

  24. 24

    How do I script a filecount in android shell?

  25. 25

    How do I wait for a file in the shell script?

  26. 26

    How do I handle switches in a shell script?

  27. 27

    How do I add a line to the shell script?

  28. 28

    How do i write a terminal script to setup ubuntu for me instead of spending days every time?

  29. 29

    How do I write a script that runs on startup?

HotTag

Archive