"Automation" of Linux

Source: Internet
Author: User

Source: http://www.cnblogs.com/zhenbianshu/p/5042303.html"Automation" of Linux

When the Web site of a Linux system is operational, we often need to maintain the site, such as reviewing the remaining resources and responding, splitting the logs, sorting the data, performing specific tasks in a specific state, and so on, all of which require Linux to perform some tasks automatically. This blog post describes how to perform common Linux automation tasks.

Implementing automation has the following benefits:

    1. Save manpower, one script is enough.
    2. Automatic night-time can avoid the peak traffic, does not affect the efficiency of the site during the day.
    3. Accurate, well-set situation, will not make mistakes.
    4. Of course, the most important thing is to worry about, do not have to knock on certain orders frequently.
Boot up

Start-up should be our very frequent needs, we often need to automatically execute certain commands at boot to open the service, process, etc., with it we do not have to enter the same heap every time the command.

Chkconfig command

Use the Chkconfig command to start a specific service or program at a different startup level.

Let's start with the Linux operating level:

    • Level 0 means: shutdown
    • Level 1 means: Single user mode
    • Level 2: Multi-user command line mode with no network connection
    • Level 3 means: Multi-user command line mode with network connection
    • Level 4 means: not available
    • Level 5 means: Multi-user mode with graphical interface
    • Level 6 means: reboot

The Chkconfig command is as follows:

chkconfig --list //命令查看已设置的开启自启动列表。xxxd 0:off 1:off 2:on ... 6:off //list的结果,表示在xxxd服务在启动级别为2 3 4 5 的情况下会自动启动。 chkconfig --add xxxd//向任务列表中添加一个xxxd服务chkconfig [--level 1/2/../6] xxxd on/off//设置xxxd用服务在n状态为开/关,[]内省略则在2345级别开启chkconfig --del xxxd //将任务列表中的xxxd服务删除
Editing of RC.D files

You can also directly edit the files in the/etc/rc.d/directory to enable startup. There are many files in this directory, RCN.D is the Startup folder in the case of the boot status N, rc, Rc.sysinit, INIT.D are the system's module or the system settings of the self-starter file [clamp].

We use vim rc.local to edit the rc.local file to customize our own self-starter program. The command is very simple, just as usual in operation. Such as/usr/local/apache/bin/apachectl start indicates that the Apache server is booting from.

At implementation timed tasks

At is a simple function simple task program, it can only be a one-time scheduled tasks, the use of the following:

#at time      //at加时间启动at命令at>operation    //输入要执行的操作at>Ctrl+D      //按Ctrl+D退出命令编辑

The common form of time is as follows

at H:m tomorrow     //第二天的H点m分at now + n minutes/hours/days/weeks  //在n分/时/天/周后at midnight         //在午夜=-=at H:m pm/am        //在当天上午/下午的H点m分

We can also view the current command for at in the /var/spool/at file. It is also important to note that the Linux default ATD process is turned off and needs to be opened manually.

Crontab implementation of timed tasks

Linux built-in cron process can help us to achieve these requirements, cron with shell script, very complex instructions are no problem.

Cron Introduction

The cron daemon is a small subsystem of utilities and configuration files that can find a style of cron on almost all UNIX-like systems, and we can use PS aux|grep cron to find the Crond daemon.

We often use the crontab command is the shorthand for cron table, it is a cron configuration file, or it can be called a job list, we can find the relevant configuration file in the following folder.

    • The/var/spool/cron/directory contains crontab tasks for each user, including root, and each task is named after the creator's name
    • /etc/crontab This file is responsible for scheduling various management and maintenance tasks.
    • /etc/cron.d/This directory is used to store any crontab files or scripts to be executed.
    • We can also put the script in the/etc/con.hourly,/etc/con.daily,/etc/con.weekly,/etc/con.monthly directory, and let it execute once per hour/day/week, month.
Use of crontab

The commands we use are as follows:

crontab [-u username]    //省略用户表表示操作当前用户的crontab    -e      (编辑工作表)    -l      (列出工作表里的命令)    -r      (删除工作作)

We use crontab-e to enter the current user's worksheet editing, is a common vim interface. Each line is a command.

The command of Crontab is composed of time + action , its time is divided, hour, day, month, week five kinds, operator has

    • * All numbers within the value range
    • / Number of numbers per past
    • - from X to Z
    • , hash number

Here are a few examples.

时间                  注释0 0 25 12 *     //在12月25日的0时0分*/5 * * * *     //每过5分钟* 4-6 * * *     //每天的4 5 6点* * * * 2,5     //每周二和周五
With a simple shell script

If our command has a very complex operation such as logical judgment, then it is a bit difficult to edit the crontab directly, and then we can use the shell script. Its origin, classification definition and the problem is not, no longer say, we directly say its usage.

We use vim/usr/sh/test.sh to edit a shell script using vim

#!/bin/sh           //声明开始shell脚本a = "hello world"   //定义一个shell变量echo $a             //熟悉的echo,输出a变量

Then crontab-e edit crontab, add */5 * * * * /usr/sh/test.sh test.sh scripts to run every five minutes, or you can use /phppath/php/filepath/test.php to execute PHP programs with PHP processes.

"Automation" of Linux

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.