1.在系统中有service这个命令时:
这个命令在red hat当中常用,有的linux发行版本中没有这个命令.
service crond start //启动服务
service crond stop //关闭服务
service crond restart //重启服务
2.linux发行版本没有service这个命令时:
/etc/init.d/cron stop
/etc/init.d/cron start
3.执行出现 /bin/systemctl 。。。。说明是新版的可用以下命令操作
/bin/systemctl restart crond.service # 启动服务
/bin/systemctl reload crond.service # 重新载入配置
/bin/systemctl status crond.service # 查看crontab服务状态
4.编辑crontab
crontab -e
5.脚本无法执行问题
如果我们使用 crontab 来定时执行脚本,无法执行,但是如果直接通过命令(如:./test.sh)又可以正常执行,这主要是因为无法读取环境变量的原因。 解决方法:
-
1、所有命令需要写成绝对路径形式,如: /usr/local/bin/docker。
-
2、在 shell 脚本开头使用以下代码:
#!/bin/sh . /etc/profile . ~/.bash_profile3、在 /etc/crontab 中添加环境变量,在可执行命令之前添加命令 . /etc/profile;/bin/sh,使得环境变量生效,例如:
20 03 * * * . /etc/profile;/bin/sh /var/www/runoob/test.sh