File checking script need help

Hi, Gurus,

I need a scripts to check specified file if it exists or not at certain time (say every month between 5th and 7th). if file exists do something otherwise do another thing.

can anybody help this?

Thanks in advance

:wall:

These lines will check the file daily at noon, on days between the 5th and the 7th when put in your crontab(edit it via crontab -e):

# Do something if /path/to/file exists
0 12 5,6,7 * * [ -f /path/to/file ] && do_something
# Do something if /path/to/file does not exist
0 12 5,6,7 * * [ -f /path/to/file ] || do_something_else

In general, cron lines work like

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

...and the last field is the shell script(bourne shell, not csh) to be run.

See man 5 crontab for details on the crontab file.

Thanks for your quick replay,

Unfortunately, My sys admin does not want to use cron job to do this, I have to using script to check this. do you have any idea about using script to check it?

The alternative is to build your own, buggier, less reliable cron. :confused:

#!/bin/sh

# Append log files if they already exist
[ -s ~/auto.log ] && exec 1>>~/auto.log
[ -s ~/auto.err ] && exec 2>>~/auto.err

while true
do
        # Reopen logfiles if deleted or truncated
        [ -s ~/auto.log ] || exec 1>~/auto.log
        [ -s ~/auto.err ] || exec 2>~/auto.err

        DATE=$(date +%Y-%m-%d-%H-%M)

        case "${DATE}" in
        ????-??-0[567]-12-0[0-5])
                echo "$DATE:  checking for file"
                if [ -f /path/to/file ]
                then
                        do_something
                else
                        do_something_else
                fi
                ;;
        *)
                ;;
        esac

        sleep 300 # Wait 5 minutes
done