LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-19-2014, 09:22 AM   #1
Spuddy
Member
 
Registered: Jul 2010
Posts: 83

Rep: Reputation: 15
Question Cronjob to check if file contents are missing and to remove and re-copy if missing.


Hi,

I'm fairly new to Unix and have a basic requirement to check the contents of a file every 10 minutes. If it is missing certain parameters, I want to remove it and re-copy an original from another location.

For example, I have a file called config.cf

If abcdefghijklmnopqrstuvwxyz is missing from the files contents, I wish to rm config.cf and re-copy it from /var/www/test/config.cf

Otherwise I'm happy for this to be done based on lines of code. I.E less than 612 lines of code, rm config.cf and copy from /var/www/test/config.cf

Any ideas?
 
Old 03-19-2014, 09:27 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
what have you tried and where are you stuck.

i think normal commands like grep, cp, mv, rm is all you would need.
 
Old 03-19-2014, 09:30 AM   #3
Spuddy
Member
 
Registered: Jul 2010
Posts: 83

Original Poster
Rep: Reputation: 15
I have never written a cron file, thus nothing! Any pointers would be great.
 
Old 03-19-2014, 09:47 AM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
i use crontab -e to edit the cron entry and use this as a guide:
http://en.wikipedia.org/wiki/Cron#Pr...ng_definitions
 
Old 03-19-2014, 09:59 AM   #5
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
OK you need a couple of things.

First create a script to do what you want using your favorite editor in the directory you want the script to exist (e.g. vi /usr/local/bin/fix_config.sh)

Code:
#!/bin/bash
# fix_cocnfig.sh
#
PATH=$PATH:/bin:/usr/bin
if grep -q abcdefghijklmnopqrstuvwxyz config.cf
then echo config.cf is OK
else cp /var/www/test/config.cf config.cf
fi
Next create a cron entry to run the script with the "crontab -e" command which puts you in a vi session of cron.
In the vi session of cron enter a line like:

Code:
00,10,20,30,40,50 * * * *  /usr/local/bin/fix_config.sh >>/var/log/fix_config.log 2>&1
DISCUSSION:
In the above script the first line is called the interpreter line when it starts with "#!". This tells it what shell or program to run the rest of the commands as. I'm assuming you're doing this on Linux. If not you may need to pick another shell (e.g. ksh or posix sh on UNIX).

The PATH is a variable that tells it where to look for executable commands. Typically when logged in your PATH is already set but cron and some other background jobs get minimal settings so you want to insure PATH contains the directories that use the commands you want. You can determine those by running "which <command>" for the commands you're going to use (e.g. "which cp" and "which grep")

The "if", "then", "else", "fi" seen in next three lines is a standard conditional. (elif is also available). It evaluates the condition and if true it does the "then" and if not does the "else".)

The "grep -q abcdefghijklmnopqrstuvwxyz config.cf" uses the grep command which searches for patterns. The -q option tells it not to give any output but just test whether the pattern you look for is there. This is what the "if" is testing for - if it finds the
pattern it just outputs a note saying it is OK. If not it does the copy using the cp command. The "fi" tells it to end the conditional as you can have quite a bit more than one line for the "then", "else" and "elif" steps in more complicated scripts.

Note that the cp has the full path for the source file but not for the target because you didn't tell us where the target lives. You probably want to put in a full path for the target.

The cron entry is an example. The first few fields are the time specification for when it should run:
00,10,20,30,40,50 * * * *
The first section specifies it should run every 10 minutes. The fields with "*" mean it should run every day every hour. You'd have to adjust all these fields to run it when you want.

The "/usr/local/bin/fix_config.sh >>/var/log/fix_config.log 2>&1" is the actual command line that will be run each time. It is running your new script and appending its output to a log called /var/log/fix_config.log. It is also sending any errors to that same log by redirecting standard error (stderr, file descriptor 2) to standard output (stdout, file descriptor 1). Note that you don't have to log at all so you could leave out the ">>/var/log/fix_config.log 2>&1". If you DO log make sure you keep track of the size of the log to avoid filling up your /var.

I'd suggest you do a Google search for "shell scripting tutorial" to get the basics of scripting.

You can also use the built in manual system to get details of most commands by simply typing "man <command>" as for example:
man grep
man cp
man cron
man crontab
man bash
man echo
 
Old 03-19-2014, 10:20 AM   #6
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by MensaWater View Post
I'd suggest you do a Google search for "shell scripting tutorial" to get the basics of scripting.
https://www.linuxquestions.org/quest...llected-35954/ has a "few".

Last edited by Habitual; 03-19-2014 at 10:43 AM.
 
  


Reply

Tags
copy, cron



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check missing header files included from another header file adisan82 Linux - Software 1 01-28-2011 03:57 AM
/boot folder contents missing/deleted samengr Linux - General 1 11-29-2008 07:17 AM
Can anybody check this copy,remove and chown scripts? sathyguy Linux - Newbie 1 03-18-2007 11:05 PM
How to check which package provide a required missing file? davidas Linux - Newbie 4 08-10-2004 12:48 PM
Contents of Binutils, Book 4.1, Ch 5.8, missing .so files itsjustme Linux From Scratch 1 07-22-2003 07:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration