An alternative would always be to have two processes: a supervisor and a worker.
Reorganize all your logic into a run routine (or main or something else). If your real logic discovers that it needs to be restarted, it should exit with a predefined non-zero exit code (e.g. 1).
Then your main script and supervisor will look like this:
if (my $worker = fork) {
In a simple scenario, when you just want to restart once, it might be a bit overkill. But if at any time you should be able to handle other error scenarios, this method may be preferable.
For example, if the exit code is 255, this means that the main script is called die (). In this case, you may need to follow some decision making procedure to restart the script, ignore the error, or exacerbate the problem.
CPAN implements many modules that implement such supervisors. Proc :: Launcher is one of them, and the man page contains an extensive discussion of related work. (I never used Proc :: Launcher, mainly because of this discussion I am contacting him)
source share