1

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

bleah1
  • 471
  • 3
  • 18
  • 1
    What do you want to do? Why do you need flock? `what I actually want` - what do you actually want? `flock("url.txt",1)` - this is invalid, `flock()` takes opened file descriptor and `LOCK_SH`, `LOCK_EX` or `LOCK_UN` arguments, not filename and `1`. – KamilCuk Sep 06 '19 at 10:53
  • @KamilCuk I have edited the question. I know the declaration of `flock()` in c++ : `extern int flock (int __fd, int __operation) __THROW;` But also, in c++ we have `#define LOCK_SH 1 /* Shared lock. */`. So you can use 1 instead of `LOCK_SH` if you want. What I have written is just to express what I would like to make. – bleah1 Sep 06 '19 at 11:11
  • 1
    Don't read system headers. Use [man pages](http://man7.org/linux/man-pages/man2/flock.2.html), they are the documentation for functions. `in c++ we have` - you shouldn't concern yourself with what is `LOCK_SH` is. it is, let it be. Do `flock(fd, LOCK_SH)`. Ok, so you have some php script that does something. How is it related to the bash program? How is it all related to C++ program? How is it all related to flock? Why do you use flock in php program? Do you want to synchronize something? Block something? – KamilCuk Sep 06 '19 at 11:22
  • @KamilCuk I have written an explanation of who does what in the previous edit... It's also found here: https://stackoverflow.com/questions/57790149/how-can-i-avoid-errors-when-php-c-and-shell-script-try-to-access-the-same-fil – bleah1 Sep 06 '19 at 11:25

0 Answers0