How to set time limit only for one php file? I have a script which walks through database table, makes some checks and based on them it makes notifications for website users. It works for a pretty long time. Currently in dev it takes a minute for every user.  So I think I need to set max execution time(.htaccess, php etc) to 0 for this one.

Currently it's launched from "drupal page with form", later I'll be launching it with cron. Drupal is not letting me set set_time_limit(0) because it's not the beginning of file where I can put it. But I don't want to set_time_limit(0) for whole website.

Is there a way bypass this limitaion? Maybe some external script execution? I'd like to remain Drupal functionality in it.
Don't do this. While it is possible to do this, using set_time_limit(0) is giving your server free rein to execute indefinitely; you're setting yourself up for server instability. 

If you have a large workload that you need to execute in a Drupal context, use its Queue API. This API will let you queue up a list of items to process (i.e. the table rows you're checking) and process them with a queue worker (i.e. the task that "does the checks" and generates user notifications).

You can find many examples of creating a queue and a queue worker by searching Google for [drupal 8 queue api].