Hello, Please write a Linux BASH script based upon the following instructions. F
ID: 3600740 • Letter: H
Question
Hello,
Please write a Linux BASH script based upon the following instructions.
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.facebook.com/” (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
• Log your outputted messages to a file called ‘Server_Info.log’ in your newly created ‘logs’
directory. You can use whatever webpage you want
Explanation / Answer
Larger files are harder to manipulate.
File systems run out of space.
The information you log may constitute personal data.
A solution to this generic problem of log file growth is log rotation. This involves the regular (nightly or weekly, typically) moving of an existing log file to some other file name and starting fresh with an empty log file. After a period the old log files get thrown away.
Because this is a general issue, many Linux distributions (SLES included) include a general solution that can be applied to any set of log files, not just the web server's. There is an Apache-specific solution (which is provided by the rotatelogs command) but we will use SLES's generic solution, provided in the logrotate package.
Once each night the logrotate program reads in its configuration files telling it which logs to rotate and how to do it. One of these files tells it to rotate Apache's log files.
The main configuration file sets up the defaults and then reads in a directory of instructions for specific sets of log files from the /etc/logrotate.d directory.
/etc/logrotate.conf: commands
weekly
Each file should be rotated weekly. The log rotation job runs nightly, though, so this can be changed to daily for a specific log file if desired.
The three commands that specify how often rotation should take place are daily, weekly and monthly.
rotate 4
Keep four sets of log files. The comment is slightly inaccurate; four weeks' worth of logs will be kept if rotation is done weekly. If rotation is done daily then this command means that four days' worth of logs are kept.
create
After moving the main log file from logfile to logfile.1 a new, empty logfile should be created.
include /etc/logrotate.d
This command instructs the log rotation program to read in every file in this directory. One of these files will correspond to the web server's log files.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.