首页 > 解决方案 > Mono 可执行文件不会从 Mac 上的 crontab 运行

问题描述

我试图让一个单声道可执行文件在每天的特定时间自动运行,但它不会从 crontab 运行。

使用 crontab -e 我设置了以下计划任务:

10 15 * * * (cd ~/Documents/automation && bash auto_run.sh)

在 auto_run.sh 文件中,我有以下内容:

#!/bin/bash
echo “`date “+%Y-%m-%d %H:%M:%S”` : auto run starting >> auto_run_logging.txt

mono /Users/admin/Projects/auto_task/auto_task/bin/Debug/auto_task.exe

echo “`date “+%Y-%m-%d %H:%M:%S”` : auto run ending >> auto_run_logging.txt

我已chmod +x在文件上使用以确保它们执行。

在预定时间,可以在文本文件中看到日期/时间消息,但单声道可执行文件不运行。直接从终端运行文件bash auto_run.sh效果很好。

任何帮助,将不胜感激。我正在使用 macOS Mojave

标签: bashmacoscronmono

解决方案


发生这种情况是因为mono无法找到它应该从 cron 内部运行的路径。

.bash_profile您可以在运行 cron 时将路径放入并获取它。或者运行which mono并提及脚本中运行mono命令的路径。

命令 :

which mono# 它会为你mono提供存在的路径。

脚本将是:

#!/bin/bash
echo “`date “+%Y-%m-%d %H:%M:%S”` : auto run starting >> auto_run_logging.txt

/path/to/mono/mono /Users/admin/Projects/auto_task/auto_task/bin/Debug/auto_task.exe

echo “`date “+%Y-%m-%d %H:%M:%S”` : auto run ending >> auto_run_logging.txt

另一种方法是,将mono路径放入.bash_profile并像这样运行 cron:

10 15 * * * . ~/.bash_profile; (cd ~/Documents/automation && bash auto_run.sh)

推荐阅读