The force exerted by the spring is to attain its equilibrium position. If we push the spring inwards, a force is generated to bring it to its equilibrium position. If we pull the spring outwards, a restoring force is generated on the spring, which pulls it inwards towards the equilibrium position. If no force is applied to the spring, it remains in the equilibrium position. Let us consider a spring that is fixed at one end.
These movements of pendulums are called oscillations, which show simple harmonic motion. It swings to and fro about its mean position where the string and the bob undergo the motion. A pendulum undergoes simple harmonic motion. The motion of an object that moves to and fro about a mean position along a straight line is called simple harmonic motion.