可执行Python脚本无法从crontab运行

时间:2020-10-11 01:31:38

标签: python python-2.7 cron

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

脚本位于/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

现在 可以 工作了... 我看到了:

/p/home/osmc/python/pyscripter_file.txt中的

“测试脚本”

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

reading this article之后。.自从我使python脚本成为可执行文件并在脚本的顶部添加了“ shebang”行#!/ usr / bin / python之后,我就认为我不需要包含{在crontab中/usr/bin/python之前的{1}}?我的python脚本从另一台计算机上的crontab中运行。

更新

我尝试按照Gordon Davisson的建议获取python脚本的十六进制转储 enter image description here 由于某些原因,脚本第1行上的#!/ usr / bin / python之前会出现其他字符。该文件是在PyScripter IDE中创建的。

根据测试,如果我在Notepad ++中创建文件,则不会出现此问题。我在Notepad ++中创建了test3.py,hexdump给出了: enter image description here

test3.py也将通过crontab运行,如下所示: enter image description here

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

我使用以下文件格式创建了test.py:PyScripter中的UTF-8和UNIX。似乎PyScripter会插入错误的字符?它们在Python IDE中不可见,因此我无法删除它们。我该如何阻止呢?

Flex

3 个答案:

答案 0 :(得分:0)

改为在第一行下面使用(这是首选方法): #! / usr / bin / env python

请参阅:Python scripts in /usr/bin

此外,您可能需要将可执行python移至/ usr / local / bin /

答案 1 :(得分:0)

除了尝试使用env作为shebang行之外,您可能还需要在crontab中定义python路径,以支持库导入。 (我有/lib/python,但您放置的位置可能有所不同。)

PYTHONPATH=$PYTHONPATH:/lib/python

答案 2 :(得分:0)

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

PyScripter IDE包括Byte Order Mark(BOM)在当选择UTF-8编码的编码脚本的开始。脚本的十六进制转储中的字符串开头的十六进制字符 ef bb bf 证明了这一点,这是戈登·戴维森(Gordon Davisson)建议的一种调试措施。

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

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