How to trace cpu/memory usage for a process

I don't know when the process will start and end, I need write a script to trace it's cpu/memory usage when it is runing. How to write this script?

then you have to do sampling ...

execute your script now and then, and log the output.

how to write a script to trace cup/memory usage for a process?