How do I make sure my bash script isn't already running?

后端 未结 13 939
無奈伤痛
無奈伤痛 2020-12-31 05:56

I have a bash script I want to run every 5 minutes from cron... but there\'s a chance the previous run of the script isn\'t done yet... in this case, i want the new run to j

13条回答
  •  半阙折子戏
    2020-12-31 06:41

    If you want to check the process's existence, just look at the output of

    ps aux | grep your_script_name

    If it's there, it's not dead...

    As pointed out in the comments and other answers, using the PID stored in the lockfile is much safer and is the standard approach most apps take. I just do this because it's convenient and I almost never see the corner cases (e.g. editing the file when the cron executes) in practice.

提交回复
热议问题