The environment

CentOS7

Train of thought

Set an infinite loop that starts the HTTPD service while executing the script, starts recording the current time, and then prints it to the log, sleeps for 14 seconds, then immediately stops the HTTPD service, starts recording the current time, and prints it to the log. Repeat the loop.

code

Open the file with vi test.sh, press I to edit it, and enter the following code:

#! /bin/shwhile : do systemctl start httpd curdate=`date +"%Y-%m-%d %H:%M:%S"` echo [INFO] ${curdate} >> /home/opp/test.log "httpd is starting" sleep 14 systemctl stop httpd curdate=`date +"%Y-%m-%d %H:%M:%S"` echo [INFO] ${curdate} >> /home/opp/test.log  "httpd is stopping" doneCopy the code

Then you need to create the test.log file:

mkdir /home/opp
vi /home/opp/test.log
Copy the code

Press Esc and then :wq to exit.

Enter the following command to start executing the script:

./test.sh
Copy the code

Wait for a while, then press Ctrl+Z to stop the script and enter the following command to view the log

vi /home/opp/test.log
Copy the code

You can see the following:

You can see that this is basically done by having the HTTPD service stop every 15 seconds, using a script to monitor and start, and logging. But because the program takes time to run, some times will be longer than 15s, but the general idea is this.

The log specification

Pay attention to log specifications. Generally, the first log level is INFO, WARN, ERROR, etc. Then the time and the content of the record and so on. For details, you can check the log specification online.

reference

Shell scripts are executed every few seconds

Shell retrieves the current date and time

Linux(centos) Commands for creating, deleting, and moving folders and files

Shell input/output redirection

Shell script that automatically monitors the Apache service status and restarts it

Summary of logging specifications

LOG Usage Specification (Sorting)