在 FreeBSD 中将 Python 脚本作为 cron 作业运行 [英] Running a python script as a cron job in FreeBSD

查看:24
本文介绍了在 FreeBSD 中将 Python 脚本作为 cron 作业运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 freeBSD 的新手.我刚刚设置了一个服务器并安装了python 3.6.现在我想每天 15 点运行一个 python 脚本,所以我尝试设置一个 cron 任务.但在某种程度上,cron 任务永远不会运行或给我错误.由于 cron 使用邮件报告错误并且邮件似乎没有安装在我的服务器上,我不知道该脚本是实际运行还是根本没有运行.在/etc/crontab 中添加的行如下:

I'm new to freeBSD. I just set up a server and installed python 3.6. Now i want to have a python script run every day at 15h00, so i tried to set up a cron task. But in some way, the cron task never runs or is giving me errors. Since cron uses mail to report errors and mail doesn't seem to be installed on my server, I have no clue whether the script actually runs or is not running at all. The line added in /etc/crontab is the following:

0 15 * * * root /usr/local/bin/python3.6 /root/myscript.py

其中/usr/local/bin 是安装 python 的目录.在普通命令行中运行此命令时,它可以完美运行,但是使用 cron 时,它一直无法运行.欢迎任何帮助

Where /usr/local/bin is the directory where python is installed. When running this command in the normal command line, it works perfectly, but with cron, it keeps not working. Any help is welcome

提前致谢

推荐答案

要调试您的环境,请将其添加到 /etc/crontab

To debug your environment add this to /etc/crontab

* * * * * root env > ~/cronenv

等待文件 ~/cronenv 被创建(一分钟后)并使用 do 环境启动一个新的 shell:

Wait for file ~/cronenv to be created (after a minute) and start a new shell using does environments:

env - `cat ~/cronenv` /bin/sh

然后调用你的脚本 /usr/local/bin/python3.6/root/myscript.py

这将有助于在 cron 使用的相同环境中测试/调试您的代码.

This will help to test/debug your code within the same environment cron is using.

这篇关于在 FreeBSD 中将 Python 脚本作为 cron 作业运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆