Skip to content

Instantly share code, notes, and snippets.

@bingoohuang
Created June 27, 2018 15:39
Show Gist options
  • Save bingoohuang/6e9d675abcb1fbca9f5f9864125aa2d3 to your computer and use it in GitHub Desktop.
Save bingoohuang/6e9d675abcb1fbca9f5f9864125aa2d3 to your computer and use it in GitHub Desktop.
ps output lstart in ISO format like YYYY-MM-DD HH:MM:SS
# from: https://unix.stackexchange.com/questions/401785/ps-output-with-iso-date-format
ps -eo lstart,pid,cmd --sort=start_time|sed '1d' | awk '{c="date -d\""$1 FS $2 FS $3 FS $4 FS $5"\" +\047%Y-%m-%dT%H:%M:%S\047"; c|getline d; close(c); $1=$2=$3=$4=$5=""; printf "%s\n",d$0 }'
#2018-04-23T15:38:18 1 /sbin/init
#2018-04-23T15:38:18 2 [kthreadd]
#2018-04-23T15:38:18 3 [migration/0]
#2018-04-23T15:38:18 4 [ksoftirqd/0]
#2018-04-23T15:38:18 5 [stopper/0]
#2018-04-23T15:38:18 6 [watchdog/0]
# from https://stackoverflow.com/questions/29203110/invoking-date-command-inside-awk-string-with-a-formatting
# As for why store the command in a variable - because you have to close it after you use it and it must be spelled exactly the same way in the close command as it was when you opened the pipe.
# Compare:
cmd = "date +%a -d \"" $1 "\""
cmd | getline
close(cmd)
# vs:
"date +%a -d \"" $1 "\"" | getline
close("date +%a -d \"" $l "\"")
# and take an extremely close second look to spot the bug in the 2nd version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment