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 Planning Task in linux system

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to realize the planned task in the linux system". The content in the article 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 planned task in the linux system".

First, customize the planned tasks

1. Atd service (one-time)

1. The command corresponding to atd service-"at"

Install first when using the at command

[root@localhost lianxi] # yum install at-y

Then you have to start the atd service, or you won't perform the task regularly.

[root@localhost lianxi] # service atd startRedirecting to / bin/systemctl start atd.service

The examples are as follows:

[root@localhost lianxi] # at 11:00 # create an one-time scheduled task at > bash / root/wang.shat > # ctrl+d exit job 5 at Wed Dec 22 11:00:00 2021 [root@localhost lianxi] # at-L3 Wed Dec 22 10:20:00 2021 a root5 Wed Dec 22 11:00:00 2021 a root [root@localhost lianxi] # [root@localhost lianxi] # atrm 3 # Delete scheduled task number 3 Task [root@localhost lianxi] # at-l # View scheduled task list 5 Wed Dec 22 11:00:00 2021 a root [root@localhost lianxi] #

2. The directory where one-time scheduled tasks are stored: / var/spool/at

The batch command is also equivalent to the at command, except that it runs scheduled tasks when the system load is low.

Uptime command:

[root@localhost lianxi] # uptime # check how long the system has been turned on, and the average load of the system 10:28:33 up 4 days, 16:13, 2 users, load average: 0.00,0.01,0.05

2. The crond service (periodic) service is installed by default and starts automatically when booted.

1. Command corresponding to crond service-"crontab is a command to create and manage periodic scheduled tasks."

-e means create a scheduled task (edit)

-l View the list of scheduled tasks

2. Directory where periodically scheduled tasks are stored: / var/spool/cron

Configuration file for cron service: / etc/crontab

Log file for the cron service: / var/log/cron (you can know whether a scheduled task is executed (CMD))

The format is shown in the figure

Example: "example 1 (root user)"

The sshd service is automatically turned on at 7:50 every morning and shuts down at 22:00

Clear the FTP server public directory Ivar/ftp/pub at 12:00 every five days

Restart the httpd service at 7: 30 every Saturday

At 17:30 every Monday, Wednesday and Friday, pack the backup / etc/httpd directory

[root@localhost lianxi] # crontab-ecrontab: installing new crontab [root@localhost lianxi] # crontab-L303 * bash / root/sc.sh50 7 * service sshd start50 22 * service sshd stop0 12 * / 5 * * rm-rf / var/ftp/pub/*30 7 * * 6 service httpd restart30 17 * * 1 var/ftp/pub/*30 5 tar czf / backup/httpd.tar.gz / etc/httpd30 4 * / bin/bash / lianxi/backup/backup_ log.sh [root @ localhost lianxi] #

3. The meaning of d after atd service and crond service

D-"daemon (daemon): a process that has been running in memory until we stop it artificially, otherwise it has been running in memory, because it has been running in memory, so our users can access it at any time, so it has been guarding you, waiting for your arrival.

Second, synchronization time

In centos7: use the ntpdate command

# first step install [root@localhost lianxi] # yum install ntpdate-y # second step [root@localhost lianxi] # date-s "2021-12-22 15:6:12" # modified time Wednesday, December 22, 2021, 15:06:12 CST # third step [root@localhost lianxi] # ntpdate time.windows.com22 Dec 11:59:38 ntpdate [13102]: step time server 20.189.79.72 offset-11225.674351 sec [root@localhost lianxi] # date2021 Wednesday, 22 December 2000, 11:59:42 CST

In centos8 or 7: using chrony

[root@localhost lianxi] # yum install chrony-y # install [root@localhost lianxi] # service chronyd restart # restart Service Redirecting to / bin/systemctl restart chronyd.service [root@localhost lianxi] # date-s "2021-12-22 15:6:12" Wednesday, 22 December 2021, 15:06:12 CST [root@localhost lianxi] # date2021 Wednesday 22 December 12:02:38 CST III, exercise

1. Write a script / backup/backup_log.sh to back up all the files under the / var/log directory to the / backup directory, requiring the file name to include the date of the day, accurate to seconds, for example: 2016-6-6-2_30_20-log.tar.gz.

At the same time, it is required to delete the backup files from seven days ago in the / backup directory, and only keep the files from the last seven days.

two。 To execute as the root user, the requirement for the scheduled task is to execute the above script / backup/backup_log.sh at 4:30 every day

[root@lamp-test backup] # pwd/backup [root@lamp-test backup] # cat backup_log.sh #! / bin/bashmkdir-p / backuptar-czf / backup/$ (date +% F_%H_%M_%S)-log.tar.gz / var/log/*find / backup-mtime + 7-type f-name "* .tar.gz"-exec rm-rf {}\ [root@lamp-test backup] # crontab-L304 * bash / backup/backup_ log.sh IV, security issues

1. How to identify illegal planned tasks? Where are you going to see it? Where will the crond process find the script that needs to be executed?

1./var/spool/cron-"where user-defined scheduled tasks are stored

two。 The planned task of the operating system itself-(hitchhiking)

[root@localhost lianxi2] # ls / etc/cron.*/etc/cron.deny / etc/cron.d:0hourly / etc/cron.daily: script to be executed per hour logrotate man-db.cron / etc/cron.hourly: script to be executed per hour 0anacron / etc/cron.monthly: script to be executed every month / etc/cron.weekly: script executed weekly [root@localhost lianxi2] # cat / etc/cron.deny Users who disable the crond service can write to it

2. The case of hitchhiking

Hitchhiking example: [root@lamp-test cron.hourly] # pwd/etc/cron.hourly [root@lamp-test cron.hourly] # ls0anacron poweroff.sh [root@lamp-test cron.hourly] # chmod + x poweroff.sh [root@lamp-test cron.hourly] # cat poweroff.sh init 0 [root@lamp-test log] # cd / etc/cron.hourly/ [root@lamp-test cron.hourly] # ls0anacron poweroff.sh [root@lamp-test cron.hourly] # rm-rf poweroff.sh Thank you for reading The above is the content of "how to realize the planned task in the linux system". After the study of this article, I believe you have a deeper understanding of how to realize the planned task in the linux system, 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

Development

Wechat

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

12
Report