Find the answer to your Linux question:
Results 1 to 2 of 2
I currently have several processes alternating and sometimes the system gets stuck on them. Is there a way that allows me to iterate through these processes and their properties (runtime/start ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2012
    Posts
    1

    Quick question about running through running processes in bash


    I currently have several processes alternating and sometimes the system gets stuck on them. Is there a way that allows me to iterate through these processes and their properties (runtime/start date/end date) every few minutes or so? I don't know their PIDs since there are several processes within the entire job.
    EDIT:
    Forgot to mention that these processes stop running and begin running at different times, so the PIDs of the entire job are constantly changing.
    EDIT2:
    I forgot the grep command existed. Is there a way to grep for a PID that has been running for more than 20 minutes based on runtime alone?
    Or, barring that, getting the PID of a *.exe process, finding that runtime, comparing it to 20 minutes and, if it exceeds, killing it?
    Last edited by Bolanok; 07-16-2012 at 06:40 PM.

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by Bolanok View Post
    I currently have several processes alternating and sometimes the system gets stuck on them. Is there a way that allows me to iterate through these processes and their properties (runtime/start date/end date) every few minutes or so? I don't know their PIDs since there are several processes within the entire job.
    EDIT:
    Forgot to mention that these processes stop running and begin running at different times, so the PIDs of the entire job are constantly changing.
    EDIT2:
    I forgot the grep command existed. Is there a way to grep for a PID that has been running for more than 20 minutes based on runtime alone?
    Or, barring that, getting the PID of a *.exe process, finding that runtime, comparing it to 20 minutes and, if it exceeds, killing it?
    You can use the excellent guide here to determine the length (in seconds) of a given process, based upon its pid.

    i modified it a bit for my own needs, here it is:
    Code:
    #!/bin/bash
    
    # get required command line arg (the pid)
    [ $# -ne 1 ] && echo "Usage: $0 <PID>" && exit 1
    PID=$1
    
    # make sure the process is running
    ! [ -d /proc/$PID ] && echo "/proc/$PID: No such directory" && exit 1
    
    # process name of pid
    process=$(awk '$1 ~ /Name/{print $2}' /proc/$PID/status)
    
    # freq of the system timer (use timer-checker.sh to determine, or look for CONFIG_HZ in kernel .config)
    #HZ=200
    # nope, that's way off, must use "100" for some reason...
    HZ=100
    
    # determine the start-time of a process since boot time
    ((p_seconds_since_boot = $(cat /proc/$PID/stat | cut -d " " -f 22) / $HZ ))
    echo Seconds since boot: $p_seconds_since_boot
    
    # To get the absolute start time I am adding the boot time of the Linux system from the /proc/stat file to the start time of the process.       
    boottime=$(grep btime /proc/stat | cut -d " " -f 2)
    ((p_starttime = $boottime + $p_seconds_since_boot ))
    echo Process start time: $p_starttime
    
    # current time in seconds since epoch
    now=$(date "+%s")
    echo "Current time (sec): $now"
    
    # To get the runtime in seconds of the process I just subtract the absolute start time of the process from the actual time.
    (( p_runtime = $now - $p_starttime ))
    
    echo -e "\nProcess \"$process\" (pid $PID) running for $p_runtime seconds"
    which convert_sec >/dev/null 2>&1 && convert_sec $p_runtime
    echo -e "Started: $(date -d @$p_starttime)\n"
    
    # compare output to ps -o pid,cmd,etime -p $PID
    ps -o pid,cmd,etime -p $PID
    note: if you don't know the pid of your process, try:
    Code:
    pidof -x <process_name>
    FYI, in the code, it says it uses the kernel system timer (in HZ) to convert jiffies to seconds. on my system, the timer is set to 200/250HZ, but that would yield incorrect results - that is why I hard-coded it to 100, which seems to work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •