2

I want to check if a directory contains a file (for example sample.txt). If it doesn't contain that file in 5 hrs then I would need to perform some action. How can I do this in a script?

I tried this:

if=$(find /home/Savio/Dsktop/check -type f -Sample.txt "*.txt") )

What will be the commands I need to use?

3
  • sleep 5h; yourcommand. May 25, 2015 at 13:13
  • @davidbaumann this should be in answer not in comment. May 25, 2015 at 13:18
  • Sorry, I didn't have the time to make a answer with explanation. May 25, 2015 at 16:04

2 Answers 2

4

The above shell script suggested by statox is of course correct, but it does not take into account the fact the computer may go down in between the two checks, or you may logout, or you may interrupt the ssh session from which you are running the script.

The simplest way to assure yourself against all these events simultaneously is to use the at command to execute forward in time the second check. Thus I suggest the above script be modified as follows:

 #!/bin/bash
 FILE=/home/Savio/Dsktop/check/sample.txt    
 if [ -f $FILE ]
 then
        #Do what you want if file exists
 else
        at now + 5 hours /home/Savio/ExecutableScript
 fi

where /home/Savio/ExecutableScript is an executable script (*chmod 755 ExecutableScript) which checks for the exitence of the file sample.txt, and, if it does not exist, takes suitable actions knowing that the 5h time lapse has expired already.

I understand most people are not familiar with the at now + 5 hours format of at invocation, you may find it detailed here.

The above will survive reboots, logouts, disconnections.

EDIT: the at command is especially useful because it uses your environment, not root's. Also, keep in mind that at uses the standard Bourne shell sh, not the Bourne-again shell bash. Thus, in the she-bang in the file /home/Savio/ExecutableScript, make sure you specify the bash shell explicitly, for it to work as you expect it to.

2

You could use something like that

#!/bin/bash
FILE=/home/Savio/Dsktop/check/sample.txt    
if [ -f $FILE ]
then
    #Do what you want if file exists
else
    sleep 5h
    if [ ! -f $FILE ]
    then
        #Do what you want if the file still doesn't exists
    fi
fi

The variable $FILE contains the path of the file you are looking for, it can be modified to search for other files.

The test if [ ! -f $FILE ] use the -f parameter which is "Test if the file exists".

And the delay you need is implemented with sleep 5h. Sleep allows to pause a script. If you don't provide a suffix after the number the time is interpreted as seconds but you can also use the suffixes s, m, h and d to set time to seconds, minutes, hours or days.

EDIT : The problem of this method is that it won't survive a reboot or a disconnection, see the answer of @MariusMatutiae for a more robust solution.

3
  • 1
    I don't have enough reputation to comment on his answer so I'll do it here: The answer of @MariusMatutiae is way more robust and the use of at now is pretty cool I didn't know about it. OP should accept this answer.
    – statox
    May 25, 2015 at 14:45
  • @startox your answer is right, the user didnt ask about persistent command so your script solve his question. May 25, 2015 at 15:19
  • 2
    @Francisco indeed my answer solve the problem but I find his solution more elegant since it covers more scenarios without introducing a drawback :) That said OP is free to use the solution she feels the more appropriate to her situation.
    – statox
    May 25, 2015 at 15:26

You must log in to answer this question.

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