Write shell scripts (2)

Source: Internet
Author: User

SELF: http://lulu87.blog.51cto.com/1244696/373771

Case 1: use shell scripts for System Management

Lab Environment

The company is equipped with a Linux server for technical training at the Branch Office. Therefore, you need to add user accounts frequently or in batches. In addition, the server also has a web site with the Training Department, you need to regularly monitor the running status of website service programs.

Requirement Description

Write a script to check the running status of the system service httpd. The requirements are as follows:

1) when the service status is abnormal, the log information is included in the "/var/log/htmon. log" file.

2) Automatically Restart the abnormal HTTPd service.

3) if the HTTPd service cannot be restarted, restart the server host.

4) with the crond scheduled Task Service, the detection task is executed every 15 minutes from Monday to Friday.

You can write a script to add users in batches. The requirements are as follows:

1) interaction is required. When the Administrator executes the script program, the number of users (less than 100 users) and username prefix can be specified, you can also set the expiration time and initial password for these user accounts.

2) User encoding uses two digits, for example, "01", "02", and "03" instead of "1", "2", and "3.

3) write the corresponding batch delete user script. The Department must provide interactive functions, but it must be able to specify the username prefix through command line parameters, run the script to delete all user accounts with this prefix, but avoid accidental deletion of root users.

Recommendation procedure

Compile the htmon. Sh script file to check the HTTPd service status.

1) run the "service httpd status" command to test the HTTPd service status. If the returned value is 0, it indicates normal. If the returned value is not 0, it indicates abnormal.

2) When the service status is abnormal, use the redirection append symbol "& gt;" to write log information to the "/var/log/htmon. log" file.

3) run the "service httpd restart" command to restart the HTTPd service and run the "service httpd status" command again to check the status of the HTTPd service.

4) if the service is still abnormal, run the chkconfig command to set the HTTPd service to self-start, and then run the shutdown command to restart the system.

5) Save the tested script file to the file "/opt/htmon. Sh" and set the scheduled task to be executed on a regular basis.

The script content is as follows:

CrondPlan Service:

Write the myuadd. Sh script to add user accounts in batches.

1) Use the READ command to prompt the user to enter variable information such as the user quantity, user name prefix, expiration time, and initial password.

2) use the while statement to execute the Add USER command operation cyclically.

3) use the if statement to determine the user number. If the number is less than 10, the account is automatically appended with the character "0" at the end of the prefix string ".

The script content is as follows:

The result of executing the script:

Write the myudel. Sh script to delete user accounts in batches.

1) use the first location parameter entered when the Administrator executes the script as the username prefix (we recommend that you do not set it to 3 characters in length and avoid overlap with other irrelevant users to avoid accidental deletion ).

2) If the postmaster does not specify the location parameter, end the program after an error is prompted (you can use the "Exit 1" statement. 1 indicates that the script returns the status value of the shell environment after exiting ).

3) use the grep and cut commands to filter out the user account names with the specified prefix in the passwd file and use the "-V" option to exclude the root user.

4) Back Up Files such as "/etc/passwd", "/etc/shadow", and "/etc/group, in order to avoid accidental deletion of system users and use them for restoration (prior to the deletion of users should be carefully checked and confirmed ).

5) execute the command to delete the user account cyclically using the for statement.

The script content is as follows:

Execution result:

Case 2: compile a simple system Service Startup Script

Lab Environment

On an rhel5 server, the zebra software is installed through source code compilation to provide the soft routing service. To facilitate management operations such as startup and termination of the zebra service, you need to write a simple STARTUP script for the zebra service.

Requirement Description

Compile the zebrad script program and save it to the "/etc/init. d" directory. The requirements are as follows.

1) Run "/etc/init. d/zebrad start" to start the zebra service program.

2) Execute "/etc/init. d/zebrad stop" to terminate the zebra service program.

3) Run "/etc/init. d/zebrad restart" to restart the zebra service program.

4) Execute "/etc/init. d/zebrad status" to view the zebra service status.

5) if the "Start", "stop", "restart", and "status" parameters are used when the script is executed, the system will exit after prompting for help information.

6) The relevant prompt information is displayed when the service is started or terminated.

Run the "chkconfig-add zebrad" command to add zerbrad as a system service and check the startup status in runtime Level 3 and 5.

Recommendation procedure

Write the "/etc/init. d/ZEBRA" script file and add the "X" permission

1) define functions for the zebra service control statements that start, stop, and view statuses respectively.

2) run the "/usr/local/sbin/ZEBRA-d" command to start the zebra service.

3) Terminate the zebra service process by saving the PID number in the "/var/run/zebra. PID" file.

4) determine the zebra service status by judging the "zebra-d" information in the current running process, for example, run the "PS aux | grep" zebra-d "| grep-V" grep "& gt;/dev/null" command first, and then judge its return status.

5) set the case statement. When the script parameter ($ I) provided by the user is "start", "stop", "restart", and "status", the corresponding functions are called respectively, otherwise, the system performs the default operation. The system displays usage help information and exits.

The script file is as follows:

Use"Start","Stop","Restart","Status"And other Parameter TestsZebraFunctions of the script program.

Execute"Chkconfig-add zebra"CommandZebraAddIxtService, and confirm its startup status at each running level.

119850602

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.