Brandon
Brandon

Reputation: 11

reading a file created from a cron job

I have a small program that runs and generates a new text dump every 30sec-1min via cron.

program > dump.txt

However I have another PHP web program that accesses the text dump in a read-only mode whenever someone visits the webpage. The problem is that I believe if someone accesses the website the very second the cron job is running the webpage may only read half the file because i think Linux does not lock the file when > is used.

I was thinking of doing:

echo "###START###" > dump.txt
program >> dump.txt
echo "###END###" >> dump.txt

Then when the PHP webpage reads the dump in memory, I could do a regex to check if the start and end flags are present and if not, then to try again until it reads the file with both flags.

Will this ensure the files integrity? If not, how can I ensure that when I read dump.txt it will be intact.

Upvotes: 0

Views: 549

Answers (1)

DylWylie
DylWylie

Reputation: 61

Rather than create the file in the directory, why not create it somewhere else? Then after the data's been written, just move it into the webroot and overwrite the previous set.

Example:

sh create_some_data.sh > /home/cronuser/my_data.html
mv /home/cronuser/my_data.html /var/www/

Upvotes: 4

Related Questions