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会自动加载新的计划任务。

相关推荐
2601_9620723310 分钟前
docker自建rustdesk-server远程桌面
运维·docker·容器
Blockchina32 分钟前
Codex 实战:从一句需求到可验收的 Linux 主机巡检脚本
运维·服务器·网络
2601_962218471 小时前
万象生鲜系统冷链物联网接入技术实现生鲜企业温控管理数字化
大数据·运维·微服务·云原生·架构
Best-Wishes1 小时前
BurpSuite Pro教育版在linux系统中配置
linux·运维·服务器·网络安全·burpsuite
志栋智能1 小时前
从关联到因果:智能超自动化如何实现真正的根因分析定界
运维·自动化
虎王物联1 小时前
RK3568嵌入式Linux跑Docker:内核配置排查到AIoT容器化的完整路径
linux·运维·docker·rk3568·aiot·嵌入式linux
从入门到退休3 小时前
企业远程控制选型:向日葵SDK vs RustDesk自建,谁是更务实的选择?
运维·服务器·网络·远程工作·远程控制
小王C语言5 小时前
Windows 无法使用 ssh 连接虚拟机(ubuntu),网络没问题的情况下
运维·ssh
java_logo5 小时前
Docker 部署 MinIO:轻松搭建 S3 兼容对象存储平台
运维·docker·容器·对象存储·minio·轩辕镜像·minio部署
祖力555 小时前
进程间通信(IPC机制):信号
linux·运维·服务器·进程·进程间通信·多任务·ipc