Table of Contents
Deep fake originality of Linux automation tasks" >Deep fake originality of Linux automation tasks
boot" >boot
at implements scheduled tasks" >at implements scheduled tasks
crontab implements scheduled tasks" >crontab implements scheduled tasks
cron introduction" >cron introduction
crontab的使用" >crontab的使用
配合简单的shell脚本" >配合简单的shell脚本
Home System Tutorial LINUX These Linux 'automation' skills teach you to complete tasks easily

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

Feb 14, 2024 pm 06:12 PM
linux linux tutorial linux system linux command shell script b net Getting started with linux linux 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服务删除
Copy after login

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退出命令编辑
Copy after login

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分
Copy after login

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      (删除工作作)
Copy after login

我们用crontab -e进入当前用户的工作表编辑,是常见的vim界面。每行是一条命令。

crontab的命令构成为 时间+动作,其时间有分、时、日、月、周五种,操作符有

  • * 取值范围内的所有数字
  • / 每过多少个数字
  • - 从X到Z
  • 散列数字

以下是几个例子。

时间                  注释
0 0 25 12 *     //在12月25日的0时0分
*/5 * * * *     //每过5分钟
* 4-6 * * *     //每天的4 5 6点
* * * * 2,5     //每周二和周五
Copy after login

配合简单的shell脚本

如果我们的命令有逻辑判断等非常复杂的操作时,再直接编辑crontab就有点困难了,这时,我们可以使用shell脚本。其来历,分类定义与题不符,不再多说,我们直接说它的用法。

我们用vim /usr/sh/test.sh来使用vim编辑一个shell脚本

#!/bin/sh           //声明开始shell脚本
a = "hello world"   //定义一个shell变量
echo $a             //熟悉的echo,输出a变量
Copy after login

然后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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1237
24
Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

git software installation git software installation Apr 17, 2025 am 11:57 AM

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)

How to use VSCode How to use VSCode Apr 15, 2025 pm 11:21 PM

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages ​​and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version

See all articles