-bash: crontab: command not found

本文介绍两种在Linux系统中配置定时任务的方法:一是通过crontab命令为特定用户配置;二是编辑系统级的/etc/crontab文件。前者适用于用户级别的定时任务,配置文件保存在/var/spool/cron目录下;后者则适用于系统级的定时任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[root@foss01 ~]# crontab -e

-bash: crontab: command not found

[root@foss01 ~]# yum install -y vixie-cron


[root@foss01 ~]# crontab -e

no crontab for root - using an empty one

crontab: installing new crontab

[root@foss01 ~]# chkconfig --list crond   -查看是否是开机启动

crond           0:off 1:off 2:on 3:on 4:on 5:on 6:off

[root@foss01 ~]# 



Linux配置定时任务,大家都知道使用crontab这个系统功能,但有时候我们需要区分用户执行,下面就直接说一下2种方法的区别:

方法1:

使用命令 crontab -e 然后直接编辑定时脚本。

这样执行以后,属于用户自定义的,会被写到 /var/spool/cron 目录下,生成一个和用户名一致的文件,文件内容就是我们编辑的定时脚本。

如:

[root@foss01 ~]# cd /var/spool/cron/

[root@foss01 cron]# ll

total 4

-rw------- 1 root root 70 Mar 31 10:36 root

[root@foss01 cron]# cat root

* */2 * * * /usr/sbin/ntpdate 192.168.20.37 >> /root/ntpdate.log 2>&1

方法2:

使用命令 vi /etc/crontab 编辑定时脚本。

系统级的)做系统级配置我们会直接配置 /etc/crontab


[root@nhrnormal ~]# cat /etc/crontab 

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

HOME=/


# For details see man 4 crontabs


# Example of job definition:

# .---------------- minute (0 - 59)

# |  .------------- hour (0 - 23)

# |  |  .---------- day of month (1 - 31)

# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...

# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat

# |  |  |  |  |

# *  *  *  *  * user-name command to be executed

59 23 * * *   root /root/qingli.sh

(用户级的)一般还是建议大家使用 crontab -e ,这样系统也会帮着检查我们配置的脚本语法。


本文转自aaa超超aaa 51CTO博客,原文链接:http://blog.51cto.com/10983441/1758738


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值