0

I'm a bash scripting rookie and have been googling this scenario for about six hours to no avail. I have found segments of what I need but no comprehensive solution.

Here is the problem: I need to write some code that will be able to check that 3 files exist (file1.txt, file2.csv and file3.log). Before proceeding however, I need to confirm that file3.log has stopped writing (this is to confirm that the script that creates the 3 files has finished successfully).

checking the existence of these files seems easy enough using something like:

if [ -f -e /path/to/file/file1.txt && /path/to/file/file2.csv && /path/to/file/file3.log]
then
# somehow check the exit status of file3.log
if [$? !=0]
#somehow loop (maybe every 5 minutes) until $?=0
done

OR

I was also thinking can I just avoid all of this together and just check the exit status of the initial script? but I wasn't sure because if there was some sort of failure and the files weren't created then that would be a serious problem because the next part of the scanning process takes about 2 days...

Sorry if this might seem like a stupid question, I'm so deep in the google "if-until-exit-zero-else..." hole that I have no idea what to do from here.

3
  • is file3.log a program, or the output of a program? Commented Jul 24, 2019 at 8:48
  • Its the output of the initial script @ctrl-alt-delor
    – Geordeaux
    Commented Jul 24, 2019 at 10:34
  • You can't check the exit status of a file, you can see when it is written to, created, deleted etc. But it has no concept of exit to check. You have to check the exit status of the program that is writing to it. However if the writer writes a distinctive word at the end (this is an inefficient test), or keeps the file open, until the process exits. Then you can monitor it. Commented Jul 24, 2019 at 10:58

1 Answer 1

0

To check that a named program or a script has finished, use the command:

wait $(pgrep program-name)
3
  • Note: this requires program-name to be a child of the current shell. Commented Jul 24, 2019 at 9:23
  • Thanks! Ill give this a try and get back to you!
    – Geordeaux
    Commented Jul 24, 2019 at 10:36
  • Thanks harry this worked perfectly without any messing around
    – Geordeaux
    Commented Jul 24, 2019 at 23:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .