How to create a background service

how to create a background service to run a script that inserts over 500 records to a doctype