This is for my Intro to Linux class. We are using BASH and Vi editor. LAB INSTRU
ID: 3601962 • 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 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.
Thank you in advance!
Explanation / Answer
When you first login, your current working directory is your home directory. Your home directory has the same name as your username and it is where your personal files and subdirectories are saved. To find out what is in your home directory, type
$ ls [Enter]
compute
crontab <<'EOF'
SHELL=/bin/bash
#min hr md mo wkday command
*/10 * * * * curl 'http://localhost:8983/solr/dataimport?command=full-import'
EOF
Use crontab -l to get a look at it afterwards. BUT, add an option to that curl command to put the output somewhere specific, since it might be run somewhere to which you don't have write access. Also, if curl is anywhere unusual, you may need to specify its full path, like /usr/bin/curl, or set the crontab PATH variable.
The quotes around EOF prevent substitution in the contents of the HEREIS document (everything between the <<EOF and EOF). HEREIS documents are a shell feature, not part ofcrontab`.
See man 5 crontab for a detailed breakdown of what goes in crontab files.
I usually keep a ~/.crontab file to edit with a special first line, and the execute bit set:
#!/usr/bin/env crontab
SHELL+/bin/sh
[... etc.]
This lets me edit my ~/.crontab and then just run it with:
$ vi ~/.crontab
$ ~/.crontab
#!/bin/bash
CURL='/usr/bin/curl'
RVMHTTP="https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer"
CURLARGS="-f -s -S -k"
# you can store the result in a variable
raw="$($CURL $CURLARGS $RVMHTTP)"
# or you can redirect it into a file:
$CURL $CURLARGS $RVMHTTP > /tmp/rvm-installer
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.