How to make custom plugin run on demand?

You can run scheduled events using wp_cron.

To trigger it manually you’ll need to create an admin screen and create a button that triggers the function. To do that read up on Creating a Plugin and Adding Admin Menus.