You could setup your IPsec tunnels better and get the same net effect without a script.
For example, if it's a tunnel mode IPsec connection, if you setup P2 entries with remote hosts to ping, that normally will bring them back up automatically.
For IPsec VTI mode, if you set the child SA close action to "reconnect" on one side that will make them reconnect when they disconnect.
There wouldn't normally be an IKE/P1 issue unless you don't have working DPD on both sides.