How to perform common Linux automation tasks
In Linux, when a web website is operating, we often need to maintain the website, such as checking the remaining resources and responding, splitting logs, organizing data, and executing specific tasks in a specific status, all these require linux to automatically execute certain tasks. This blog post describes how to perform common linux automation tasks.
The following benefits apply to "Automation:
Saving manpower. One script is enough.
Automatic execution at night can avoid peak website traffic without affecting website efficiency during the day.
Accurate. If the configuration is complete, no errors will occur.
Of course, the most important thing is worry-free. You don't need to frequently knock on some commands.
Start
We often need to automatically execute some commands to start services and processes when starting the system, with it, we don't have to input the same heap command every time we start the system.
Chkconfig command
You can use the chkconfig command to start specific services or programs at different startup levels.
First, let's talk about the linux running level:
Level 0: Shutdown
Level 1: Single User Mode
Level 2: multi-user command line mode without network connection
Level 3: multi-user command line mode with network connection
Level 4: unavailable
Level 5: multi-user mode with graphic interface
Level 6: restart
The command for chkconfig is as follows:
Chkconfig -- list // command to view the set list of enabled auto-start instances.
Xxxd 0: off 1: off 2: on... 6: off // list result, indicating that the xxxd service will be automatically started when the startup level is 2, 3, 4, 5.
Chkconfig -- add xxxd // add a xxxd service to the task list
Chkconfig [-- level 1/2/../6] xxxd on/off // set the service of xxxd to on/off in the n state, and enable the service at the 2345 level if it is omitted in [].
Chkconfig -- del xxxd // Delete the xxxd service from the task list
Edit the rc. d File
You can also directly edit the files in the/etc/rc. d/directory to enable automatic startup. This directory contains many files, rcn. d is the Startup Folder when the startup status is n, rc, rc. sysinit, init. d is the system module or the self-starting file [Folder] set by the system.
We use vim rc. local to edit the rc. local file to customize our own startup plan. The command is very simple, just as it is usually in operation. For example,/usr/local/apache/bin/apachectl start indicates that the apache server is automatically started when it is started.
At
At is a simple scheduled task program with simple functions. It can only perform one-time scheduled tasks. Its usage is as follows:
# At time // at plus time to start the at command
At> operation // enter the operation to be performed
At> Ctrl + D // press Ctrl + D to exit the command editing
The common time format is as follows:
At H: m tomorrow // the next day's H: m
At now + n minutes/hours/days/weeks // After n minutes/hour/day/week
At midnight // at midnight =-=
At H: m pm/am // at am/pm of the current day
You can also view the current at command in the/var/spool/at file. Note that the atd process is disabled by default in linux and needs to be enabled manually.
Crontab for scheduled tasks
The built-in cron process in linux can help us achieve these needs. cron works with shell scripts, and there are no problems with complicated commands.
Cron Introduction
The cron daemon is a small subsystem composed of utilities and configuration files. cron can be found in almost all UNIX-like systems, we can use ps aux | grep cron To Find The crond daemon.
We often use the crontab command, short for the cron table. It is a cron configuration file or a job list. We can find related configuration files in the following folders.
In the/var/spool/cron/directory, each user contains the root crontab task. Each task is named by the creator.
The/etc/crontab file schedules various management and maintenance tasks.
The/etc/cron. d/directory is used to store any crontab files or scripts to be executed.
We can also put the script in/etc/con. hourly,/etc/con. daily,/etc/con. weekly,/etc/con. in the monthly directory, run the command once every hour, day, week, or month.
Use of crontab
Common commands are as follows:
Crontab [-u username] // omitting the user table to operate the crontab of the current user
-E (edit a worksheet)
-L (list the commands in the work table)
-R (delete work)
We use crontab-e to enter the current user's worksheet editing, which is a common vim interface. Each line is a command.
The crontab command is composed of time + action, which has the following time types: minute, hour, day, month, and Friday. The operators include:
* All numbers in the value range
/How many digits each time
-From X to Z
, Hash number
The following are examples.
Time comment
0 0 25 12 * // at 00:00 on January 1, December 25
*/5 * // every 5 minutes
* 4-6 *** // every day
* *** // Two or five times a week
With simple shell scripts
It would be a little difficult to edit crontab directly if our commands have logical judgment and other complicated operations. At this time, we can use shell scripts. Its origins, classification definition and question are not consistent. We will not talk about it any more. We will talk about its usage directly.
We use vim/usr/sh/test. sh to edit a shell script using vim.
#! /Bin/sh // declaration start shell script a = "hello world" // define a shell variable echo $ a // familiar echo, output a variable
Then, edit crontab-e and add */5 */usr/sh/test. sh runs test every five minutes. sh script, you can also use/phppath/php/filepath/test. php to use the php process to execute the php program.
This article permanently updates the link address: