Clear system logs shell scripts -- self-study notes

Source: Internet
Author: User

This is a script for clearing system logs:

Vim logmess_clean.sh

# Bin/bash // shell interpreter used by the script

Cd/var/log // switch to the log storage directory

Echo> messages // clear logs

Echo "logmessages is clean" // output "log clearing" after script execution is complete"

[Root @ localhost shellscripts] # ll view the script permission

Total 4-rw-r --. 1 root 67 May 26 logmess_clean.sh // The script written by default is not executable.

Chmod + x logmess_clean.sh

[Root @ localhost shellscripts] #./logmess_clean.sh logmessages is clean run this script:

# Cat/var/log/messages check that the script is successfully executed:

If you do not want to input./or sh to execute the script, you want to execute scriprt anywhere in the system. Move the script to the PATH environment variable:

[Root @ localhost shellscripts] # echo $ PATH // view the PATH of the current system shell environment variable/usr/lib/qt-3.3/bin:/usr/local/sbin: /usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

[Root @ localhost shellscripts] # mv logmess_clean.sh/bin // move the script to the variable path

[Root @ localhost ~] # Logmess_clean.sh // execute the script

Logmessages is clean

You can also configure the environment variable path:

[Root @ localhost ~] # Vim/root/. bash_profile // edit the current environment variable after the system

PATH = $ PATH: $ HOME/bin:/shellscripts/

[Root @ localhost ~] # Logmess_clean.sh

Logmessages is clean

Root/. bash_profile is one of the three environment variable setting files in linux, and the scope of this environment variable is only for the current user.

After the modification is complete, the system has recorded the/bin path, and we need to disconnect the connection. to take effect immediately, run the following statement: # source. bash_profile

You can also edit the. bashrc environment variable file.

Modify the. bash File

[Root @ localhost/] # vi/root/. bashrc

#. Bashrc

# User specific aliases and functions

Alias rm = rm-I

Alias cp = cp-I

Alias mv = mv-I

# Source global definitions if [-f/etc/bashrc]; then./etc/bashrc fi

Export PATH = "$ PATH:/shellscripts/" // Add this row

 

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.