I have a PHP web-page that writes some values inside configuration.txt
. Between those values there is a URL.
Next I have a simple bash script that reads the URL from configuration.txt
and puts the response body in another url_response.txt
file once every 2 seconds:
while [ 0 ]
do
line=$(head -n 1 ../configuration.txt)
wget -q -i $line -O url_response.txt
sleep 2
done
This is how I've implemented flock()
in PHP:
$currentSettingsFile = fopen($configFilePath, "w");
if(flock($currentSettingsFile, LOCK_SH)) {
// echo "<br>Writing in $configFilePath<br>";
foreach ($arrayInputs as $key2 => $value2) {
if($value2 != '')
fwrite($currentSettingsFile,$value2.PHP_EOL);
}
flock($currentSettingsFile, LOCK_UN);
fclose($currentSettingsFile);
}
I have read online some things, but I don't seem to find what I actually want. I did not find any tutorial explaining what I want to do with flock()
. The ones that I found don't do what I want it to do:
https://linux.die.net/man/1/flock
https://blog.famzah.net/2013/07/31/using-flock-in-bash-without-invoking-a-subshell/
Need an in-depth explanation of how to use flock in Linux shell scripting
I am not a prolific Linux user and as such I have very little knowledge of commands and things like -n -i -l -a
etc and that is why I don't understand much out of those guides.
I would like to do something like this:
while(true)
{
if (flock("url.txt",1) && flock("url_response.txt", 1))
{
wget -q -i url.txt -O url_response.txt
flock("url.txt",8) // unlock
flock("url_response.txt", 8) // unlock
sleep 2
}
}
I am aware that what I've written above is not correct.
Also, does the script needs anymore lines added to it if this script is going inside a c++ program ?
P.S: I haven't yet learned how to integrate a bash script inside a c++ program, but I'm taking it one step at a time. I will read here: How to run a bash script from C++ program