Homemade activity monitor-ish

Hello - I didn't find this with search, but I'm guessing it's out there somewhere. Apologies for reposting if that's the case.

I'm shell scripting on a 10.5.8 Mac.

I want to make a program that runs in the background and periodically logs computer use, storing things like - cpu usage, programs running, etc. I know I can get the pid of the most recently called process with $!, but is there a way to get the pid of any arbitrary process, or even to see all pids in use at one time? Do all processes have single, unique pids?

Is it possible to see the number of windows open at any one time?

Thanks.

Yes, all processes have a single unique pid. The ps command might help you. Not sure if this is the same switches on Mac, but you can get for example process stati with:

ps aux

where the STAT column might be interessting for you. The values are explained in the man page for ps so you might grep for the ones you'd like to see.
Where you can see the number of open windows, I do not know. But maybe you can find them in some X log or also as processes or threads.

Thanks so much. ps looks great.