This is for my Intro to Linux Class. We use BASH and Vi Editor. Thank you for yo
ID: 3603549 • Letter: T
Question
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. It’s 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
Explanation / Answer
Well, it seems few information is missing but lemme paste the scripts:
log_copy.bash
#!/bin/bash
LAST_HOUR=$(( $(date +%l) - 1 ))
SEARCH_STRING=$(date +"%b-%d-%Y-${LAST_HOUR}:")
grep "${SEARCH_STRING}" > "/home/users/vijay/log/Server_Hourly.log"
------------------------------------------------------------------------------------------
Now, create one file named scheduler with below mentioned content:
scheduler
#!/bin/bash
PATH=/bin;/usr/local/bin;/usr/bin;/usr/sbin;/sbin;
*/10 * * * * bash /home/users/vijay/your_username/script/IsTheServerUp.bash
0 * * * * bash /home/users/vijay/your_username/script/log_copy.bash
---------------------------------------------------------------------------------------------------------
Now, execute below menioned command to schedule the jobs. It will directly schedule the job without any need for an editor:
sudo crontab /path-to-scheduler/scheduler
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.