Wait/if Generated kind of operation

Dear All,

I am running a c++ code that will generate various output files.
After generating each and every file I wanted to process that file and remove using shell.

How can I do that?

example for post script:

#!/bin/sh
for dirName in ab ac ad ae;
do
    grep -r -n "Max_x"$dirName/file;
    rm $dirName/file;
done

above code will work for completed simulation.

Now lets say I want to run this scritp along with my c++ and do the same.
is there any wait function in shell to do this job?

example:

#!/bin/sh
for dirName in ab ac ad ae;
do
    wait till $dirName/file to be found
    if($dirName/file found)
    do
        grep -r -n "Max_x"$dirName/file;
        rm $dirName/file;
    done
done

Thanks & Regards,
linuxUser_

Hello Linuxuser_

We can use sleep command for put a wait to execute the next process, but it is better you can check the previous command's satus like it is completed or not by a condition as sleep will wait only for mentioned timings by the user. kindly try it and let us know if you face any issues. Also refer man sleep and man test for same too.

Thanks,
R. Singh

1 Like

Dear Ravinder,

Thanks for the reply. I will check it out. One quick question based on your reply.
will sleep break from the wait after it satisfies the condition?
I mean, lets say time for generating one new file is 5 hrs and I have given sleep time as 10hrs.will it quit from the loop after finding the required file and restart with sleep time 10 hrs?

Thanks & Regards,
linuxUser_

hi,

you can use this for wait while the file exist,

while ! [ -e pathOfYourFile ];do sleep 1;done
1 Like

Dear All,

Same type of problem again:

Instead of waiting for file to generate, I want to tell my shell script till one already exist file is getting updated:

Example:
my file will get updated after each and every time as shown below

fileContent (will be updated with time values as shown below after finishing calculations)

Time = 1E-5
Time = 2E-5
Time = 3E-5
Time = 4E-5
Time = 5E-5
:
:

at time = 1E-5 I want to do some shell operations and I want to tell my shell script that wait till next time to update.
after Time = 2E-5 I want to do again shell operation and wait for next time to update.

Thanks & Regards,
linuxUser_

Hello linuxuser_

Here is an example for same, lets say we have a file name file20 which is being generated daily and we are checking either this is generated or not for today.

a=10;
while [ $a -le $a ]
do
 if [[ -f file20 ]]
 then
  echo "File20 has been created."
  break;
 else
  sleep 120 ## Taking 2 mins sleep here
 fi
done

Just want to add here we can run this script in background because this is a infinite loop so it will be keep on running till it will see that file20 is being created. So we can schedule this to run in a day at crontab and when it sees the flle accordingly we can set script to send an email to a list to notify on same.

Please let me know if you have any queries. Also please don't try this at Live environment, first try this at non live or test environments.

Thanks,
R. Singh

1 Like

Dear Ravinder,

Thanks a lot for your knowledge.
I need a check little different way. I have a file with name say file30.
This fill content will vary with time. Like push_back type of writing.
after finishing 1st iteration it will contain

Time = 1

after 2nd iteration it will have

Time = 1
Time = 2

and so-on.

I want to check whether new time is updated or not.

How can I do that?

Regards,
linuxUser_

Hello linuxuser_

Could you please let me know the following points.

i- Contents are updating daily in a sngle file or after creating a backup of old file it is creating a new one like as follows.
file_yesterday, file_today OR there is only 1 file.

ii- Also what is the time period when file is being updated? Is it a daily a fixed time task or not?

Also please add more information if there any it will help us to understand your actual requirement.

Thanks,
R. Singh

file content after 1st iteration

Time = 1

file content after 2nd iteration

Time = 1
Time = 2

file content after 3rd iteration

Time = 1
Time = 2
Time = 3

file content after 4th iteration

Time = 1
Time = 2
Time = 3
Time = 4

and so-on.
Hope it will clear all your questions.
Hint is I always have to search for last Time value and compare with previous value. If its get updated have to do some shell operations.

Regards
linuxUser_

Hello linuxuser_

You can use the following code for same. This is also an example of infinite loop which will break when it's condition gets satisfied(means file is being updated). Please run this in background and you can set it to crontab as per your need like 2 hours or 4 hours difference.

a=10;
cp -p file11 /tmp/file1 ## created copy of file at tmp
while [ $a -le $a ]
do
## comparing the values of files present in directory and in tmp ##

 value=`diff /tmp/file1 file11` 
  if [[ -n $value ]]
  then
   echo "file has been updated."
   rm /tmp/file1
   break;
  else
   sleep 120
  fi
done

Please let us know if you have any queries.

Thanks,
R. Singh

1 Like

Thanks a lot :slight_smile:
One very quick question:
will sleep 120 allow shell to check for the condition in mean time or it will make shell to sleep for 120 minutes?

In other way how should I come-up with correct value for sleep time? or its independent of time?

Thanks & Regards
linuxUser_

Hello linuxuser_

you can put sleep timings as per your wish lets say it is 2 mins then again it will check the condition either file has been updated or not if yes it will come out of loop else it will start sleep process again. So we need not to worry about condition check as it will happen only thing this sleep process will make us to set the time period like after how much time we need script to check about file's status. But if you will put less minutes lets say 1 min then it will keep server occupy to check the file status in every one min so better we can mention 5 mins or so(as per your wish as you would have better idea about file's updating timings) so that it will not occupy server that much which it will do when we will keep 1 min interval.

Thanks,
R. Singh

1 Like

Dear Ravinder,

Need little help regarding the same problem.
Finally I got one conclusion that I should be able to read time value from the output file that will get update after every time step.

file format after some iterations:

Time = 0

xMin = 0.0
yMin = 0.0
xMax = 1.0
yMax = 1.0

Time = 1

xMin = 0.01
yMin = 0.01
xMax = 0.98
yMax = 0.899

Time = 2

xMin = 0.19
yMin = 0.25
xMax = 0.795
yMax = 0.9878

Time = 3

I want to read last time or last but-one depends on existence of some file for particular time step.
Can you help me?

Regards,
linuxUser_

Dear linuxuser_

Could you please be more clear on your requirement, as I understood you are requesting for checking a file's existance. Let me give you an example for same.

if [[ -f "file2" ]] 
then 
echo 
"file2 is present."
else 
echo "file2 is NOT present." 
fi

In my case file is present so it will give following output. Also please refer man test for more information. Hope this helps.

file2 is present.

Thanks,
R. Singh

file will exist all the time and it will get update with data(like log file).
like push_back in cpp, data will be pushed into the file after every iteration.
I want to read Time value.
But trick is I want to read last two time occurrences and compare file existence.
If you see in my previous reply, last two time values are 2 and 3.
I want to check whether data files(these files are different) for
these time are present or not.

In the same case, since simulation is still going there will be no data files for time 3. After finished with 2nd simulation data some data will be written. but in log file I can see Time =3 as well which is not done yet.

Let me know if you still didn't understand.

Thanks for help.

---------- Post updated at 07:05 PM ---------- Previous update was at 06:56 PM ----------

Making my explanation even more simple:
How can I read last two occurrences of time value?

Hello linuxuser_

Hope following example may help you(for bash). Here is a command named stat you can check by man stat for more information on same. Lets say we have 2 files named file2 and file33 . Now let us compare their modify timings.

val1=`stat file2 | awk '/Modify/ {print $2}'`
echo $val1
2014-09-01
val2=`stat file33  | awk '/Modify/ {print $2}'`
echo $val2
2014-09-03
 
if [[ "$val1" == "$val2" ]]
then
echo "timings are equal."
else
echo "timings are NOT equal."
fi
timings are NOT equal.

Let me know if I can help further. By this we can compare the timings of files and check if files have been modified or not for today's date.

NOTE: I have tested this in bash environment.

Thanks,
R. Singh