#!/bin/bash ##Alias to show how long a process has been running. #+Thank Ron for this absolute banger. #source /root/.bash_profile > /dev/null 2>&1 #SLOW source "$SCRIPTS/Okiru" arg0=$(basename "$0") #Show help if arguments are misused usage() { exec 1>2 # Send standard output to standard error help exit 1 } flag_error() { echo -e "$arg0: $*." >&2 help exit 1 } help() { echo "$arg0 - show how long a process has been running" echo " {-p|--process-id} # -- Process ID to check" echo " {-h|--help} -- Print this help message and exit" exit 0 } #Pass arguments to the script declare -a PIDS=() flags() { if [[ $# == "0" ]]; then help exit 1 elif [[ $# == "1" ]]; then if [[ $1 =~ ^[0-9].*$ ]]; then PIDS+=("$1") export PIDS elif [[ $1 =~ ^[a-z].*$ ]]; then procs=$(ps aux | grep $1 | grep -v grep | grep -v "$(pwdx $$ | awk '{print $NF}')") # exclude the path of the current script PID ^ if [[ -n "$procs" ]]; then for proc in "$procs"; do proc_pid=$(echo "$proc" | awk '{print $2}' | grep -v "$(pwdx $$)") if [[ -n $(ps aux | grep "$proc_pid") ]]; then PIDS+=("$proc_pid") export PIDS fi done fi elif [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then help else flag_error "Invalid process ID: $1" fi else while test $# -gt 0 do case "$1" in (-p|--process-id) shift [ $# = 0 ] && flag_error "No process ID specified!" if [[ $1 =~ ^[0-9].*$ ]]; then PIDS+=("$1") export PIDS else flag_error "Invalid process ID: $1" fi shift;; (-h|--help) help;; (*) help;; esac done fi } flags "$@" for pid in ${PIDS[@]}; do elapsed=$(ps -p $pid -o etime | grep -v "ELAPSED") cmd=$(ps -o command $pid | tail -1) if [[ -z $elapsed ]]; then printf "No data for PID ${RED}$pid${STOP}.\n" continue fi if [[ -n $(echo $elapsed | grep "-") ]]; then days=$(echo $elapsed | awk -F- '{print $1}') hours=$(echo $elapsed | sed -e "s/$days-//g" | awk -F: '{print $1}') minutes=$(echo $elapsed | sed -e "s/$days-//g" | awk -F: '{print $2}') seconds=$(echo $elapsed | sed -e "s/$days-//g" | awk -F: '{print $2}') printf "Process ${PURPLE}$pid${STOP} (${YELLOW}$cmd${STOP}) has been up for ${BLUE}$days${STOP} days, ${LIGHT_BLUE}$hours${STOP} hours, ${CYAN}$minutes${STOP} minutes and ${LIGHT_CYAN}$seconds${STOP} seconds.\n" elif [[ -z $(echo $elapsed | grep "-") ]]; then #If a process has been running for an hour or longer, output is 3 fields if [[ $(echo "$elapsed" | awk -F: '{print NF+1}') == 4 ]]; then hours=$(echo $elapsed | awk -F: '{print $1}') minutes=$(echo $elapsed | awk -F: '{print $2}') seconds=$(echo $elapsed | awk -F: '{print $2}') printf "Process ${PURPLE}$pid${STOP} (${YELLOW}$cmd${STOP}) has been up for ${BLUE}$hours${STOP} hours, ${LIGHT_BLUE}$minutes${STOP} minutes and ${CYAN}$seconds${STOP} seconds.\n" #If running for less than an hour - only 2 fields elif [[ $(echo "$elapsed" | awk -F: '{print NF+1}') == 3 ]]; then minutes=$(echo $elapsed | awk -F: '{print $1}') seconds=$(echo $elapsed | awk -F: '{print $2}') printf "Process ${PURPLE}$pid${STOP} (${YELLOW}$cmd${STOP}) has been up for ${BLUE}$minutes${STOP} minutes and ${LIGHT_BLUE}$seconds${STOP} seconds.\n" fi fi done