Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use flock File Lock to resolve crontab conflicts in linux

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how linux uses flock file lock to solve crontab conflict problem", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's learn how to use flock file lock to solve crontab conflict problem in linux.

Linux crontab command, you can regularly perform operations, the minimum cycle is performed once per minute. For crontab implementation per second, please refer to my previous article "Linux crontab implementation per second"

Now there is a problem. If the task is set to execute once per minute, but it may not be completed within one minute, then the system will execute the task again. Two identical missions are being performed.

For example:

Cycle 300 times, sleeping for 1 second per cycle. Execution takes 300 seconds or 5 minutes to complete.

Set crontab to execute every minute

* * * * * php /home/fdipzone/php/test.php >> /home/fdipzone/php/test.log

After 2 minutes, use ps aux| grep test.php Looking at it, you can see that there are two test.php processes executing.

Three minutes later, you see three test.php processes executing.

We hope to execute the previous task and then execute the next task. If the previous task is not executed, the task will not be executed until the next cycle. If the previous task is executed, the next task can be executed.

Use linux flock file lock to achieve task locking and resolve conflicts

Format:

flock [-sxun][-w #] fd#

flock [-sxon][-w #] file [-c] command

option

-s, --shared: acquire a shared lock

-x, --exclusive: acquire an exclusive lock

-u, --unlock: remove a lock, usually unwanted, the script will automatically discard the lock after execution

-n, --nonblock: If the lock is not acquired immediately, fail instead of waiting

-w, --timeout: Wait for specified time if lock is not acquired immediately

-o, --close: Close the descriptor of the file before running the command. Used if the command spawns child processes that are not controlled by locks

-c, --command: Run a single command in the shell

-h, --help Show help

-V, --version: Display version

Continue back to the first test.php, file lock uses exclusive lock, if lock fails do not wait. The parameter is-xn

* * * * * flock -xn /tmp/mytest.lock -c 'php /home/fdipzone/php/test.php >> /home/fdipzone/php/test.log'

In this way, when the task is not completed and the next task judges that/tmp/mytest.lock is locked, the current task is ended and the next cycle is judged again.

At this point, I believe that we have a deeper understanding of "how to use flock file lock to solve crontab conflict problems in linux," may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report