Is there an option for the Linux top command where I can filter processes by name and write the CPU usage of that process every second to a log file?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
top&pgrepTo filter the output of
topby process name, you can usepgrepto get a list of PIDs by process name then pass them to the-poption oftop.For example:
Note: the
-d','option delimits the PIDs with commas, which is what is expected by thetop -p.Note 2:
topwill return a failure message if there are no running processes that match the name you specify inpgrep.To write the results of
topto a file, use the-n 1option (only one iteration) and redirect the output to your log file.To do that every second, perhaps a
whileloop with asleepwould do?To timestamp each entry, you can append the output of
date. E.g.