目标:
通过crontab命令在Linux系统设置定时任务
crontab命令
-u <user> define user
-e edit user's crontab
-l list user's crontab
-r delete user's crontab
-i prompt before deleting
-n <host> set host in cluster to run users' crontabs
-c get host in cluster to run users' crontabs
-s selinux context
-V print version and exit
-x <mask> enable debugging
准备定时任务的文件 /home/demo/cron-job
这个文件一定要是Unix(LF)格式,不能是Windows(CR LF)格式会导致定时任务执行失败
15 3 * * * /bin/sh /home/demo/restart.sh
这种方式可能会手动执行restart.sh没问题,但是定时任务不生效,缺少环境变量导致。
在shell中执行时是能够找到环境变量的,但是在crontab中,它是不会自己导入环境变量的,需要我们指定 . /etc/profile 就是导入环境变量。并将所有路径都写成绝对路径。
15 3 * * * ./etc/profile;