What's the best way to make sure only one instance of a Perl program is running?

后端 未结 3 1110
清歌不尽
清歌不尽 2020-12-13 19:50

There are several ways to do this, but I\'m not sure which one of them is the best.

Here\'s what I can think of:

  • Look for the process using pgrep.
3条回答
  •  半阙折子戏
    2020-12-13 20:14

    Do the old PID file trick.

    • start process
    • see if there is a file called "myprog.PID"
    • check for existence of running proc. with matching PID using kill 0, $pid
    • if prog name of PID proc. matches, complain loudly and exit
    • if not, clean up stale "myprog.PID"
    • create a file called "myprog.PID" and then continue

    HTH

    cheers,

    Rob

提交回复
热议问题