Shell Programming service scripting, file lock, and signal capture

Source: Internet
Author: User
Tags flock lock

Shell scripting is an essential skill for Linux OPS engineers and a very important skill, so it's only good to learn shell programming well. Basic grammar I will not speak, learn the C language of these languages, a little bit to understand the basis of shell programming, so we directly cut to the point.

The 20th day of the classes:

Today's Course Outline:

    1. Functions in Shell programming

    2. Write a script that automatically mounts

    3. How to implement automatic mount with AutoFS

    4. File lock and Signal capture trap

    5. Sed stream File Editor

Detailed Explanation:

    • Functions in Shell programming

In the shell, we define the functions in two ways:

Call the function name directly at the time below.

The thing to note here is the return value of the function in the shell, which is not the same as other programming languages, and its return value returns is the exit status code that returns the function. Execution succeeded to 0, unsuccessful for non 0.

    • Write a script that automatically mounts

To write a service script, the general steps are:

1. Have Execute permission (chmod +x/...)

2. Support Start,stop ...

3. Boot up

Next we write a service script that is automatically mounted, following the steps described above,

1. We can first write the main body of the script: 3.4 line is on behalf of me this service script can boot up, 5 lines means I want to use/etc/init.d/functions this library, the following success and failure are from this library.

2. Write the start () function, Mountpoint is to detect/boss whether this folder is a mount point, if it is hanging at the point, then I start the failure, that is, itself is already mounted, if not the mount point, then I normally boot, and then mount the IP file. The Stop function is similar to the start function.

3. Write the status () function

4. Test start, move the script written to/etc/init.d/clearly for Bossnfs

    • How to implement automatic mount with AutoFS

The automatic mount here, as we have previously learned to add to the/etc/fstab, he is when you visit a directory, our share will hang in this access directory, even if this directory does not exist. Here we can use AutoFS to implement auto mount, install service, yum install Autofs,service autofs restart. There are two requirements, the first is to visit/mnt/boss this directory, automatically mount 172.16.65.128:/share to this boss directory. The second is to access the/boss directory automatically automatically mount 172.16.65.128:/share to this boss directory.

Requirements 1. Modify the configuration file, Vim/etc/auto.master, add/mnt this directory

Write/etc/auto.mnt this file:

Requirement 2: Modify the configuration file, Vim/etc/auto.master, add/boss to this directory, it should be noted here, the root directory to use/-to indicate

Write the/etc/auto.root file: It is important to note that the Boss directory is in the root directory, so use absolute path.

Test: Successful Mount

    • File lock and Signal capture trap

1. What is file lock, file lock is when we run a service, another terminal is not running at the same time, this is the function of file lock, then we write a simple script to test the function of file lock:

Test, I run in one terminal, the other terminal also began to run the script, but wrong, prompt:

2. Signal capture, we in the above script execution, press CTRL + C, run to terminate, but the lock file is not deleted, always exists, so the next time the script runs, it will not run, unless we manually delete the lock file, so we have to capture CTRL + C this signal, Then, by capturing this signal, we perform certain operations so that our lock files do not persist.

Requirement 1: Make the above service script in the process of execution, can not press CTRL + C, I changed the script to the end of the following: On behalf of my trap 2 signal, "" There is nothing, indicating that after capturing the signal do nothing, after deleting the $flock lock file, we release 2 signal.


Requirement 2: We are allowed to press CTRL + C while the script is running, but after pressing, the script must automatically delete the lock file, then the next time you run the lock file will not be prompted to run, here we only need to modify the capture signal 2, delete the lock file and then exit.


    • Sed stream File Editor

Sed is called a stream file editor or a non-interactive editor, and SED is suitable for editing files in scripts because Vim cannot be used in scripts. Sed the main usage of the increase and deletion of the search, this can see the man manual, there is a lot of practice. Tomorrow I will provide an sed file that will summarize all the uses of SED basically. I hope you will pay attention!

Summarize:

Today mainly on the shell scripting in the future we will often contact, the service script to write, in fact, learn to write one, the other is appeased, also explained the SED stream file editor, because in the script, we can not use VIM to modify the configuration file.

Turn from:

"Day 20th" shell Programming service scripting, File lock and signal capture-today's headline (toutiao.com)
http://toutiao.com/a6321640633670009090/

Shell Programming service scripting, file lock, and signal capture

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.