Two things to note:
1. There is a switch there to a generator/machine that, when flipped, will cause electricity to spark across one of the holes.
2. The worm will always come out of whichever hole you are closest to, just before it starts shaking/rumbling the ground.
Identify which hole the sparks will shoot across (it's the wall/opening that the machine has its back to. Now go stand near/to the side of (you cannot stand directly in front of it because of the hole) until the ground starts shaking.
At that point, the worm is guaranteed to come through that hole. From here, you just need to time flipping the switch, causing the electricity to jolt out, when the worm is out of the hole.
Once you hear/see the rumbling go run to the machine. Maybe count off two or three seconds in your head and then flip the switch - voila, you have fried worm.
You may need to try two or three times to get the timing right, but just position the worm to come from that one hole and fry it.
If you do miss the timing at all, be careful to move away if the rumbling starts while you're standing next to the switch - the worm is about to come down from above.