For troubleshooting purpose, I'd like to record PID and its command every 15s and write them to a text file.
The desired output: TimeStamp, PID, Command
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
This will get you you the three columns you want:
$ ps -hopid,comm | perl -anle'print time, ", $F, $F"' 1541626566, 6496, gedit 1541626566, 7513, bash
You can limit or extend the scope of
ps, ie. what processes it lists. Then you put this in a loop with redirection.
while true; do echo x; sleep 15; done > out
echo x with the command proper and
out with the file name you choose.
As for the command producing the information, here's a run-down.
-hturns off the header line and
-ostands for the output (PID and command).
perl -anle'print time, ", $F, $F"'. Here
perlis Perl. The
-anleflags reprent correspondingly:
a- load input into an array,
n- take care of new lines,
l- process each single line, and finally
e- execute the code that follows.
'print time, ", $F, $F"'. First print the time stamp, then the first column of the array that holds the input material, and then the second.