crontab里面不能执行 但是在crontab -e 里面可以 是为什么呢(操作系统-linux)

在脚本里面:


#!/bin/bash
source /etc/profile
echo "hello" > /home/test.log



crontab -e

          • sh /home/test.sh

/etc/crontab

          • root sh /home/test.sh

上面的可以成功
下面的不会有任何反应

代码没什么问题啊?
为什么会这样呢

在编写命令后面,建议都需要写成绝对路径的形式,命令也是,比如 sh ,你应该写成/usr/bin/sh 这样绝对命令的形式。
我这边测试,使用绝对命令的形式,没有问题。


[root@servera ~]# cat /etc/crontab 
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# 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
* * * * * root /bin/echo "xxx" >> /root/crontab.log
[root@servera ~]# 
[root@servera ~]# date
Thu Dec 16 14:24:50 CST 2021
[root@servera ~]# date
Thu Dec 16 14:24:53 CST 2021
[root@servera ~]# cat crontab.log 
xxx
[root@servera ~]# date
Thu Dec 16 14:25:03 CST 2021
[root@servera ~]# 

而且,执行的计划任务,crontab都会在日志中有记录,你可查看 tail -f /var/log/cron 日志进行观察。