When a Web website is hosted on an operational Linux system, it is often necessary to maintain the website, such as checking resource usage and responding accordingly, log segmentation, data sorting, and performing specific tasks in specific states. etc. In order to realize the automated execution of these operations, the Linux system provides a convenient way. This article will introduce common Linux automation task implementation methods.
The benefits of automating tasks are as follows:
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 commandUse the chkconfig command to start specific services or programs at different startup levels.
Let’s talk about the running level of linux first:
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 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.
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.
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.
我们经常使用的是crontab命令是cron table的简写,它是cron的配置文件,也可以叫它作业列表,我们可以在以下文件夹内找到相关配置文件。
我们常用的命令如下:
crontab [-u username] //省略用户表表示操作当前用户的crontab -e (编辑工作表) -l (列出工作表里的命令) -r (删除工作作)
我们用crontab -e进入当前用户的工作表编辑,是常见的vim界面。每行是一条命令。
crontab的命令构成为 时间+动作,其时间有分、时、日、月、周五种,操作符有
*
取值范围内的所有数字/
每过多少个数字-
从X到Z,
散列数字以下是几个例子。
时间 注释 0 0 25 12 * //在12月25日的0时0分 */5 * * * * //每过5分钟 * 4-6 * * * //每天的4 5 6点 * * * * 2,5 //每周二和周五
如果我们的命令有逻辑判断等非常复杂的操作时,再直接编辑crontab就有点困难了,这时,我们可以使用shell脚本。其来历,分类定义与题不符,不再多说,我们直接说它的用法。
我们用vim /usr/sh/test.sh来使用vim编辑一个shell脚本
#!/bin/sh //声明开始shell脚本 a = "hello world" //定义一个shell变量 echo $a //熟悉的echo,输出a变量
然后crontab -e编辑crontab,添加 */5 * * * * /usr/sh/test.sh
每隔五分钟运行一次test.sh脚本,也可以用 /phppath/php /filepath/test.php
来用php进程来执行php程序。
如果您觉得本博文对您有帮助,您可以推荐或关注我,如果您有什么问题,可以在下方留言讨论,谢谢。
The above is the detailed content of These Linux 'automation' skills teach you to complete tasks easily. For more information, please follow other related articles on the PHP Chinese website!