These Linux 'automation' skills teach you to complete tasks easily
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.
The benefits of automating tasks are as follows:
- Save manpower: Just write a script to automate tasks.
- Time flexibility: By automatically executing at night, you can avoid website traffic peak periods without affecting website efficiency during the day.
- Accuracy: When set up properly, automated tasks will be error-free.
- 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The key differences between CentOS and Ubuntu are: origin (CentOS originates from Red Hat, for enterprises; Ubuntu originates from Debian, for individuals), package management (CentOS uses yum, focusing on stability; Ubuntu uses apt, for high update frequency), support cycle (CentOS provides 10 years of support, Ubuntu provides 5 years of LTS support), community support (CentOS focuses on stability, Ubuntu provides a wide range of tutorials and documents), uses (CentOS is biased towards servers, Ubuntu is suitable for servers and desktops), other differences include installation simplicity (CentOS is thin)

CentOS installation steps: Download the ISO image and burn bootable media; boot and select the installation source; select the language and keyboard layout; configure the network; partition the hard disk; set the system clock; create the root user; select the software package; start the installation; restart and boot from the hard disk after the installation is completed.

CentOS has been discontinued, alternatives include: 1. Rocky Linux (best compatibility); 2. AlmaLinux (compatible with CentOS); 3. Ubuntu Server (configuration required); 4. Red Hat Enterprise Linux (commercial version, paid license); 5. Oracle Linux (compatible with CentOS and RHEL). When migrating, considerations are: compatibility, availability, support, cost, and community support.

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.

After CentOS is stopped, users can take the following measures to deal with it: Select a compatible distribution: such as AlmaLinux, Rocky Linux, and CentOS Stream. Migrate to commercial distributions: such as Red Hat Enterprise Linux, Oracle Linux. Upgrade to CentOS 9 Stream: Rolling distribution, providing the latest technology. Select other Linux distributions: such as Ubuntu, Debian. Evaluate other options such as containers, virtual machines, or cloud platforms.

Troubleshooting steps for failed Docker image build: Check Dockerfile syntax and dependency version. Check if the build context contains the required source code and dependencies. View the build log for error details. Use the --target option to build a hierarchical phase to identify failure points. Make sure to use the latest version of Docker engine. Build the image with --t [image-name]:debug mode to debug the problem. Check disk space and make sure it is sufficient. Disable SELinux to prevent interference with the build process. Ask community platforms for help, provide Dockerfiles and build log descriptions for more specific suggestions.

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)
