crantab定时任务中添加shell,下方是解决办法。
若shell中包含sudo则提示:no tty present and no askpass program specified
解决办法1:进入/etc/sudoers.d 建立任意一个不是.和~结尾的文件(名称随意),里面输入:
sensetime ALL=(ALL) NOPASSWD: ALL # 若写ALL代表所有sudo命令都不需要输入密码
保存即可,不过crontab中部分命令依然出现只能超级用户执行。
解决办法2:在添加crontab时,直接输入 sudo crontab -u root -e 使用root启动命令,要是通过 whereis ntpdate 找到软件的原始位置写入下条命令:例如 ntpdate 写成 /usr/sbin/ntpdate,就可以正常执行命令
* * * * * /etc/init.d/ntp stop >/dev/null 2>&1 && /usr/sbin/ntpdate time1.aliyun.com >/dev/null 2>&1 && /etc/init.d/ntp start >/dev/null 2>&1 && /sbin/hwclock >/dev/null 2>&1
其他: crontab -e 所修改的文件在 /var/spool/cron/crontabs 中 对应的文件名为用户名,crontabs文件夹需要加 权限
select-editor 可以重置 crontab -e 的编辑方式,可选择 vim