RHEL7学习之crontab无法执行ntpdate

时间:2022-06-12 12:18:42

1,"/etc/crontab"文件

[root@localhost ~]# more /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root # For details see man crontabs # Example of job definition:
# .---------------- minute ( - )
# | .------------- hour ( - )
# | | .---------- day of month ( - )
# | | | .------- month ( - ) OR jan,feb,mar,apr ...
# | | | | .---- day of week ( - ) (Sunday= or ) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed

2,检查crond服务是否启动,crontab命令格式后,还是无法同步。查看crontab日志

From root@localhost.localdomain  Sat Feb  ::
Return-Path: <root@localhost.localdomain>
X-Original-To: root
Delivered-To: root@localhost.localdomain
Received: by localhost.localdomain (Postfix, from userid )
id B340060ADA00; Sat, Feb :: + (CST)
From: "(Cron Daemon)" <root@localhost.localdomain>
To: root@localhost.localdomain
Subject: Cron <root@localhost> root ntpdate -u 192.168.10.10
Content-Type: text/plain; charset=UTF-
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=>
X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/>
X-Cron-Env: <LANG=en_US.UTF->
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Message-Id: <.B340060ADA00@localhost.localdomain>
Date: Sat, Feb :: + (CST) /bin/sh: root: command not found

3,发现"/bin/sh: root: command not found",需要改成绝对路径

[root@localhost ~]# whereis crontab
crontab: /usr/bin/crontab /etc/crontab /usr/share/man/man1/crontab..gz /usr/share/man/man5/crontab..gz
[root@localhost ~]# whereis clock
clock: /usr/sbin/clock /usr/share/man/man8/clock..gz
[root@localhost ~]# crontab -e

  * * *  /usr/sbin/ntpdate -u  192.168.10.10; /usr/sbin/clock -w