Home  >  Article  >  System Tutorial  >  Linux CronTab scheduled tasks to easily realize automated operation and maintenance

Linux CronTab scheduled tasks to easily realize automated operation and maintenance

WBOY
WBOYforward
2024-02-13 14:10:02596browse

In daily Linux server management, we often need to perform certain operations at specific times, such as backing up data, cleaning up junk files, etc. This requirement can usually be accomplished by manually executing commands, but with the increase in business and data volume, manual execution can no longer meet our requirements. Therefore, in this article, we will introduce to you how to use Linux CronTab scheduled tasks and how to improve efficiency in automated operation and maintenance.

Linux CronTab 定时任务,轻松实现自动化运维

The crondtab process will regularly check whether there is a task to be executed every minute. If there is a task to be executed, the task will be automatically executed. This article mainly introduces the scheduled task crontab in Shell. Friends who need it can refer to it

crondtab scheduled task

crondtab is a daemon process used under Linux to periodically perform certain tasks or wait for processing certain events. Similar to scheduled tasks under Windows, the crondtab process will regularly check whether there are tasks to be executed every minute. If If there is a task to be performed, the task is automatically performed.

How to make the shell script execute regularly every day?

1. Create a new shell script that needs to be executed regularly, here is the date.sh script.

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "hello world"
echo "----------------------------------------------------------------------------"
endDate=`date +"%Y-%m-%d %H:%M:%S"`
echo "★[$endDate] Successful"
echo "当前时间":$(date +"%Y-%m-%d %H:%M:%S") >> /www/wwwroot/date.txt
echo "----------------------------------------------------------------------------"

2. Add this date.sh script to the scheduled task, just run the "crontab -e" command to add it.

[root@localhost ~]# crontab -e
*/1 * * * * /www/wwwroot/date.sh > /dev/null 2>&1
*/1 * * * * /www/wwwroot/date.sh >> /www/wwwroot/result.txt 2>&1
  
# 每天10:30执行
30 10 * * * /www/wwwroot/date.sh

3. Finally, let’s check the output file date.txt and crontab log.

[root@localhost ~]# tail -n 5 /www/wwwroot/date.txt
[root@localhost ~]# tail -f /var/log/cron

In this article, we learned about the basic concepts, usage and common techniques of CronTab scheduled tasks in Linux. Through CronTab scheduled tasks, we can easily implement automated operation and maintenance tasks and improve work efficiency. I hope this article can help Linux operation and maintenance personnel and enthusiasts better manage servers and complete various tasks more efficiently.

The above is the detailed content of Linux CronTab scheduled tasks to easily realize automated operation and maintenance. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete