create a lock file in bash to avoid duplicate execution

前端 未结 1 1349
野趣味
野趣味 2021-01-07 06:59

I\'m not very good on bash I\'ve been modifying a code to create a lock file so a cron don\'t execute a second time if the first process hasn\'t finish.

LOCK         


        
相关标签:
1条回答
  • 2021-01-07 07:28
    #!/bin/sh
    
    (
      # Wait for lock on /tmp/lock
    
      flock -x -w 10 200 || exit 127 # you can use or not use -w
    
      #your stuff here
    
    ) 200> /tmp/lock
    

    check man page flock.

    This is the tool for you. And it comes with example in man page :)

    0 讨论(0)
提交回复
热议问题