Crontab项导致未知命令

2024-03-29 11:29:13 发布

您现在位置:Python中文网/ 问答频道 /正文

条目

0 * * * *  /home/ec2-user/python-scripts/master.py

导致以下错误:

sh: python3: command not found

我的剧本最上面有一句话:

#!/usr/local/bin/python3

我认为Python之路是正确的:

[ec2-user@ip-152-31-33-105 cron.hourly]$ /usr/local/bin/python3
Python 3.4.3 (default, Mar 17 2016, 20:37:33) 
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

根据建议,我寻找了CR,但没有发现:

0000000 23 21 2f 75 73 72 2f 6c 6f 63 61 6c 2f 62 69 6e
0000020 2f 70 79 74 68 6f 6e 33 0a 0a 69 6d 70 6f 72 74
0000040 20 6f 73 0a 0a 64 65 66 20 65 78 74 72 61 63 74
0000060 28 29 3a 0a 20 20 6f 73 2e 73 79 73 74 65 6d 28
0000100 22 70 79 74 68 6f 6e 33 20 63 6c 69 6e 74 6f 6e
0000120 5f 74 77 65 65 74 73 2e 70 79 22 29 20 20 0a 20
0000140 20 6f 73 2e 73 79 73 74 65 6d 28 22 70 79 74 68
0000160 6f 6e 33 20 63 72 75 7a 5f 74 77 65 65 74 73 2e
0000200 70 79 22 29 0a 20 20 6f 73 2e 73 79 73 74 65 6d
0000220 28 22 70 79 74 68 6f 6e 33 20 6b 61 73 69 63 68
0000240 5f 74 77 65 65 74 73 2e 70 79 22 29 0a 20 20 6f
0000260 73 2e 73 79 73 74 65 6d 28 22 70 79 74 68 6f 6e
0000300 33 20 73 61 6e 64 65 72 73 5f 74 77 65 65 74 73
0000320 2e 70 79 22 29 20 0a 20 20 6f 73 2e 73 79 73 74
0000340 65 6d 28 22 70 79 74 68 6f 6e 33 20 74 72 75 6d
0000360 70 5f 74 77 65 65 74 73 2e 70 79 22 29 0a 20 20
0000400 72 65 74 75 72 6e 20 31 0a 0a 65 78 74 72 61 63
0000420 74 28 29 0a 0a
0000425

我该怎么做?你知道吗

谢谢


Tags: pymasterhomebinusrlocalsh错误
1条回答
网友
1楼 · 发布于 2024-03-29 11:29:13

确保行尾不包含回车符,而只包含新行。你知道吗

od -t x1 /home/ec2-user/python-scripts/master.py
# If there's 0d in the output, it's a carriage return (\r, CR)

如果有回车符,字符也被认为是可执行路径的一部分。你知道吗


要删除CR,可以使用dos2unix之类的工具。你知道吗

如果不想使用dos2unix,可以使用python:

$ /usr/local/bin/python3 -c \
'f = open("/home/ec2-user/python-scripts/master.py", "r+b"); \
s = f.read().replace(b"\r", b""); f.seek(0); f.write(s); f.close()'

相关问题 更多 >