未找到 cron.service 文件

未找到 cron.service 文件

我的备份脚本在手动执行时工作正常;但 crontab 没有任何运行。我正在运行 Fedora 35 工作站。

crontab 编辑器的工作原理:

$ crontab -e

cron 守护程序未运行。这些命令中的每一个都没有输出:

$ pgrep cron
$ pgrep crond
$ pidof cron
$ pidof crond

我尝试启动 cron:

$ whereis cron
cron: /usr/share/man/man8/cron.8.gz
$ whereis crond
crond: /usr/sbin/crond /usr/share/man/man8/crond.8.gz
$ sudo service cron start
Redirecting to /bin/systemctl start cron.service
Failed to start cron.service: Unit cron.service not found.
$ sudo systemctl start cron
Failed to start cron.service: Unit cron.service not found.

我尝试使用 cronie 而不是 cron:

$ sudo dnf install cronie
Package cronie-1.5.7-3.fc35.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
$ sudo systemctl enable cronie.service
Failed to enable unit: Unit file cronie.service does not exist.
$ sudo systemctl start cronie.service
Failed to start cronie.service: Unit cronie.service not found.

另请注意,如果之前已安装,anacron(“cronie.service”)会在 Fedora 系统升级期间保留下来,但在进行全新安装时,至少不会安装在 Fedora Server 35 上,非常令人惊讶。

答案1

如果使用该命令:

# systemctl start crond.service

那么系统会为您保留它,即使重新启动也是如此。因此,您不必记住这样做!

此外,要获取状态,请使用:

# systemctl status crond.service

最后,# 当然意味着来自 root 帐户,因此请酌情使用(或不使用)sudo。

答案2

Cron 在使用 ajgringo619 发布的 crond 位置后执行了我的备份脚本:

$ ls -l /usr/sbin/crond 
-rwxr-xr-x. 1 root root 74448 Jul 21 15:05 /usr/sbin/crond
$ sudo /usr/sbin/crond start
$ pgrep cron
121692

答案3

您的脚本可能正在使用/调用具有相对路径和/或 shell 环境上下文的资源。

Cron 在没有 shell 和环境上下文的情况下运行其脚本。

尝试使用用户运行脚本nobody

  sudo -u nobody <your-scirpt-full-path>

一旦您的脚本与用户一起运行,nobody它可能会以cron.

bash将当前shell 环境上下文注入脚本的常见技巧:

  sed -i "2i source $HOME/.bash_script" <your-scirpt-full-path>

答案4

在 Ubuntu 上请使用以下命令

sudo service cron start

相关内容