linux运维定时任务crontab命令

linux crontab 安装

在Linux系统中,cron是一个常驻进程,用于在后台运行周期性任务。要安装cron,你通常会使用Linux发行版的包管理器。以下是在不同Linux发行版中安装cron的命令:

对于基于Debian的系统(如Ubuntu),使用以下命令:

`sudo apt-get update``sudo apt-get install cron`

对于基于Red Hat的系统(如CentOS),使用以下命令:

`sudo yum install cronie`

对于Fedora系统,使用以下命令:

`sudo dnf install cronie`

安装完成后,cron服务通常会自动启动。你可以使用以下命令检查cron服务的状态:

`# 对于使用Systemd的系统``sudo systemctl status cron``# 对于旧的init系统``service cron status`

要编辑cron任务,你可以使用crontab命令:

`crontab -e`

这将打开一个编辑器来编辑当前用户的cron作业。在这里,你可以添加或修改任务,格式如下:

bash 复制代码
# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
0 2 * * * /sbin/reboot
*/120 * * * * /usr/local/projects/release/shell/auto_clear_cache.sh
`* * * * * command_to_execute`

这里五个星号分别代表分钟、小时、日、月、星期,command_to_execute是你想要执行的命令。

linux crontab 每10分钟

要在Linux中使用crontab设置每10分钟执行一次任务,你可以编辑crontab文件并添加如下行:

`*/60 * * * * /usr/local/projects/release/shell/auto_clear_cache.sh`

这行的意思是,每小时的每60分钟都会执行指定的命令或脚本。

例如,如果你想每10分钟执行一次/usr/local/projects/release/shell/auto_clear_cache.sh

脚本,你可以这样设置:

`crontab -e`

然后在打开的编辑器中添加:

`*/10 * * * * /usr/bin/backup.sh`

保存并退出编辑器,crontab会自动加载新的计划任务。

相关推荐
客观花絮说2 分钟前
DSC+DW实时+异步搭建部署
运维
x66ccff21 分钟前
【linux】4张卡,坏了1张,怎么办?
linux·运维·服务器
我命由我123452 小时前
GPIO 理解(基本功能、模拟案例)
linux·运维·服务器·c语言·c++·嵌入式硬件·c#
kka杰2 小时前
Linux 进程3
linux·运维·服务器
华纳云IDC服务商2 小时前
网站服务器怎么计算同时在线人数?
运维·服务器
没有名字的小羊2 小时前
Linux基础命令——账户简单管理
linux·运维·服务器·网络·数据库
smile_life_2 小时前
服务器非法关闭后MySQL服务启动失败
运维·服务器·mysql
kka杰2 小时前
Linux 进程2
linux·运维·服务器
大白菜和MySQL2 小时前
tomcat服务搭建部署ujcms网站
java·linux·运维·服务器·tomcat
极客小张3 小时前
基于正点原子Linux开发板的智能监控与家电控制系统设计:深度解析Video4Linux和TCP/IP技术栈
linux·运维·c++·物联网·网络协议·tcp/ip·算法