首页 > 解决方案 > 可执行的 Python 脚本不会从 crontab 运行

问题描述

我正在尝试让 Python 脚本从 crontab 运行,但它不会。

脚本在/home/osmc/python/test.py

#!/usr/bin/python

# importing modules
import os
import time
import datetime
from datetime import datetime

def f_log(strtext):
# If this method cannot write to the log file, send an email but continue script execution anyway
    logFilePath = '/home/osmc/python/pyscripter_file.txt'
    ts_local = datetime.now()
    ts = ts_local.strftime("%d/%b/%Y %H:%M:%S")

    try:
        # Check if the logfile exists
        if(os.path.isfile(logFilePath)):
            # Append the error message to the log file. 'a+' creates the file if it does not exist.
            with open(logFilePath, 'a+') as f:
                f.write(ts + '\t' + strtext + '\n')
        else:
            print("Log File does not exist - Creating File now")
            with open(logFilePath, 'a+') as f:
                f.write(ts + '\t' + 'Log File does not exist - Creating File now' + '\n')
                f.write(ts + '\t' + strtext + '\n')
    except IOError as e:
        # Handle the exception
        print("you made a booboo")

f_log("Test Script")
print("Hello World")

我使脚本可执行...chmod 744 test.py

ls -l给出了这个:

-rwxr--r-- 1 root root   937 Oct 11 01:45 test.py

# which python/usr/bin/python

这是我在根目录下的 crontab:

PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/bin/python
01 02 * * * /home/osmc/python/test.py >> out.txt 2>&1

crontab 运行,但我在out.txt文件中得到以下输出:

/home/osmc/python/test.py: 1: /home/osmc/python/test.py: #!/usr/bin/python: not found
/home/osmc/python/test.py: 4: /home/osmc/python/test.py: import: not found
/home/osmc/python/test.py: 5: /home/osmc/python/test.py: import: not found
/home/osmc/python/test.py: 6: /home/osmc/python/test.py: import: not found
/home/osmc/python/test.py: 7: /home/osmc/python/test.py: from: not found
/home/osmc/python/test.py: 9: /home/osmc/python/test.py: Syntax error: "(" unexpected

这以前对我有用,我不明白为什么它不再有用了??

如果我将 crontab 更改为:

PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/bin/python
01 02 * * * /usr/bin/python /home/osmc/python/test.py >> out.txt 2>&1

现在它确实有效......我明白了:

/home/osmc/python/pyscripter_file.txt 中的“测试脚本”

我看到: /root/out.txt 中的“Hello World”

读完这篇文章/usr/bin/python后.. 我想既然我使 python 脚本可执行并且在脚本顶部有“shebang”行#!/usr/bin/python,我之前不需要/home/osmc/python/test.py在 crontab 中包含?我有一个 python 脚本从另一台机器上的 crontab 中运行。

更新

我尝试按照 Gordon Davisson 的建议获取 python 脚本的 hexdump 在此处输入图像描述 出于某种原因,脚本第 1 行的 #!/usr/bin/python 之前存在其他字符。该文件是在 PyScripter IDE 中创建的。

通过测试,如果我在 Notepad++ 中创建文件,我没有这个问题。我在 Notepad++ 中创建了 test3.py,hexdump 给出了: 在此处输入图像描述

test3.py 也将从这样设置的 crontab 运行: 在此处输入图像描述

我不需要在脚本的完整路径之前提供 /usr/bin/python ,因为我使它可执行并且在第一行有 shebang。但是 test.py 也是可执行的,并且在第一行有 shebang(它与 test3.py 相同)但是它不会从 crontab 运行。

我使用文件格式创建了 test.py:UTF-8 和 PyScripter 中的 UNIX。似乎 PyScripter 正在插入错误的字符?它们在 Python IDE 中不可见,因此我无法删除它们。我怎样才能阻止这个?

柔性

标签: pythonpython-2.7cron

解决方案


当我在 PyScripter IDE 中创建 python 脚本并选择 UTF-8 文件格式(编码)时,问题就发生了。

PyScripter IDE includes the Byte Order Mark (BOM) at the start of the encoded script when UTF-8 encoding is selected. 这可以通过脚本的 hexdump 中字符串开头的十六进制字符来证明: ef bb bf ,这是 Gordon Davisson 建议的调试措施。

这会阻止 python 脚本在 crontab 中运行,除非在脚本路径之前添加 /usr/bin/python。我不知道为什么会这样。

这种情况下的解决方案是在 PyScripter 中选择 UTF-8(无 BOM)作为文件格式,然后脚本将从crontab 运行,而无需在脚本路径之前指定 /usr/bin/python。


推荐阅读