Skip to content
Advertisement

Crontab Entry Causing Unknown Command

The entry

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

Is causing the following error:

sh: python3: command not found

My script has the shebang at the top:

#!/usr/local/bin/python3

I think the python3 path is correct:

[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.
>>> 

As suggested, I looked for a CR but none found:

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

What should I try?

Thanks

Advertisement

Answer

Make sure the end-of-line does not contains Carriage return, but only new line.

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

If there’s carriage return, the characters is also considered as a part of the executable path.


To remove CR, you can use tools like dos2unix.

If you don’t want to use dos2unix, you can use 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()'
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement