Ubuntu 18.04上cron作业的身份验证问题

Ubuntu 18.04上cron作业的身份验证问题,ubuntu,cron,Ubuntu,Cron,我有一个干净的Ubuntu 18.04安装,我很难得到一个cron作业来执行脚本 Crontab-l包含以下内容: # Edit this file to introduce tasks to be run by cron. # # Each task to run has to be defined through a single line # indicating with different fields when the task will be run # and what comm

我有一个干净的Ubuntu 18.04安装,我很难得到一个cron作业来执行脚本

Crontab-l
包含以下内容:

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command

SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/user/local/sbin:/usr/sbin:/home/rob/scripts

*/1 * * * * /bin/bash /home/rob/scripts/scan2.sh >> /home/rob/scripts/scan.log 2>&1
0 1 * * * /bin/bash /home/rob/scripts/trimdb.sh
0 1 * * * /bin/bash /home/rob/scripts/sortf.sh
我可以看到cron作业在
/var/log/syslog
中的正确时间执行,如下所示,没有任何错误:

May 28 21:38:01 net CRON[1899]: (rob) CMD (/bin/bash /home/rob/scripts/scan2.sh >> /home/rob/scripts/scan.log 2>&1)
May 28 21:39:01 net CRON[1915]: (rob) CMD (/bin/bash /home/rob/scripts/scan2.sh >> /home/rob/scripts/scan.log 2>&1)
May 28 21:40:01 net CRON[1931]: (rob) CMD (/bin/bash /home/rob/scripts/scan2.sh >> /home/rob/scripts/scan.log 2>&1)
May 28 21:41:01 net CRON[1947]: (rob) CMD (/bin/bash /home/rob/scripts/scan2.sh >> /home/rob/scripts/scan.log 2>&1)
但是,当我检查cron服务时,我可以看到有一个auth错误:

May 28 21:46:01 net sudo[2146]: pam_unix(sudo:auth): conversation failed
May 28 21:46:01 net sudo[2146]: pam_unix(sudo:auth): auth could not identify password for [rob]
May 28 21:46:01 net CRON[2134]: pam_unix(cron:session): session closed for user rob
May 28 21:47:01 net CRON[2152]: pam_unix(cron:session): session opened for user rob by (uid=0)
May 28 21:47:01 net CRON[2153]: (rob) CMD (/bin/bash /home/rob/scripts/scan2.sh >> /home/rob/scripts/scan.log 2>&1)
May 28 21:47:01 net sudo[2164]: pam_unix(sudo:auth): conversation failed
May 28 21:47:01 net sudo[2164]: pam_unix(sudo:auth): auth could not identify password for [rob]
May 28 21:47:01 net CRON[2152]: pam_unix(cron:session): session closed for user rob
手动运行脚本时,该脚本运行良好

谢谢你的帮助

scan.log的内容:

sudo: no tty present and no askpass program specified
/home/rob/scripts/scan2.sh: line 43: grep: command not found
/home/rob/scripts/scan2.sh: line 1: date: command not found
/home/rob/scripts/scan2.sh: line 2: date: command not found
/home/rob/scripts/scan2.sh: line 3: date: command not found
/home/rob/scripts/scan2.sh: line 5: date: command not found
/home/rob/scripts/scan2.sh: line 6: date: command not found
/home/rob/scripts/scan2.sh: line 7: date: command not found
/home/rob/scripts/scan2.sh: line 41: grep: command not found

看起来
scan2.sh
中有东西试图运行
sudo
,而
sudo
需要您的密码进行身份验证。但是,
sudo
无法获取您的密码,因为cron作业未与终端关联,因此
pam_auth
(这是
sudo
用于提示输入密码的库)报告失败


要解决这个问题,您可以使用
sudo-A
,将
$sudo\u ASKPASS
环境变量设置为提供密码的程序名(它可以只是一个shell脚本)。如果您这样做,请确保只有您的UID能够读取、写入和运行您的密码提供程序

这是“rob”或“root”crontab?你在
扫描.log中看到有用的东西了吗?这是rob crontab在问题中添加了scan.log的内容听起来像是在要求我们调试
/home/rob/scripts/scan2.sh
而没有在
/home/rob/scripts/scan2.sh
中显示代码。