how to write a function to show lists of running processes or while loop functions in php


Please i have a while loop function that am using to insert data into my database periodically, for some reasons i found out that while loop is more easy to use than cronjob, while loop runs without anyone having to visit the site, but my problem is that once a while loop is started it keeps running even if you delete the codes from your php file, it keeps running, and if you amend the code with new values and start it again both the old loop and the new one keeps running. My question is how can i write a php function that will list out all current running loop processes in my webhost or server and then use another code to end or stop or remove it from running?

Below is my code

$  date = date('Y-h-m H:i:s'); $  counter = 0; while($  counter < 10){      $  wpdb->query("INSERT INTO a_boy (username, password) VALUES ('cash', $  date)");     $  counter + 1;     sleep(60); }