File not loading in Crontab

I have added two files to be loaded at startup in the crontab file using the following code.

@reboot python3 /home/pi/Power_Restart_Button/power_button.py &
@reboot python3 /home/pi/Tank/main.py

The first file just has a momentary button connected to start/stop/reboot the pi.
The second file is a gui that I made using tkinter.
The first file works perfectly but the second file wont start.

Can anyone help with this issue

Hey @Denver42165
Let’s try a few things

Try appending your last cron job with an ampersand so it looks like this:
@reboot python3 /home/pi/Tank/main.py &
and reboot.

Does main.py contain references to other directories in the filesystem, and if so, did you create these jobs with crontab -e or sudo crontab -e? If you used sudo then all the file paths will need to be absolute.

@Michael_R I used crontab -e for the startup of the programs.
The programs include references to the file system with full file path. eg. /home/pi/Water_Tank/etc
What do you recommend I change for this to work?

Did you try the modified cron job, with the & on the end?

Both of your steps are correct:
using sudo crontab -e would modify root’s crontab, meaning the script would be run as root, rather than Pi - this would affect how relative file paths work. Just calling crontab -e is the way you usually want to do something like this.
The file path in your script is ok, I just wanted to make sure it was an absolute path.

I haven’t tried running a TKinter script from cron before, but I can see how there might be problems with threading…
This is something I have on the to-do list for future tutorials.

For the time being, maybe you can help me out!
This forum post asks the same questions, where the solution was to edit the /etc/profile file. Perhaps a GUI application can’t be run from cron.

@Michael_R Thanks. The link worked and the GUI loaded but now my program is caught in a loop and i cant exit the GUI. Is there a way of bypassing the startup and going straight to the terminal on startup. maybe a shortcut key on the keyboard?

Ctrl+Alt+F1 should jump you to to tty1

Thanks all I have is a blank black screen. what else can i try?
I worked it out… Its bricked!. I just have to reload NOOBS.
I’m glad I did all my program development on GITHUB. :grin:

Interesting… are you sure there’s not a prompt showing in the top left corner?
FYI, you can jump out of the desktop GUI and into tty1 using Ctrl+Alt+F1, and if memory serve, you can jump back to the GUI using Ctrl+Alt+F2.
Think of it as peeling back the GUI - lifting the bonnet - of raspbian.

I can understand the hesitation to use github, but consider this:
On github you can open an issue on your own repository, with whatever your problem is. Documenting the problem well enough may feel tedious, but often we think most critically about our problems whenever we’re trying to tell others about them (see Rubber-duck debugging).
On github, you’re not working on a forum so there’s less scope (usually) for a back-and-forth conversation to clear things up. This makes you think doubly hard about your problem.

Then, once you’re finished opening the issue, you can copy and paste the problem into a forum post, and people can go and look at your repo and may even fix it for you with a pull-request.

Plus you get to learn how to use a sweet tool, good workflow for bigger projects, and its easy to share! :smiley:

@Michael_R I think you misunderstood me. I think GITHUB is awesome! It saved my programs. When I had to reinstall NOOBS, all I had to do was download the latest working file of my programs.

I found a solution to the original problem.
A python file that uses tkinter wont start until after the graphics is loaded. the solution is to add the python file in the LXDE-pi autostart.

The link below gives a great 10 step process.
https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=126264

With all the advise and help that Core has given me in the past few weeks, I hope my solution helps someone else out.

1 Like