Home  >  Article  >  System Tutorial  >  These Linux "automation" skills teach you to complete tasks easily

These Linux "automation" skills teach you to complete tasks easily

PHPz
PHPzforward
2024-02-14 18:12:13506browse

Deep fake originality of Linux automation tasks

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.

这些 Linux 的“自动化”技巧,教你轻松完成任务

The benefits of automating tasks are as follows:

  1. Save manpower: Just write a script to automate tasks.
  2. Time flexibility: By automatically executing at night, you can avoid website traffic peak periods without affecting website efficiency during the day.
  3. Accuracy: When set up properly, automated tasks will be error-free.
  4. Most importantly, automating tasks saves people the hassle of typing 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 commandUse 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: means shutdown
  • Level 1 means: single user mode
  • Level 2 means: multi-user command line mode without NFS function
  • Level 3 means: There is a multi-user command line mode with NFS function
  • 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.

我们经常使用的是crontab命令是cron table的简写,它是cron的配置文件,也可以叫它作业列表,我们可以在以下文件夹内找到相关配置文件。

  • /var/spool/cron/ 目录下存放的是每个用户包括root的crontab任务,每个任务以创建者的名字命名
  • /etc/crontab 这个文件负责调度各种管理和维护任务。/etc/cron.d/ 这个目录用来存放任何要执行的crontab文件或脚本。
  • 我们还可以把脚本放在/etc/con.hourly、/etc/con.daily、/etc/con.weekly、/etc/con.monthly目录中,让它每小时/天/星期、月执行一次。

crontab的使用

我们常用的命令如下:

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     //每周二和周五

配合简单的shell脚本

如果我们的命令有逻辑判断等非常复杂的操作时,再直接编辑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!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete