Is there any way to get the PID of the top CPU/MEM usage, I need to use that in a script, top command doesn't seem to work for it.
ps has many options for displaying and sorting details about processes.
ps hax o pid k -time | head -n 1 will give you the PID of the process with the most CPU time used.
See the man page for full details. The man page functions well enough as a reference but is not a tutorial - IMO your best option is to read and experiment until you understand what it can do and how to use it best. AFAIK, there is no tutorial-style documenation for
ps available anywhere.
Try something like this:
top -b -n1 | tail -n+8 | sort -nr -k9