Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Creating A Script For The Crontab | The ‘Curl’ Command First create a directory

ID: 3602108 • Letter: C

Question

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 ran 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 Offliline


•   Log your outputted messages to a file called ‘Server_Info.log’ in your newly created ‘logs’ directory. You can use whatever webpage you want

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 Offliline

Explanation / Answer

1.find code below isServerup.sh

#!/bin/bash

#your home directory

exec &>> /home/gopal/logs/Server_Info.log

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

2.change permission for isServeruP.sh using below command

sudo chmod 777 -R isServerup.sh

3.now follow below commands to create crown job

cd /etc/cron.hourly

sudo gedit myjob.sh

4.code for myjob.sh

#!/bin/bash

#redirect to your home directory

cd /home/gopal/

./IsTheServerUp.sh

5.save it change permission using below command.

sudo chmod 777 -R myjob.sh

6.now follow below comands

crontab -e

select option 2(choose editor)

7.paste below to run every one minute cron job

#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

8.Done now you can check every one time log file inside server_info.log

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote