Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Automation of linux

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to realize the automation of linux". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to realize the automation of linux".

Boot up

Boot should be our frequent requirement, we often need to automatically execute certain commands to start services, processes, etc., with it we no longer have to enter the same set of commands every time we boot.

Chkconfig command

Use the chkconfig command to start a specific service or program at different startup levels.

Let's first talk about the running level of linux:

Level 0 indicates that the computer is turned off

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 command for chkconfig is as follows:

Chkconfig-- list / / command to view the set open self-boot list.

The result of xxxd 0:off 1:off 2:on... 6:off / / list indicates that the xxxd service will start automatically when the startup level is 2 3 4 5.

Chkconfig-- add xxxd// adds a xxxd service to the task list

Chkconfig [--level 1 chkconfig... xxxd on/off//] sets the xxxd service on / off in n status, and the omission in [] will enable it at level 2345

Chkconfig-- del xxxd / / remove the xxxd service from the task list

Editing of rc.d files

You can also directly edit the files in the / etc/rc.d/ directory to achieve boot self-startup. 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 modules of the system or self-boot files of the system settings.

We use vim rc.local to edit the rc.local file to customize our self-boot plan. The command is very simple, just like you usually do. For example, / usr/local/apache/bin/apachectl start means to boot the apache server automatically.

Implementation of timing tasks with at

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 plus time to start at command

At > operation / / enter the action to be performed

At > Ctrl+D / / Press the Ctrl+D exit command to edit

The common forms of its time are as follows

At HRV m tomorrow / / H point m points on the second day

At now + n minutes/hours/days/weeks / / in n minutes / hours / days / weeks later

At midnight / / at midnight =

At Hrig m pm/am / / at H: M in the morning / afternoon of the day

We can also view the current command of at in the / var/spool/at file. It is also important to note that the linux default atd process is turned off and needs to be opened manually.

Implementation of timing tasks with crontab

Linux's built-in cron process can help us achieve these requirements, cron with shell scripts, very complex instructions are not a problem.

Cron introduction

The cron daemon is a small subsystem 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.

What we often use is that the crontab command is the abbreviation for cron table, which is the configuration file of cron, or the job list, which we can find in the following folder.

/ var/spool/cron/ directory stores crontab tasks for each user, including root, and each task is named after the creator

/ etc/crontab this file is responsible for scheduling various management and maintenance tasks.

The / etc/cron.d/ directory is used to hold 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 directory and have it executed every hour, day, week, and month.

The use of crontab

Our common commands are as follows:

1234crontab [- u username] / / omitting the user table means to operate the current user's crontab-e (edit worksheet)-l (list the commands in the worksheet)-r (delete work)

We use crontab-e to enter the current user's worksheet editing, which is a common vim interface. Each line is an order.

The command of crontab is composed of time + action, which can be divided into minutes, hours, days, months and Fridays, and the operators are

* all numbers in the value range

/ how many digits per pass

-from X to Z

, hash number

Here are a few examples.

Time note

12340 0 25 12 * / at 00:00 on December 25th * / 5 * / every 5 minutes * 4-6 * / every day at 456 * 2Jing 5 / / every Tuesday and Friday

With a simple shell script

If our command has very complex operations such as logical judgment, it will be a bit difficult to edit crontab directly, so we can use shell scripts. Its origin, classification definition is not in line with the title, no more, let's talk about its usage directly.

We use vim / usr/sh/test.sh to edit a shell script using vim

123bind _ hello world sh / / declare to start the shell script a = "hello world" / / define a shell variable echo $a / / familiar echo, output the a variable

Then crontab-e edits the crontab and adds * / 5 * / usr/sh/test.sh to run the test.sh script every five minutes, or you can use / phppath/php / filepath/test.php to execute the php program with the php process.

Thank you for reading, the above is the content of "how to realize the automation of linux". After the study of this article, I believe you have a deeper understanding of how to realize the automation of linux, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report