search
HomeSystem TutorialLINUXThese Linux 'automation' skills teach you to complete tasks easily
These Linux 'automation' skills teach you to complete tasks easilyFeb 14, 2024 pm 06:12 PM
linuxlinux tutoriallinux systemlinux commandshell scriptb netGetting started with linuxlinux learning

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:良许Linux教程网. If there is any infringement, please contact admin@php.cn delete
什么是linux设备节点什么是linux设备节点Apr 18, 2022 pm 08:10 PM

linux设备节点是应用程序和设备驱动程序沟通的一个桥梁;设备节点被创建在“/dev”,是连接内核与用户层的枢纽,相当于硬盘的inode一样的东西,记录了硬件设备的位置和信息。设备节点使用户可以与内核进行硬件的沟通,读写设备以及其他的操作。

Linux中open和fopen的区别有哪些Linux中open和fopen的区别有哪些Apr 29, 2022 pm 06:57 PM

区别:1、open是UNIX系统调用函数,而fopen是ANSIC标准中的C语言库函数;2、open的移植性没fopen好;3、fopen只能操纵普通正规文件,而open可以操作普通文件、网络套接字等;4、open无缓冲,fopen有缓冲。

linux中什么叫端口映射linux中什么叫端口映射May 09, 2022 pm 01:49 PM

端口映射又称端口转发,是指将外部主机的IP地址的端口映射到Intranet中的一台计算机,当用户访问外网IP的这个端口时,服务器自动将请求映射到对应局域网内部的机器上;可以通过使用动态或固定的公共网络IP路由ADSL宽带路由器来实现。

什么是linux交叉编译什么是linux交叉编译Apr 29, 2022 pm 06:47 PM

在linux中,交叉编译是指在一个平台上生成另一个平台上的可执行代码,即编译源代码的平台和执行源代码编译后程序的平台是两个不同的平台。使用交叉编译的原因:1、目标系统没有能力在其上进行本地编译;2、有能力进行源代码编译的平台与目标平台不同。

linux中eof是什么linux中eof是什么May 07, 2022 pm 04:26 PM

在linux中,eof是自定义终止符,是“END Of File”的缩写;因为是自定义的终止符,所以eof就不是固定的,可以随意的设置别名,linux中按“ctrl+d”就代表eof,eof一般会配合cat命令用于多行文本输出,指文件末尾。

linux怎么判断pcre是否安装linux怎么判断pcre是否安装May 09, 2022 pm 04:14 PM

在linux中,可以利用“rpm -qa pcre”命令判断pcre是否安装;rpm命令专门用于管理各项套件,使用该命令后,若结果中出现pcre的版本信息,则表示pcre已经安装,若没有出现版本信息,则表示没有安装pcre。

linux怎么查询mac地址linux怎么查询mac地址Apr 24, 2022 pm 08:01 PM

linux查询mac地址的方法:1、打开系统,在桌面中点击鼠标右键,选择“打开终端”;2、在终端中,执行“ifconfig”命令,查看输出结果,在输出信息第四行中紧跟“ether”单词后的字符串就是mac地址。

linux中rpc是什么意思linux中rpc是什么意思May 07, 2022 pm 04:48 PM

在linux中,rpc是远程过程调用的意思,是Reomote Procedure Call的缩写,特指一种隐藏了过程调用时实际通信细节的IPC方法;linux中通过RPC可以充分利用非共享内存的多处理器环境,提高系统资源的利用率。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools