so i have spent the past few days going through the API trying to work out how to write my script and so far i have something that does nothing every second

this is my first time ever using an API as such, i usually work in java and php.... havent used lua in a while

what im trying to do (but cant seem to work out how).
every second
check if entity exists on surface
true| get position of all entity's
run command on each entity
false| do nothing
end
pretty much what i want, but for the life of me i cant work out how to find the entity so that i can begin to work out how im going to run my command.
anyone got any advise please
Thanks in advance