search
HomeSystem TutorialLINUXLearn these Linux 'automations' to easily complete tasks

Learn these Linux 'automations' to easily complete tasks

Feb 13, 2024 am 08:24 AM
linuxlinux tutoriallinux systemlinux commandshell scriptb netembeddedlinuxGetting started with linuxlinux learning

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.

Learn these Linux automations to easily complete 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!

Statement
This article is reproduced at:良许Linux教程网. If there is any infringement, please contact admin@php.cn delete
Explain the architectural differences between Linux and Windows.Explain the architectural differences between Linux and Windows.May 06, 2025 am 12:01 AM

The main differences in architecture between Linux and Windows include: 1) Design philosophy and kernel structure: Linux uses a modular kernel, Windows uses a single kernel; 2) File system: Linux supports multiple file systems, Windows mainly uses NTFS; 3) Security: Linux is known for its permission management and open source features. Windows has a unique security mechanism but lags in repair; 4) Usage experience: Linux command line operation is more efficient, and Windows graphical interface is more intuitive.

What are some common security threats targeting Linux versus Windows?What are some common security threats targeting Linux versus Windows?May 05, 2025 am 12:03 AM

Linux and Windows systems face different security threats. Common Linux threats include Rootkit, DDoS attacks, exploits, and permission escalation; common Windows threats include malware, ransomware, phishing attacks, and zero-day attacks.

How does process management differ between Linux and Windows?How does process management differ between Linux and Windows?May 04, 2025 am 12:04 AM

The main difference between Linux and Windows in process management lies in the implementation and concept of tools and APIs. Linux is known for its flexibility and power, relying on kernel and command line tools; while Windows is known for its user-friendliness and integration, mainly managing processes through graphical interfaces and system services.

What are the typical use cases for Linux versus Windows?What are the typical use cases for Linux versus Windows?May 03, 2025 am 12:01 AM

Linuxisidealforcustomization,development,andservermanagement,whileWindowsexcelsineaseofuse,softwarecompatibility,andgaming.Linuxoffershighconfigurabilityfordevelopersandserversetups,whereasWindowsprovidesauser-friendlyinterfaceandbroadsoftwaresupport

What are the differences in user account management between Linux and Windows?What are the differences in user account management between Linux and Windows?May 02, 2025 am 12:02 AM

The main difference between Linux and Windows in user account management is the permission model and management tools. Linux uses Unix-based permissions models and command-line tools (such as useradd, usermod, userdel), while Windows uses its own security model and graphical user interface (GUI) management tools.

How does the command line environment of Linux make it more/less secure than Windows?How does the command line environment of Linux make it more/less secure than Windows?May 01, 2025 am 12:03 AM

Linux'scommandlinecanbemoresecurethanWindowsifmanagedcorrectly,butrequiresmoreuserknowledge.1)Linux'sopen-sourcenatureallowsforquicksecurityupdates.2)Misconfigurationcanleadtovulnerabilities.Windows'commandlineismorecontrolledbutlesscustomizable,with

How to Make a USB Drive Mount Automatically in LinuxHow to Make a USB Drive Mount Automatically in LinuxApr 30, 2025 am 10:04 AM

This guide explains how to automatically mount a USB drive on boot in Linux, saving you time and effort. Step 1: Identify Your USB Drive Use the lsblk command to list all block devices. Your USB drive will likely be labeled /dev/sdb1, /dev/sdc1, etc

Best Cross-Platform Apps for Linux, Windows, and Mac in 2025Best Cross-Platform Apps for Linux, Windows, and Mac in 2025Apr 30, 2025 am 09:57 AM

Cross-platform applications have revolutionized software development, enabling seamless functionality across operating systems like Linux, Windows, and macOS. This eliminates the need to switch apps based on your device, offering consistent experien

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment