flock

How do I use the linux flock command to prevent another root process from deleting a file?

邮差的信 提交于 2019-11-30 23:56:55
问题 I would like to prevent one of my root processes from deleting a certain file. So I came across the flock command, it seems to fit my need, but I didn't get its syntax. If I only indicate a shared lock, it doesn't work: flock -s "./file.xml" If I add a timeout parameter, it still doesn't work: flock -s -w5 "./file.xml" It seems that way, it fits in flock [-sxun][-w #] fd# way. (What is this fd# parameter?) So, I tried: flock [-sxon][-w #] file [-c] command Using flock -s -w5 "./file.xml" -c

PHP check if file locked with flock()?

一曲冷凌霜 提交于 2019-11-30 17:54:24
Will fopen() fail if a file exists, but is currently locked with LOCK_EX ? Or do I have to open it, and then try and set a lock, in order to determine if one already exists? I've also read that flock() will; pause [the script] untill you get the lock for indefinite amount of time or till your script times out http://www.php.net/manual/en/function.flock.php#95257 If so, is it true this 'pause' can be by-passed with; if (!flock($f, LOCK_SH | LOCK_NB)) { // file locked, do something else } flock() doesn't actually prevent you from reading/writing to a file, it only allows you to "communicate" the

Using AppleScript to grab the URL from the frontmost window in web browsers: The definitive list

二次信任 提交于 2019-11-30 06:32:48
问题 I built a [widget][1] that grabs the URL from the frontmost window in Safari, then allows you to shorten it using the tr.im API. Works sweet as. I want to make this more flexible, so am investigating how to grab an URL from other browsers. Here's the AppleScript that works in Safari: tell application "Safari" return URL of front document as string end tell After some digging, I determined that the following might work for Firefox (though one person has told me it doesn't work for him,

php解决抢购秒杀抽奖等大流量并发入库导致的库存负数的问题

谁都会走 提交于 2019-11-30 05:49:22
我们知道数据库处理sql是一条条处理的,假设购买商品的流程是这样的: sql1:查询商品库存 1 if (库存数量 > 0) 2 { 3 //生成订单... 4 sql2:库存-1 5 } 当没有并发时,上面的流程看起来是如此完美,假设同时两个人下单,而库存只有1个了,在sql1阶段两个人查询到的库存都是>0的,于是最终都执行了sql2,库存最后变为-1,超售了,要么补库存,要么等用户投诉吧。 解决这个问题比较流行的思路: 1.用额外的单进程处理一个队列,下单请求放到队列里,一个个处理,就不会有并发的问题了,但是要额外的后台进程以及延迟问题,不予考虑。 2.数据库乐观锁,大致的意思是先查询库存,然后立马将库存+1,然后订单生成后,在更新库存前再查询一次库存,看看跟预期的库存数量是否保持一致,不一致就回滚,提示用户库存不足。 3.根据update结果来判断,我们可以在sql2的时候加一个判断条件update ... where 库存>0,如果返回false,则说明库存不足,并回滚事务。 4.借助文件排他锁,在处理下单请求的时候,用flock锁定一个文件,如果锁定失败说明有其他订单正在处理,此时要么等待要么直接提示用户"服务器繁忙" 本文要说的是第4种方案,大致代码如下: 阻塞(等待)模式 1 <?php 2 $fp = fopen ( "lock.txt" , "w+" ); 3

Running python script with cron only if not running

假装没事ソ 提交于 2019-11-30 03:28:31
I need to run a python script (job.py) every minute. This script must not be started if it is already running. Its execution time can be between 10 seconds and several hours. So I put into my crontab: * * * * * root cd /home/lorenzo/cron && python -u job.py 1>> /var/log/job/log 2>> /var/log/job/err To avoid starting the script when it is already running, I use flock(). This is the script (job.py): import fcntl import time import sys def doIncrediblyImportantThings (): for i in range (100): sys.stdout.write ('[%s] %d.\n' % (time.strftime ('%c'), i) ) time.sleep (1) if __name__ == '__main__': f

PHP check if file locked with flock()?

旧城冷巷雨未停 提交于 2019-11-30 00:57:47
问题 Will fopen() fail if a file exists, but is currently locked with LOCK_EX ? Or do I have to open it, and then try and set a lock, in order to determine if one already exists? I've also read that flock() will; pause [the script] untill you get the lock for indefinite amount of time or till your script times out http://www.php.net/manual/en/function.flock.php#95257 If so, is it true this 'pause' can be by-passed with; if (!flock($f, LOCK_SH | LOCK_NB)) { // file locked, do something else } 回答1:

Release of flock in case of errors?

随声附和 提交于 2019-11-29 13:33:56
Imagine the following Perl code (here in pseudo code): successfully acquired flock for FILEHANDLER # line 1 some error or maybe simply a call to exit() # line 2 close FILEHANDLER (which also releases the lock) # line 3 In this case I wouldn't release the lock, as the Perl script ends in line 2. In that case, is the lock ever released by the operating system? Does it see "hey, the script that acquired the lock crashed" and release the lock? Does it release the lock immediately? Also, is there one Perl instance running for each script, so that it's clear which script crashed/stopped without

php对文件的操作,很全面的整理

江枫思渺然 提交于 2019-11-29 11:22:36
<?php $file = "./test/f1.php"; //===============判断文件能不能读取(权限问题),或者存不存在 if (is_readable($file) == false) { echo "<br/>"; die('no'); } //===============判断文件存不存在 if (file_exists($file) == false) { echo "<br/>"; die('no file'); } //======================================================读取文件内容 //方法一 $dataString = file_get_contents($file); echo "<br/>1"; var_dump($dataString); echo htmlentities($dataString); //方法二,该方法如果文件内容空会报错 $fJuBing = fopen($file, 'r'); //创建指定文件读操作的句柄 $dataString = fread($fJuBing, filesize($file)); fclose($fJuBing); echo "<br/>2"; var_dump($dataString); echo htmlentities(

Using AppleScript to grab the URL from the frontmost window in web browsers: The definitive list

有些话、适合烂在心里 提交于 2019-11-28 19:36:22
I built a [widget][1] that grabs the URL from the frontmost window in Safari, then allows you to shorten it using the tr.im API. Works sweet as. I want to make this more flexible, so am investigating how to grab an URL from other browsers. Here's the AppleScript that works in Safari: tell application "Safari" return URL of front document as string end tell After some digging , I determined that the following might work for Firefox (though one person has told me it doesn't work for him, possibly a conflict with some extension?): tell application "Firefox" set myFirefox to properties of front

【秒杀抢购】关于php高并发解决的一点思路

霸气de小男生 提交于 2019-11-28 11:30:24
实际场景 涉及抢购、秒杀、抽奖、抢票等活动时,为了避免超卖,那么库存数量是有限的,但是如果同时下单人数超过了库存数量,就会导致商品超卖问题。那么我们怎么来解决这个问题呢,我的思路如下(伪代码): sql1:查询商品库存 if(库存数量 > 0) { //生成订单... sql2:同时库存-1 } 当没有并发时,上面的流程看起来是再正常不过了,假设同时两个人下单,而库存只有1个了,在sql1阶段两个人查询到的库存都是>0的,于是最终都执行了sql2,库存最后变为-1,超售了,这不是我们想要的结果吧。 解决问题 解决这个问题比较流行的思路我总结了下: 方法1 用额外的单进程处理一个队列,下单请求放到队列里,一个个处理,就不会有并发的问题了,但是要额外的开启后台进程以及延迟问题,这里暂不予考虑。这里我可使用消息队列,我们常用到Memcacheq、Radis。 比如:有100张票可供用户抢,那么就可以把这100张票放到缓存中,读写时不要加锁。 当并发量大的时候,可能有500人左右抢票成功,这样对于500后面的请求可以直接转到活动结束的静态页面。进去的500个人中有400个人是不可能获得商品的。所以可以根据进入队列的先后顺序只能前100个人购买成功。后面400个人就直接转到活动结束页面。当然进去500个人只是举个例子,至于多少可以自己调整。而活动结束页面一定要用静态页面,不要用数据库