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

Does Crontab always work in the Background?

Sun Sep 03, 2017 10:36 pm

I have a Pi that runs a program automatically on reboot using Crontab.

I am very pleased with this in that the Program sends me emails and sends output to a file as it is designed to do but when I run the program from the Desktop, it also opens a output window to display data that is produced by the Program.
However, using Crontab, the window does not open and someone once before on the Forum said that it is because Cron runs in the background.

Is it possible to add something to either Crontab or even my Program, to ensure that the output window is always opened.

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

Re: Does Crontab always work in the Background?

Sun Sep 03, 2017 11:19 pm

It always runs in the background with a limited environment. It also starts before the GUI starts.

Getting GUI things to start from cron isn't reliable. You're going to need to set a DISPLAY=:0 or DISPLAY=:1 environment variable at a bare minimum.
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.

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

Re: Does Crontab always work in the Background?

Mon Sep 04, 2017 6:29 pm

Thank you for your reply.
Maybe Cron is not the best solution then to run a Python 3 program automatically on reboot.

I shut down, then power down this Pi in the early hours every day, before powering it back up and running my program up reboot. This is because it seems to crash about every third day if I don't. It has never crashed whilst I have been operating it in this way, probably about 4 or 5 months now. I tried a different Pi and it did the same, so I am happy with my Reboot method, apart from the fact that it does not have a GUI with Cron.

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

Re: Does Crontab always work in the Background?

Mon Sep 04, 2017 6:38 pm

Correct.

If it's a background service write a systemd service file, they can even include a watchdog function to restart on failure.

If it's a GUI program start it from LXDE's autostart file.
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.

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

Re: Does Crontab always work in the Background?

Mon Sep 04, 2017 10:40 pm

Thank you, I will read up on that.

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

Re: Does Crontab always work in the Background?

Thu Sep 07, 2017 10:20 pm

I'm stuck again!

I have tried to follow the instructions for autostart in the following article:
http://www.wikihow.com/Execute-a-Script ... spberry-Pi

My autostart file now reads as follows:
@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
sudo /usr/bin/python /home/pi/Detect_Phototransistor.py
@xscreensaver -no-splash
@point-rpi

However, my 'Detect_Phototransistor.py' program does not run after the Pi reboots.
I tried changing the 3rd line to
sudo /usr/bin/python3 /home/pi/Detect_Phototransistor.py
but this made no difference.

Any further help would be appreciated.

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

Re: Does Crontab always work in the Background?

Thu Sep 07, 2017 10:32 pm

Put the @ sign on the front of that line.
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.

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

Re: Does Crontab always work in the Background?

Fri Sep 08, 2017 6:17 pm

I still cannot get it to work.
Maybe I am expecting too much.

My Python 3 program when run from IDLE, opens up a window labelled 3.4.2 Shell. All output is directed to this window. Basically, the program monitors and counts pulses from a Solar Panel Meter. (1 flash of an LED on the panel indicates 1 watt generated). I have mounted a Photo Transistor in front of the LED and the following is an example of out to the shell window:

18017 Fri 18:45:02 Rate: 20.77secs 0.173 kw
18018 Fri 18:45:25 Rate: 22.62secs 0.159 kw
18019 Fri 18:45:50 Rate: 25.28secs 0.142 kw
18020 Fri 18:46:20 Rate: 29.80secs 0.121 kw


I also send the latest output figure (the first column above) to a separate csv file. The program reads this value when it first runs, so that if it is interrupted for any reason during the day, the program can carry on where it left off, rather than starting at zero again. I also have a variable within the program that can be changed, that is currently set to 8000, so the program sends me an email every 8000 units.
This all runs perfectly when started manually.

When I have it starting from Crontab, it runs the program, updates the csv file and sends me the email. However, it does not create the shell window or display the window containing the python program code. If I want to see this information, I have to manually open the folder containing my program and double click on it to open the window and display my python code. I then click on the Terminal Window and type 'killall python3', press enter and the program that had been started by Crontab is halted. I then select Run from the drop down menu on the program window and the program runs and opens up the Shell window to enable the output shown above to be displayed.

If I could automate the actions I have described in the last paragraph, it would actually be ideal because I do like the ability during the day to be able to monitor the data in the Shell window.

Is it possible to automate these actions.
As usual, any help would be appreciated.

Return to “Python”