search
HomeSystem TutorialLINUXThese 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
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
What are the considerations for hardening a Linux system compared to hardening a Windows system?What are the considerations for hardening a Linux system compared to hardening a Windows system?May 10, 2025 am 12:08 AM

The strategies for hardening Linux and Windows systems vary: 1. Linux hardening includes minimized installation, user and permission management, firewall configuration, and the use of SELinux or AppArmor. 2. Windows hardening policies involve enabling WindowsDefender, configuring group policies, using Windows updates and patch management.

Warehouse: A GUI for Effortlessly Handling Flatpak AppsWarehouse: A GUI for Effortlessly Handling Flatpak AppsMay 09, 2025 am 11:30 AM

A GUI for Effortless Flatpak Management: Introducing Warehouse Managing a growing collection of Flatpak applications can be cumbersome using only the command line. Enter Warehouse, a user-friendly graphical interface designed to streamline Flatpak a

8 Powerful Linux Commands to Identify Hard Drive Bottlenecks8 Powerful Linux Commands to Identify Hard Drive BottlenecksMay 09, 2025 am 11:03 AM

This article provides a comprehensive guide to identifying and resolving hard drive bottlenecks in Linux systems. Experienced server administrators will find this particularly useful. Slow disk operations can severely impact application performance,

4 Best QR Code Generators for Linux Users4 Best QR Code Generators for Linux UsersMay 09, 2025 am 10:27 AM

Efficient QR code generation tool under Linux system In today's digital world, QR codes have become a way to quickly and conveniently share information, simplifying data access from URLs, texts, contacts, Wi-Fi credentials, and even payment information. Linux users can use a variety of tools to create QR codes efficiently. Let's take a look at some popular QR code generators that can be used directly on Linux systems. QRencode QRencode is a lightweight command line tool for generating QR codes on Linux. It is well-received for its simplicity and efficiency and is popular with Linux users who prefer direct methods. Using QRencode, you can use the URL,

elementary OS 8: A User-Friendly Linux for macOS and Windowselementary OS 8: A User-Friendly Linux for macOS and WindowsMay 09, 2025 am 10:19 AM

Elementary OS 8 Circe: A Smooth and Stylish Linux Experience Elementary OS, a Ubuntu-based Linux distribution, has evolved from a simple theme pack into a fully-fledged, independent operating system. Known for its user-friendly interface, elegant de

40  Linux Commands for Every Machine Learning Engineer40 Linux Commands for Every Machine Learning EngineerMay 09, 2025 am 10:06 AM

Mastering Linux is crucial for any machine learning (ML) engineer. Its command-line interface offers unparalleled flexibility and control, streamlining workflows and boosting productivity. This article outlines essential Linux commands, explained fo

Arch Linux Cheat Sheet: Essential Commands for BeginnersArch Linux Cheat Sheet: Essential Commands for BeginnersMay 09, 2025 am 09:54 AM

Arch Linux: A Beginner's Command-Line Cheat Sheet Arch Linux offers unparalleled control but can feel daunting for newcomers. This cheat sheet provides essential commands to confidently manage your system. System Information & Updates These com

How to Install Scikit-learn for Machine Learning on LinuxHow to Install Scikit-learn for Machine Learning on LinuxMay 09, 2025 am 09:53 AM

This guide provides a comprehensive walkthrough of installing and using the Scikit-learn machine learning library on Linux systems. Scikit-learn (sklearn) is a powerful, open-source Python library offering a wide array of tools for various machine l

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

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

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.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment