以下是我的 crontab 的内容:
toto@toto-pc:~$ 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
21 15 * * * DISPLAY=:0 /usr/bin/freefilesync /home/toto/Backup_launch/Backup_on_My_Passport.ffs_batch >/home/toto/crontab_log 2>&1
*/10 * * * * /home/toto/Backup_launch/Backup_on_OneDrive.sh
toto@toto-pc:~$
然后,我预计FreeFileSync 每天 15:21 执行Backup_on_My_Passport.ffs_batch批处理作业,并且脚本Backup_on_OneDrive.sh每 10 分钟执行一次。
Backup_on_OneDrive.sh每 10 分钟正确执行一次,但 crontab 永远不会启动Backup_on_My_Passport.ffs_batch。
以下是crontab_log文件的内容:
Authorization required, but no authorization protocol specified
15:21:01: Error: Unable to initialize GTK+, is DISPLAY set properly?
我认为 DISPLAY 的值是正确的:
toto@toto-pc:~$ echo $DISPLAY
:0
toto@toto-pc:~$
看来是韦兰的问题。然后,在 /etc/gdm3/custom.conf 中,我取消注释了
#WaylandEnable=false
强制登录屏幕使用 Xorg 的行。然后我不得不改变
DISPLAY=:0
,DISPLAY=:1
因为echo $DISPLAY
返回1
。现在它正在按预期工作。
由于
cron
将条目的命令行部分传递crontab
到/bin/sh
(可能是到 的链接/bin/dash
),这是一个更简单的 shell,因此应该1) 将命令包装在#!/bin/bash
脚本中;2) 从您的crontab
. 这将使调试、操作、日志记录等变得更加容易,并且您永远不必担心不支持的bash
功能。sh
您也可能会从我的回答中受益(https://unix.stackexchange.com/questions/673908/why-crontab-doesnt-execute-a-scheduled-bash-script/673918#673918)。