Learn these Linux 'automations' to easily complete tasks
When the web website of the Linux system is in operation, we often need to maintain the website, such as checking the remaining resources and responding, log segmentation, data sorting, performing specific tasks in specific states, etc., all of which require Linux capabilities Achieve automatic execution of certain tasks. This blog post introduces how to perform common Linux automation tasks.

"Automation" of Linux
Implementing "automation" has the following benefits:
Save manpower, one script is enough.
Automatic execution at night can avoid the peak period of website traffic and does not affect the efficiency of the website during the day.
Accurate, if the settings are perfect, there will be no mistakes.
Of course, the most important thing is to save worry, because you don’t have to type 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 command
Use 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: shut down
Level 1 means: single user mode
Level 2 means: multi-user command line mode without network connection
Level 3 means: multi-user command line mode with network connection
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.
We often use the crontab command, which is the abbreviation of cron table. It is the cron configuration file, which can also be called the job list. We can find the relevant configuration files in the following folders.
The/var/spool/cron/ directory stores crontab tasks for each user including root. Each task is named after the creator.
/etc/crontab This file is responsible for scheduling various management and maintenance tasks.
/etc/cron.d/ This directory is used to store any crontab files or scripts to be executed.
We can also put the script in the /etc/con.hourly, /etc/con.daily, /etc/con.weekly, /etc/con.monthly directories to make it hourly/day/weekly, monthly Execute once.
Use of crontab
Our commonly used commands are as follows:
crontab [-u username] //省略用户表表示操作当前用户的crontab -e (编辑工作表) -l (列出工作表里的命令) -r (删除工作作)
We use crontab -e to enter the current user's worksheet editing, which is a common vim interface. Each line is a command.
The crontab command is composed of time actions. The time includes minutes, hours, days, months, and Fridays. The operators are
* All numbers within the value range
/How many digits have passed each time
– From X to Z
, hash number
Here are a few examples.
时间 注释 0 0 25 12 * //在12月25日的0时0分 */5 * * * * //每过5分钟 * 4-6 * * * //每天的4 5 6点 * * * * 2,5 //每周二和周五
With simple shell script
If our command has very complex operations such as logical judgment, it will be a bit difficult to edit crontab directly. In this case, we can use shell script. Its origin and classification definition do not match the title, so I won’t go into more details. Let’s just talk about its usage.
We use vim /usr/sh/test.sh to edit a shell script using vim
#!/bin/sh //声明开始shell脚本 a = "hello world" //定义一个shell变量 echo $a //熟悉的echo,输出a变量
Then crontab -e edit crontab, add */5 * * * * /usr/sh/test.sh to run the test.sh script every five minutes, you can also use /phppath/php /filepath/test.php To use the php process to execute the php program.
If you think this blog post is helpful to you, you can recommend or follow me. If you have any questions, you can leave a message below for discussion. Thank you.
The above is the detailed content of Learn these Linux 'automations' to easily complete tasks. 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

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

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 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.

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

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.

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.

Causes and solutions for the VS Code terminal commands not available: The necessary tools are not installed (Windows: WSL; macOS: Xcode command line tools) Path configuration is wrong (add executable files to PATH environment variables) Permission issues (run VS Code as administrator) Firewall or proxy restrictions (check settings, unrestrictions) Terminal settings are incorrect (enable use of external terminals) VS Code installation is corrupt (reinstall or update) Terminal configuration is incompatible (try different terminal types or commands) Specific environment variables are missing (set necessary environment variables)

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.

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →

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.
