Flexfor.com - Strona Główna tematykę forum określają wybrane tagi,
wybierz kilka i zacznij rozmawiać !
!
Musisz się zalogować, by móc dodawać wpisy na forum.
wybrane tematy :  

flock unix

boggy
  Thanks ;) 1256 dni temu
BRIRLNOTO
nie zarejestrowany
89.149.217.190
  Hello
I've just registered to say your site is very useful and nicely done!
Thank you very much for your work.

Cheers!

Sorry for offtopic.
1260 dni temu
rhethej
nie zarejestrowany
94.102.60.170
  U 1279 dni temu
MilkaJunkies
nie zarejestrowany
95.24.12.76
  Who enjoys,
Decided to buy a product in shope but they take a pay E-CHECK
Who benefited from this scheme? It is safe?
You must have your bank account?
1285 dni temu
redbusystone
nie zarejestrowany
66.232.107.138
  water watch joke go dog dog 1305 dni temu
Meowmeowz
nie zarejestrowany
79.111.243.218
  Теперь этот форум принадлежит мне. Все вопросы и предложения рассматриваются в icq 477345812 1311 dni temu
Meowmeowz
nie zarejestrowany
79.111.243.218
  Теперь этот форум принадлежит мне. Все вопросы и предложения рассматриваются в icq 477345812 1312 dni temu
anonymous
nie zarejestrowany
62.179.45.237
  How to force only one Unix Perl, Python or Ruby script to be running at a time ?


Let’s say you have the script that is executed every minute via cron. Most likely you’ve tested it, whether it is executing in less than one minute or not, but have you tested it while your box CPU resources are consumed at 100% ? Have you tested it how long it will be executing when your system is swapping ?

What if it will be running longer than the scheduled time interval is ? Then you’re likely to hit CPU, memory, number of processes or other resource’s limits.

Such issues can happen, and if your scripts are not immune to such situations, they’ll act just like nails in your system’s coffin. Especially if they’re running with low priority ( like “nice -n 19 ./your_script.sh” for example ).

Securing scripts that way could also be needed, when more then one execution at a time could have bad influence on information consistency - it may be needed to have such executions atomic.

Here are some code examples of how the scripts could be secured. Those examples are showing the trick to lock the script file itself.

For Perl scripts :

#!/usr/bin/perl
use Fcntl qw(:flock);

open SELF, “< $0″ or die;

flock SELF, LOCK_EX | LOCK_NB or exit;

For Python scripts :

#!/usr/local/bin/python
import fcntl
import sys

f = open(sys.argv[0], ‘r+’)
try:
fcntl.flock(f.fileno(),fcntl.LOCK_EX|fcntl.LOCK_NB)
except IOError:
sys.exit(0)

For Ruby scripts :

#!/usr/local/bin/ruby

f = File.open($0, File::RDONLY)
f.flock File::LOCK_EX | File::LOCK_NB or exit

Those snippets are showing the technique of creating exclusive lock on the script file itself. If another instance is trying to obtain that lock, it is not getting it and exits the execution. After the script quits, the lock is released.

You can just place similar code in the beginning of your script to not let it to run more than once at a time.
1661 dni temu
« Najnowsze wpisy | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1
wybrane tematy (tagi)
[-] flock
[-] unix

dodaj temat (tag) :

dodaj nowy:


bywalcy tego forum :
Meowmeowz
redbusystone
rhethej
BRIRLNOTO
MilkaJunkies
boggy

dodatkowe informacje o
flock unix :

gery.pl