Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is for my Intro to Linux Class. We use BASH and Vi Editor. Thank you for your help in advance. STAGE 2 | Setting

This is for my Intro to Linux Class. We use BASH and Vi Editor. Thank you for your help in advance.

STAGE 2 | Setting A Cronjob With Crontab

For this part of the lab we will create another script and add in a cron job.

Write a script called log_copy. This script should be run every hour via a cron job. Its input will be the log file from the previous bash script. One way to do this is to grep the information from the log file that matches a timestamp equal to the last hour. Use the following hints to construct this BASH script.

1. Determine what the last hour is and save it as a variable as shown below (check date man page)

LAST_HOUR=$(( $(date +%l) - 1 ))

if LAST_HOUR is less than 10 then pad a 0 in front i.e. : 6 should become 06

2. Create the search string and save it as a variable as shown below

SEARCH_STRING=$(date +%b-%d-%Y-${LAST_HOUR}:)

The above string will search through your Server_Info.log file for timestamps matching the last hour.

3. Search the log file with the following command inside of your log_copy script.

grep ${SEARCH_STRING}

4. Output the results to a new log file. Name this new log file Server_Hourly.log. Make sure that this new log file is located inside of your logs directory and that the script log_copy is located inside of your home directory.

5. Once both scripts are created test them out by running the IsTheServerUp.bash script first (run it 5-6 times). Make sure a log file called Server_Info.log was saved inside of your logs directory after running this script. Once you see that a log file was created and is populated with the result of the script you can then run your log_copy script (before running log_copy script, open Server_Info.log file and change the hour of the first two lines to one hour before). After running your second script, make sure that a second log file called Server_Hourly.log was created within your logs directory and that it is populated with the results of the second script.

6. After verifying that both scripts work you will need to create 2 cron jobs so that these scripts are ran automagically. Create a new cron job by running the following command:

% crontab -e

NOTE: If it prompts you to select an editor, select the first editor (Option 1 - /usr/bin/vim/basic) Add the following rules to your crontab:

Run your IsTheServerUp.bash script every 10 minutes.

Run your log_copy script every hour.

Make sure you add the entire path to your script so /home/users#/your_username/script

Replace # with your user group #. HINT: use the pwd command

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems An Application Oriented Approach Complete Version

Authors: Michael Kifer, Arthur Bernstein, Richard Lewis

2nd Edition

0321268458, 978-0321268457

More Books

Students also viewed these Databases questions

Question

What is database?

Answered: 1 week ago

Question

What are Mergers ?

Answered: 1 week ago