This is for my Intro to Linux class. We are using BASH and Vi editor. LAB INSTRU
ID: 3602055 • Letter: T
Question
This is for my Intro to Linux class. We are using BASH and Vi editor.
LAB INSTRUCTIONS
STAGE 1 | Creating A Script For The Crontab | The ‘Curl’ Command
First create a directory within your home directory called ‘logs’. This is where we will save our outputted logs.
To get things going with log files and log rotations we will create a script in our home directory that will log its outcome. We will call this script ‘IsTheServerUp.bash’. As you may have guessed from its name, the script will check to see if a server is up. It will then log(echo) the outcome of the script to the file. We will be using the curl command to check to see if a server is up and running. The curl command is a command that connects to a url and transfers data from it. The data to transfer depends on how the command is run but by default it will retrieve the HTML document of a web page. Use the following set of rules to write your bash script:
• Use the ‘curl’ command to request a web page from the server
• e.g, curl “http://www.csun.edu/” (You can use whatever webpage you want) • provide the ‘--output /dev/null’ option + argument to suppress output
Check the return status ($?) to determine if there is a problem
If a status of 0 is returned, log to the file a message of success. If it is not a 0, log to the file a message of error. Make sure to include the status code and the date/time stamp. An example can look like so: (check date man page on how to format the date/time stamp) NOTE: Pay attention to date format
Apr-11-2015-04:12:00 PM (0) Web Server [ www.xbox.com ] Is Online
Apr-11-2015-08:39:00 PM (7) Web Server [ www.xbox.com ] Is Offline
Log your outputted messages to a file called ‘Server_Info.log’ in your newly created ‘logs’ directory. You can use whatever webpage you want.
Thank you in advance!
Explanation / Answer
steps to follow:
1.create directory using below command by opening terminal using ctrl+Alt+T
cd logs
2.create script as you mentioned name with IsTheServerUp.sh.Dont forget to give permission to script file created by you using following command.
sudo chmod 777 -R IsTheServerUp.sh
3. below is the code for IstheServerUp.sh file.
#!/bin/bash
#your home directory
exec &>> /home/gopal/logs/Server_Info.log
#any web address you can use it
web=https://www.google.com
status=`curl $web -k -s -f -o /dev/null && echo "SUCCESS" || echo "ERROR"`
if [ $status == "SUCCESS" ]
then
echo $(date +%b-%d-%Y-%l:%M:%S) $(date +%P) [$web] "Is Online"
else
echo $(date +%b-%d-%Y-%l:%M:%S) $(date +%P) [$web] "Is Offline"
fi
3.Now its time create cron job . Now i am checking every minute with crontab whether website is online or offline
4.open fresh terminal goto following path.create myjob.sh script inside the folder
cd /etc/cron.hourly
sudo gedit myjob.sh
5.below is the code for myjob.sh
#!/bin/bash
#redirect to your home directory
cd /home/gopal/
./IsTheServerUp.sh
6.give permission to our myjob.sh using following command.
sudo chmod 777 -R myjob.sh
7.now we need to add myjob.sh script to cron to run every minute.
8..follow below commands to acheive it.open terminal use below commands.
crontab -e
9.it will ask what editor you want choose as your wish.Then paste the beolow code.
#Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
*/1 * * * * /etc/cron.hourly/myjob.sh
10.now save it.Now you can check every minute in the Server_Info.log
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.