I think that you're looking for sar. SAR stands for System ActivtyActivity Report. It's used in unixUnix-like operating systems to report abouton CPU, memory, and IO usage, collected by sysstat.
Then, sysatat can be configured to Monitor individual processes. Link
How often it collects, and how long sarSAR keeps reports, is decided on the first setup.
You just want to note that such a data collection is not "free""free," so I won't keep it on on production servers.
After it is configured, it will be easy for you to extract datasdata from reports in your script by using the sar command, grep, and awk.
You didn't specifiedspecify what OS are you are working on, so I encourage you to search how to set up sar/sysstat on your distro.