That might be caused by corrosion at the battery contacts. Check whether the area around the contacts have any signs of weird liquid stains, and if there is clean with an isopropyl alcohol wipe. If there's rust, you might want to get some sandpaper and gently rub the contacts so they would work. If the corrosion is on the motherboard, take it off and use a toothbrush dipped in isopropyl alcohol to brush at the corroded area and hopefully it won't be a problem anymore.
Nintendo 3DSs will also shut down if it did not detect the upper half's cables and components(3d slider, speaker, camera, screens). If the cables are broken, then it might work for a while and shut down randomly. Or it could be poorly connected ZIF cables or broken connectors that temporarily disconnect when you shake it. You should probably take the motherboard out and check for both corrosion and broken ZIF connectors, and when you're putting it back together make sure to connect the cables properly. If the problem still persists, then the flex cables are probably broken. Get yourself a new 3d slider cable and see if it works.
crwdns2934105:0crwdne2934105:0
crwdns2934113:0crwdne2934113:0
crwdns2915270:0crwdne2915270:0
crwdns2889612:0crwdne2889612:0
0