niespielak
Posts: 11
Joined: Fri Aug 22, 2014 11:37 am

Cron and Python3 script

Sun Aug 24, 2014 10:58 am

Hello Guys,
a few days ago i wrote post in this link: Python3 script doesn't finish (http://www.raspberrypi.org/forums/viewt ... 91#p601891) - in few words if I use command in bash: "usr/bin/python3 /home/pi/programs/Termometr.py" - everything is ok, but when i Try use: "./Termometr.py" bash start script but nothing is happen (in script file there is shebang "#!/usr/bin/env python3"" and script is chmod to 777).

There is another problem - if i want use cron to schedule 1 minutes reading (*/1 * * * * /usr/bin/python3 /home/pi/programs/Termometr.py), nothing is happen again :(. I think problem is connected with default PATH for python3 interpreter. Maybe can give any solution?

Other thing - there is any debug/log for corn to readout where is the problem with cron? The same situation with Paths - where to read default paths for user/s?

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Cron and Python3 script

Sun Aug 24, 2014 11:57 am

niespielak wrote:There is another problem - if i want use cron to schedule 1 minutes reading (*/1 * * * * /usr/bin/python3 /home/pi/programs/Termometr.py), nothing is happen again :(. I think problem is connected with default PATH for python3 interpreter. Maybe can give any solution?

Other thing - there is any debug/log for corn to readout where is the problem with cron? The same situation with Paths - where to read default paths for user/s?
Firstly a trivial thing. You don't need to put */1 in crontab. That is what just * means.

The path "/usr/bin/python3" is correct.

Output from Cron is normally sent to the user by local mail, but that is disabled on the Pi default installation. You can send the output to files by changing the entry like this:

Code: Select all

* * * * * /usr/bin/python3 /home/pi/programs/Termometr.py >>/tmp/Termometr.log 2>>/tmp/Termometr.err
Any normal output from print function will be logged in /tmp/Termometr.log an any error messages logged in /tmp/Termometr.err

niespielak
Posts: 11
Joined: Fri Aug 22, 2014 11:37 am

Re: Cron and Python3 script

Sun Aug 24, 2014 3:30 pm

Rpdom thx for reply.

I put Your code to cron, but as I wrote last post - cron is not starting the script. There is nothing to log in ">>/tmp/Termometr.log 2>>/tmp/Termometr.err".

I repeat the question - there is any debug/log for corn - example script paths are wrong, user dont have rights etc. to run the script? The same situation with Paths - where to read default paths for user/s?

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Cron and Python3 script

Sun Aug 24, 2014 3:38 pm

Yes. /var/log/auth.log will have some entries in for each time cron is scheduled to run a job.

DirkS
Posts: 10363
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Cron and Python3 script

Sun Aug 24, 2014 3:48 pm

niespielak wrote: I put Your code to cron, but as I wrote last post - cron is not starting the script. There is nothing to log in ">>/tmp/Termometr.log 2>>/tmp/Termometr.err".
What if you put a print statement at the top of your script (before the imports). Does that show up in the log?

niespielak
Posts: 11
Joined: Fri Aug 22, 2014 11:37 am

Re: Cron and Python3 script

Sun Aug 24, 2014 4:35 pm

1. In auth.log i get info that cron sometimes (not reguraly minute by minute):
"Aug 24 10:17:01 raspberrypi CRON[5563]: pam_unix(cron:session): session opened for user root by (uid=0)
Aug 24 10:17:01 raspberrypi CRON[5563]: pam_unix(cron:session): session closed for user root"
2. I add print statement before import statement and now:
a. If i use in bash: "python3 Termometr.py" or "/usr/bin/python3 /home/pi/programs/Termometr.py" every thong is ok - i get print and after that srcript is done
b. If I use in bash: "./Termometr.py" bash is stuck to moment I use "ctrl + c" - I dont get print etc.
c. There are not files: ">>/tmp/Termometr.log 2>>/tmp/Termometr.err", and cron isnt't working

DirkS
Posts: 10363
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Cron and Python3 script

Sun Aug 24, 2014 5:00 pm

Just checked /var/log/syslog and there I can see the command that cron calls. For example:
Aug 24 02:05:01 raspiserver /USR/SBIN/CRON[8615]: (pi) CMD (get_iplayer --pvr)
Looking at the man pages ('man cron') it is possible to get a bit more info about the return code, but you'll have to start the cron daemon with the correct option. Don't know how to do that (yet).

Gr.
Dirk.

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Cron and Python3 script

Sun Aug 24, 2014 5:17 pm

This thread bears out well my constant assertion that cron is complex and weird and not the right tool for most things on the Pi. The fact that "mail" doesn't work (even locally) is a joke and renders most cron debugging jobs into the "what should be a 5 minute debugging job becomes a 6 month one" category. (Credit marty@winbatch.com for this last nice turn of phrase…)

It is especially not the right tool for running something "every minute", for reasons I've detailed at length in other posts - there are clearly better ways to "run something every minute".
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

niespielak
Posts: 11
Joined: Fri Aug 22, 2014 11:37 am

Re: Cron and Python3 script

Sun Aug 24, 2014 5:48 pm

Other thing I find out that:
1. When I try to modificate shebang in my python script Termometr.py to this one: "#!/usr/bin/python3" and after that start script "./Termometr.py" i get info that import is impossible becouse bad interpreter.
2. I check out with "which python3" and there is simlink "/usr/bin/python3" (/usr/bin/python3 -> python3.2)
3. I removed python2 and made simlink: "ln -s /usr/bin/python3.2 /usr/bin/python"
4. But when I try to check out with "which python" there is still "/usr/bin/python"
5. Still - "./Termometr.py" i get info that import is impossible becouse bad interpreter

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Cron and Python3 script

Sun Aug 24, 2014 8:43 pm

niespielak wrote:Other thing I find out that:
1. When I try to modificate shebang in my python script Termometr.py to this one: "#!/usr/bin/python3" and after that start script "./Termometr.py" i get info that import is impossible becouse bad interpreter.
Did you copy and paste your code from somewhere else, or write it using an editor on a Windows box?

I ask because "bad interpreter" usually means the #! line has an error in it and sometimes you can get strange characters on the end of lines that don't show up but cause problems.

To test this, try this command and see if you get the same result as me

Code: Select all

rpdom@raspi: ~ $ head -1 Termometr.py | hexdump -C
00000000  23 21 2f 75 73 72 2f 62  69 6e 2f 70 79 74 68 6f  |#!/usr/bin/pytho|
00000010  6e 33 0a                                          |n3.|

niespielak
Posts: 11
Joined: Fri Aug 22, 2014 11:37 am

Re: Cron and Python3 script

Mon Aug 25, 2014 7:38 am

Rpdom i get this readout:
head -1 Termometr.py | hexdump -C
00000000 23 21 2f 75 73 72 2f 62 69 6e 2f 70 79 74 68 6f |#!/usr/bin/pytho|
00000010 6e 33 0d 70 72 69 6e 74 28 22 54 65 73 74 22 29 |n3.print("Test")|
00000020 0d 69 6d 70 6f 72 74 20 6f 73 0d 69 6d 70 6f 72 |.import os.impor|

I have problem before when i copy code from windows editor to nano through ssh - I remove bad characters at end of the line (It's blocks even python scripts).

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Cron and Python3 script

Mon Aug 25, 2014 7:49 am

sudo apt-get install dos2unix
dos2unix Termometr.py
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
jojopi
Posts: 3271
Joined: Tue Oct 11, 2011 8:38 pm

Re: Cron and Python3 script

Mon Aug 25, 2014 7:57 am

niespielak wrote:00000000 23 21 2f 75 73 72 2f 62 69 6e 2f 70 79 74 68 6f |#!/usr/bin/pytho|
00000010 6e 33 0d 70 72 69 6e 74 28 22 54 65 73 74 22 29 |n3.print("Test")|
00000020 0d 69 6d 70 6f 72 74 20 6f 73 0d 69 6d 70 6f 72 |.import os.impor|
That file has "0d" line endings, which is very unusual. Linux requires "0a" line endings, as in rpdom's example. Windows uses "0d 0a". As far as I was aware, the last system to use bare "0d" was Mac OS 9, which is obsolete. How did you get the file onto the Pi, and/or edit it?

One possible fix is to "nano Termometr.py", then Ctrl+O to save, and Alt+M to turn off "[Mac Format]" before pressing Enter.
DougieLawson wrote:dos2unix Termometr.py
This does not work for crazy old Mac line endings.
edit: "mac2unix" from the same package does work.
Last edited by jojopi on Mon Aug 25, 2014 8:22 am, edited 1 time in total.

niespielak
Posts: 11
Joined: Fri Aug 22, 2014 11:37 am

Re: Cron and Python3 script

Mon Aug 25, 2014 8:04 am

I used nano as editor and format file to mac standard.

User avatar
joan
Posts: 14936
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Cron and Python3 script

Mon Aug 25, 2014 8:11 am

jojopi wrote: ...
As far as I was aware, the last system to use bare "0d" was Mac OS 9, which is obsolete.
...
I wonder if there was a link between Mac OS 9 and Microware OS-9 which uses 0x0D line endings.

niespielak
Posts: 11
Joined: Fri Aug 22, 2014 11:37 am

Re: Cron and Python3 script

Mon Aug 25, 2014 8:17 am

I used a "dos2unix Termometr.py" on the file which was formated by nano dos format. Everything is ok now - script working after usin g "./Termometr.py".

Now I will check Cron.

niespielak
Posts: 11
Joined: Fri Aug 22, 2014 11:37 am

Re: Cron and Python3 script

Wed Aug 27, 2014 2:34 pm

The same situation was with crone - i used to modify "crontab -e" by nano with mac os formating. Cron don't even start with bad conf file. Now is everything ok - thx all You Guys for help!

Return to “Raspberry Pi OS”