Run a cronjob only when a file is modified?

Thread Tools Search this Thread
# 1  
Run a cronjob only when a file is modified?


I am new to cron. I have a cronjob that updates a dataset in a 3rd party application. The contents of this dataset come from a text file, which is updated irregularly. Currently my cronjob runs once every week, to update this dataset (irrespective of whether the file was updated or not).

My task is to run the cronjob, if the file was modified say in the last 7 days. I can find if the file was modified using:

find /my_dir_name/my_Filename -mtime -7 -type f

And my cronjob is:

00 1 * * 5 (myScript > myLogFile)

Is there a way to combine these?

# 2  
Instead of worrying about calculating 'a week ago', let cron do that work. Just keep a temp file that you touch whenever you update the data. If the input datafile is newer than your tempfile -- or the tempfile doesn't exist -- you should update.

00 1 * * 5 [ /tmp/.lastupdate -nt /my_dir_name/my_Filename ] || ( myScript > myLogFile ; touch /tmp/.lastupdate )

# 3  
Just keep a temp file that you touch whenever you update the data.
Do I need to maintain this manually and update everytime the "text file" is updated? I am confused - could you elaborate?

I do not edit/change the input data file. All I do is check whether or not the input data file was updated ->
if yes - Run the cron job to update the 3rd party database.
if No - Do nothing.

Last edited by ad23; 02-15-2012 at 05:53 PM..
# 4  
Originally Posted by ad23
Do I need to maintain this manually and update everytime the "text file" is updated?
Of course not, that's what touch is for. It creates and/or updates its own temporary file.

It just uses the temporary file as a timestamp, "refreshing" it after it processes the data. If the data file is older than the timestamp file, then it's been processed already. If it's newer than the timestamp, it arrived after the last batch of processing.

You don't have to trust me, try it for yourself. Type these into your shell:

$ rm -f lastupdate # This doesn't need to exist yet
$ touch inputfile
$ [ lastupdate -nt inputfile ] || ( echo "Updating" ; touch lastupdate )

$ [ lastupdate -nt inputfile ] || ( echo "Updating" ; touch lastupdate )
$ [ lastupdate -nt inputfile ] || ( echo "Updating" ; touch lastupdate )
$ [ lastupdate -nt inputfile ] || ( echo "Updating" ; touch lastupdate )
$ touch inputfile
$ [ lastupdate -nt inputfile ] || ( echo "Updating" ; touch lastupdate )


# 5  
Thanks a bunch! I understand now.

Also, I want to add a check-point to this: to avoid updating the database while the input-text-file is being updated or if the file is corrupt (or does not exit). Is it advisable to check by file size, since the updated file will be larger than the old file? If yes, how can I accomplish this?

# 6  
What's your system?
# 7  
I am using Suse Linux 10 (x86-64)

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #431
Difficulty: Medium
JavaScript arrow functions were first introduced in 6th Edition - ECMAScript 2015.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Seeing unexpected results when i run through cronjob

Hi I'm having hard time here with below script. If i run script manually i see expected results but, if i keep this script in cron job i'm getting unexpected results. Unexpected results means even though condition is true,cronjob returning output of else condition. This script and cronjob... (2 Replies)
Discussion started by: buzzme
2 Replies

2. Shell Programming and Scripting

Run cronjob for every 10 minutes

Hi Friends, I have a requirement to run the cronjob for every 10 minutes from 2:00 AM to 6:00 AM. Does the below code works? If not, please advise. * * * * * command to be executed ┬ ┬ ┬ ┬ ┬ │ │ │ │ │ │ │ │ │ │ │ │ │ │ └---------------------------------- day of week (0 - 6) (0 is... (5 Replies)
Discussion started by: srikanthbiradar
5 Replies

3. Shell Programming and Scripting

How to accomplished to run zenity in cronjob?

Hi, I would like to ask some few questions about zenity. I write a script that would generate a report if any changes on the files. i want to used zenity to had a visual alarm report if theres some changes on the comparisons of the file.I used this line of zenity but it doesn't work in cronjob.... (5 Replies)
Discussion started by: jao_madn
5 Replies

4. Shell Programming and Scripting

Error at the time of cronjob run

Hi All , Please read the below data carefully. I have run the below script on unix server its running succesfully. but when i run the same script using cronsetyp it giving me the error : command not found at (line num) below is code netstat -anp |grep 8181... (5 Replies)
Discussion started by: aish11
5 Replies

5. Shell Programming and Scripting

Run a script when a file is modified/time stamp changed

Hi, I need to run a script file which uses a file and that file is modified as and when some alarms generated, it is not based on any fixed time period.. it may be modified even once in a minute for some time and once in 30 min or once in 20 min. Hence i need to watch for the timestamp change of... (3 Replies)
Discussion started by: aemunathan
3 Replies

6. AIX

My script didn't run every run every minute at cronjob

In my cronjob, I would like to schedule my to run every minutes. I crontab -e and have in line below but it didn't seems to run at all. * * * * * When I run it manually, I can run it. Is that anything wrong with the above line? If I change it to something like below,... (4 Replies)
Discussion started by: ngaisteve1
4 Replies

7. Shell Programming and Scripting

Doesn't run as a cronjob...

Hi! I have a svn backup script that works perfectly if I execute it from the command line but if I set it as a cronjob to run at night, only part of the code works. So, basically the scripts starts by deleting the folder yesterday and then moves the folder today to the folder yesterday. When... (4 Replies)
Discussion started by: ruben.rodrigues
4 Replies

8. UNIX for Dummies Questions & Answers

Run a script if file modified in last 10 min

Hi, I want to check if a file is modified or not in the last 10 mins and run a script if so. Thanks (8 Replies)
Discussion started by: krabu
8 Replies

9. UNIX for Dummies Questions & Answers

how to retrieve original contents of a modified file (modified using vi)

Made changes to a file using vi editor and saved those changes now realised that the changes are not required How can I get the previous version of the file.i.e the one which was there on which I had made changes (3 Replies)
Discussion started by: novice100
3 Replies

10. UNIX for Advanced & Expert Users

cannot run cronjob

I have the following cron: 0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57 * * * * /v/sysadmin/sysnet/ The .pl has been tried at 755 and 777 The script works if manually run by the web browser or unix command prompt, but I can't get the Cron to do it automatically i went... (3 Replies)
Discussion started by: shahrahulb
3 Replies

Featured Tech Videos