Shimano Hub LED lights working sporadically
If I understand you, you have a hub dynamo that powers lights in the front and in the rear.  You don't make it clear whether you have a battery or not; if the lights stayed on after a minute or so (when everything worked properly) when the bike wasn't moving, then you have a battery.  If the lights fade and die a minute or so after you come to a stop, and then work again when you get rolling again, then you don't have a battery.  It's very unlikely that you have a battery next to each light, because that would make the system more expensive for very little benefit.

Your problem is most likely a loose connection between the light and the power source.  If you have a battery, then the battery is the power source; if you don't have a battery, then the power source is the hub dynamo.  An electronics technician would look at each connection between the light and the power source.  You could have a loose contact, or a corroded contact.  If the contacts are corroded, you can often remove the corrosion temporarily with a rubber eraser, a piece of fine-grained sandpaper, or an emery board that ladies use to file their nails; try the gentlest options first, because if you remove the metal plating on the contact, then the contact will corrode again very quickly.

Corrosion is often caused by moisture getting into the contacts; if the connector has a gasket, then check to see if the gasket is sealing.  If the connector is supposed to keep water out, but doesn't any more, then the usual fix is to replace the entire connector, which takes electronics repair expertise.  If your lights are several years old, then corrosion could have happened naturally.

Good luck finding and fixing the problem.  If you can't fix it, your LBS or an electronics repair shop can probably do the job.