When the web website of the Linux system is in operation, we often need to maintain the website, such as checking the remaining resources and responding, log segmentation, data sorting, performing specific tasks in specific states, etc., all of which require Linux capabilities Achieve automatic execution of certain tasks. This blog post introduces how to perform common Linux automation tasks.
"Automation" of Linux
Implementing "automation" has the following benefits:
Save manpower, one script is enough.
Automatic execution at night can avoid the peak period of website traffic and does not affect the efficiency of the website during the day.
Accurate, if the settings are perfect, there will be no mistakes.
Of course, the most important thing is to save worry, because you don’t have to type certain commands frequently.
boot
Starting at boot should be a very common need for us. We often need to automatically execute certain commands to start services, processes, etc. when booting up. With it, we no longer have to enter the same bunch of commands every time we boot up.
chkconfig command
Use the chkconfig command to start specific services or programs at different startup levels.
Let’s talk about the running level of linux first:
Level 0 means: shut down
Level 1 means: single user mode
Level 2 means: multi-user command line mode without 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: Restart
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 file
You can also directly edit the files in the /etc/rc.d/ directory to achieve automatic startup at boot. There are many files in this directory. rcn.d is the startup folder when the startup status is n. rc, rc.sysinit, and init.d are all system modules or self-starting files [folders] set by the system.
We use vim rc.local to edit the rc.local file to customize our own self-starting plan. The commands are very simple, just like normal operations. For example, /usr/local/apache/bin/apachectl start means starting the apache server automatically after booting.
at implements scheduled tasks
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加时间启动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 of at in the /var/spool/at file. It should also be noted that the atd process is closed by default in Linux and needs to be opened manually.
crontab implements scheduled tasks
The built-in cron process of Linux can help us achieve these needs. With cron and shell scripts, there is no problem with very complex instructions.
cron introduction
The cron daemon is a small subsystem composed of utilities and configuration files. Some style of cron can be found on almost all UNIX-like systems. We can use ps aux|grep cron to find the crond daemon.
We often use the crontab command, which is the abbreviation of cron table. It is the cron configuration file, which can also be called the job list. We can find the relevant configuration files in the following folders.
The/var/spool/cron/ directory stores crontab tasks for each user including root. Each task is named after the creator.
/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 directories to make it hourly/day/weekly, monthly Execute once.
Use of crontab
Our commonly used commands are as follows:
crontab [-u username] //省略用户表表示操作当前用户的crontab -e (编辑工作表) -l (列出工作表里的命令) -r (删除工作作)
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 actions. The time includes minutes, hours, days, months, and Fridays. The operators are
* All numbers within the value range
/How many digits have passed each time
– 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 simple shell script
If our command has very complex operations such as logical judgment, it will be a bit difficult to edit crontab directly. In this case, we can use shell script. Its origin and classification definition do not match the title, so I won’t go into more details. Let’s just talk about 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 to run the test.sh script every five minutes, you can also use /phppath/php /filepath/test.php To use the php process to execute the php program.
If you think this blog post is helpful to you, you can recommend or follow me. If you have any questions, you can leave a message below for discussion. Thank you.
The above is the detailed content of Learn these Linux 'automations' to easily complete tasks. For more information, please follow other related articles on the PHP Chinese website!