RDS
Posts: 776
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

No Program Output Window when running from Cron.

Sun May 14, 2017 2:51 pm

I have a Pi set up to take pictures at certain times of the day and save them to a folder on the Micro SD card, using Cron. This works very well and appears to be very reliable.
I notice though that if I run the same program manually, it produces an output window which contains the text that I have entered in to the program, namely 'Picture taken and sent to file'. However, when Cron runs the program, even though it correctly saves the picture, it does not produce the output window.

I would now like to use Cron on another one of my Pi's for another purpose but this time it is essential that the output window is visible.

Is there something extra that I need to enter into the program when running from Cron to ensure it produces the output window, that incidentally is headed, 'Python 3.4.2 shell'?

Martin Frezman
Posts: 1009
Joined: Mon Oct 31, 2016 10:05 am

Re: No Program Output Window when running from Cron.

Sun May 14, 2017 3:00 pm

Running things "in the background" and "out of sight" is kinda the main point of cron. It can be bent into doing what you want, but you might consider other approaches. Let me repeat that in case fast readers miss it on the first pass: It can be done, but it is not the usual approach, and if you really need this sort of display, you ought to consider running your program another way (say, via the startup files that people use to launch things "in the GUI"). The names and syntax of these files escapes me at the moment, but I am sure others will be along to fill in the details.

Usually, when a program is running "in the background" and "out of sight", output that would normally be displayed for a user to see is, instead, directed into log files. The maintainer is expected to read and analyze these log files to figure out if the program is working correctly.
If this post appears in the wrong forums category, my apologies.

pcmanbob
Posts: 9611
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: No Program Output Window when running from Cron.

Sun May 14, 2017 3:11 pm

Hi.
Gorden77 explains how to do it in the thread.
viewtopic.php?f=32&t=183624
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

RDS
Posts: 776
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: No Program Output Window when running from Cron.

Sun May 14, 2017 3:49 pm

@Martin Fezman
Thank you for the assurance it can be done.

@pcmanbob
Thank you for the link, I will give that a try later.
Just one other thing though: When using Cron, I know how to go back into Cron to edit the settings. When using this method what is the equivalent, or is it just a matter of deleting the file (/usr/bin/python /home/pi/filename.py) that has been created.

Martin Frezman
Posts: 1009
Joined: Mon Oct 31, 2016 10:05 am

Re: No Program Output Window when running from Cron.

Sun May 14, 2017 3:50 pm

pcmanbob wrote:Hi.
Gorden77 explains how to do it in the thread.
viewtopic.php?f=32&t=183624
Well, except for the silly UUOS (Useless Use of Sudo).
If this post appears in the wrong forums category, my apologies.

User avatar
scruss
Posts: 3256
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: No Program Output Window when running from Cron.

Mon May 15, 2017 12:20 am

Or another way to do it:
  1. in your cron job — redirect your program output to a file, like so: … crontab fields … python3 /home/pi/filename.py > /home/pi/output.txt
  2. In your ~/.config/lxsession/LXDE-pi/autostart file, add the line: @lxterminal -t 'Python 3.4.2 shell' -e 'tail -f /home/pi/output.txt'
Your program can quite happily restart and overwrite output.txt, but what it mustn't do is delete it. If that happens, the tail command will exit, leaving you with a useless window. Despite the window title, it's not really a shell, it's just watching the output file for changes.

There is no need to use sudo here at all.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

RDS
Posts: 776
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: No Program Output Window when running from Cron.

Sat May 27, 2017 9:27 pm

scruss wrote:Or another way to do it:
  1. in your cron job — redirect your program output to a file, like so: … crontab fields … python3 /home/pi/filename.py > /home/pi/output.txt
  2. In your ~/.config/lxsession/LXDE-pi/autostart file, add the line: @lxterminal -t 'Python 3.4.2 shell' -e 'tail -f /home/pi/output.txt'
Sorry for the delay in responding and I am OK with the first part but for item number 2 above, could you please explain where this would be added.

User avatar
scruss
Posts: 3256
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: No Program Output Window when running from Cron.

Sat May 27, 2017 9:48 pm

at the end of the file
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

RDS
Posts: 776
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: No Program Output Window when running from Cron.

Fri Jun 02, 2017 6:32 pm

@scruss
Thanks but sorry I meant where would that file be placed. I am not familiar with a :

~/.config/lxsession/LXDE-pi/autostart file

User avatar
Paeryn
Posts: 2986
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: No Program Output Window when running from Cron.

Fri Jun 02, 2017 7:17 pm

RDS wrote:@scruss
Thanks but sorry I meant where would that file be placed. I am not familiar with a :

~/.config/lxsession/LXDE-pi/autostart file
~ is your home directory, for the user pi it expands to /home/pi so that is equivalent to
/home/pi/.config/lxsession/LXDE-pi/autostart
Typically the shell will expand it for you so you can type it as scruss gave it, e.g.

Code: Select all

nano ~/.config/lxsession/LXDE-pi/autostart
She who travels light — forgot something.

RDS
Posts: 776
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: No Program Output Window when running from Cron.

Fri Jun 02, 2017 10:06 pm

@Paeryn
Thank you

Return to “Python”