2

I want to check the file xx_x__x.log is present in the location or the file size is 0 at every Sunday 2:30am. I have a script to check file size and to check that it exists but I don't know how to check at a particular time.

  • This is pretty straight forward with test ([). What have you tried? – heemayl Nov 28 '16 at 5:26
  • please accept @the_velour_fog' answer, which actually answer the whole question, not just part of it. – inger Aug 9 '17 at 16:29
2

To check if a file is present in a location you can use the -f in a if statement.

if [ $FILE -f ]; then

(As the_velour_fog said, the expression will return True even if the file is empty)

  • 1
    [ $FILE -f ] this will also return true even if the file is empty – the_velour_fog Nov 28 '16 at 6:22
  • 6
    you can use [[ -s $FILE ]] which will return 1 (false) if the file is present but empty – the_velour_fog Nov 28 '16 at 6:25
15

From man bash | less '+/^\s*CONDITIONAL'

-s file
True if file exists and has a size greater than zero.

To get the script to run at a pre-defined time, put your code into an executable scipt and load it into a cronjob.

I suspect you possibly could put your command into a one liner and load it into a crobtab? ( I would be interested to know if anyone else has done this successfully)
But the problem with cron commands is that

  1. they are not connected to a terminal - that I know of {although you could probably check with lsof or something} - so you don't get any debug output / feedback about what went wrong as you build up your cron command
  2. You have to wait a some time before you can test your cron command

for those reasons I would recommend just put your commands into an executable script, so that you can write your code in a coder friendly environment - i.e. a terminal, then when your are satisfied its behaviour is correct then load a command to call it into a crontab

so say this is your script /tmp/checksize.sh

#!/bin/bash

if [[ -s /tmp/xx_x__x.log  ]]
then 
        printf "\n\n%s\n\n\n" true > /tmp/sizelog.log
else 
        printf "\n\n%s\n\n\n" false > /tmp/sizelog.log
fi

just run crontab -e and add the command

5 0 * * * /tmp/checksize.sh

which means: # run five minutes after midnight, every day

about getting the timing right

from man 5 crontab

Commands  are  executed by cron(8) when the minute, hour, and month of
       year fields match the current time, and when at least one  of  the  two
       day  fields  (day of month, or day of week) match the current time (see
       ``Note'' below).  cron(8) examines cron entries once every minute.  The
       time and date fields are:

          field      allowed values
          -----      --------------
          minute         0-59
          hour       0-23
          day of month   1-31
          month      1-12 (or names, see below)
          day of week    0-7 (0 or 7 is Sun, or use names)

[...]

EXAMPLE CRON FILE
       The following lists an example of a user crontab file.

       # use /bin/bash to run commands, instead of the default /bin/sh
       SHELL=/bin/bash
       # mail any output to `paul', no matter whose crontab this is
       MAILTO=paul
       #
       # run five minutes after midnight, every day
       5 0 * * *       $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
       # run at 2:15pm on the first of every month -- output mailed to paul
       15 14 1 * *     $HOME/bin/monthly
       # run at 10 pm on weekdays, annoy Joe
       0 22 * * 1-5    mail -s "It's 10pm" joe%Joe,%%Where are your kids?%
       23 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday"
       5 4 * * sun     echo "run at 5 after 4 every sunday"
       # Run on every second Saturday of the month
       0 4 8-14 * *    test $(date +\%u) -eq 6 && echo "2nd Saturday"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.