Examples of Linux shell script tests

Source: Internet
Author: User
Tags chmod disk usage root directory linux

Script 1: Write a login welcome script for root users:

1, with vi command in the/root directory is a new script file welcome.sh, the contents are as follows:

2, using the chmod command to welcome.sh file to increase the execution (x) permissions

3. Modify the/root/.bash_profile file to invoke the welcome.sh script every time the root user logs on to the system

#vi/root/.bash_profile Add the following:

4. Verify: Login with root user to verify the output information of the Welcome script

Script 2: Scripting to start, stop, and display system service status:

1, with vi command in the/root/bin directory is a new script file status to view the status of the Crond service, which reads as follows:

Set X Permissions

Script files stop and start are similar to status and can be modified slightly.

Script 3: Write a health monitoring script/opt/monitor.sh that records CPU load, memory and swap space, disk space, recent user logon, and time information.

1, with vi command in the/OPT directory is a new script file monitor.sh, the contents are as follows:

#!/bin/bash

Mkdir-p/var/log/runrec

Recfile= "/var/log/runrec/running.today"

Rectime= ' date + '%y-%m-%d%h:%m '

Loadrec= ' uptime '

Memrec= ' Free-m '

Diskrec= ' Df-ht '

lastloginrec= ' Last-n 20 '

echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Record time: $RecTime

CPU Load Information: $LOADREC

Memory Information: $MemRec

Disk Usage Information: $DiskRec

Last Login to users record: $LastLoginRec ">> $RecFile

2, using the chmod command to monitor.sh file to increase the execution (x) permissions

3. Whether the test script can run correctly and output the correct information

4, set the planning task, request every 15 minutes to hold a monitor.sh script, and confirm Crond service has started.

To create a scheduled task:

#crontab –e contents are as follows

*/15 * * * */opt/monitor.sh

View the Crond service status and set the Crond to boot automatically.

#service Crondstatus

#chkconfig--level Crond on

Script 1: Writing a shell script monitoring host

1, with vi command in the/root directory is a new script file sysmon.sh, the contents are as follows:

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.