How to make Load balancer to wait for custom program to finish running?
Use the health check capabilities to ensure that your instance is not in service before your custom program is ready.

If you have a classic ELB, you can point to an specific enpoint, for example, /health_check.php that performs the necessary checks in your application to ensure that your custom program is ready. Then, when a new instance is attached to the load balancer, it will perform the health check action that you have configured an expects a right behaviour of it. If not, the health check fails and will continue to perform new health checks in the configured intervals until instance become in service.

On best practice for web servers behind a load balancer, for example, is to add an specific endpoint that checks that all the necessary pieces for your application is running and in service, that is database, storage, caching processes or whatever you new or consider critical for your system.

The official AWS documentation is very useful for this.

Hope this helps