Le_Moi
Posts: 6
Joined: Wed Dec 14, 2016 11:53 am

Autostart python on reboot

Wed Dec 21, 2016 1:23 pm

Hi, I'm puzzling to launch a script at startup I've gone over the documentation on the raspbian homepage and they suggested to use the crontab -e option. I've also read in another forum that you could use a service to get a program to run at startup, this would also restart the program if it crashes. Which is something I do not wish to do.

this is what I did:
as the sudo-user I type in the terminal:

Code: Select all

crontab -e
I edit the file with the recommended nano editor and at the bottom I added:

Code: Select all

@reboot python /home/pi/Desktop/prog/prog.py
I can execute that script by simply double-clicking it, but it does not run at reboot.


I then googled around and found this thread: https://www.digitalocean.com/community/ ... what-gives
I modified my crontab command to:

Code: Select all

@reboot cd /home/pi/Desktop/prog && /usr/bin/python /home/pi/Desktop/prog/prog.py
However this had no effect on me... and I can't for the life of it understand why.

I also wish to run this script maximized... which lead me to wonder if it wouldn't be better to create a shellscript that launches prog.py and append the --maximize command in that shellscript? How do you automate your scripts with the crontab option on reboot?

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

Re: Autostart python on reboot

Thu Dec 22, 2016 11:39 am

Hi.

You have not told us what you expect the python program to do.
from the last lines of your post I am guessing you are expecting to see some output from your program on the screen, using the @reboot in cron will run your program in the back ground with no output to any screen.

If you give us more info on what you are expecting the program to do my be we can help.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

mattmiller
Posts: 2167
Joined: Thu Feb 05, 2015 11:25 pm

Re: Autostart python on reboot

Thu Dec 22, 2016 6:12 pm

If you want your python prog to be autostarted and the output of it displayed on your screen then try this out
viewtopic.php?f=66&t=162947&hilit=+autostart

Le_Moi
Posts: 6
Joined: Wed Dec 14, 2016 11:53 am

Re: Autostart python on reboot

Fri Dec 23, 2016 4:47 pm

Ooh wow, that explains everything... Yes I indeed want the output to be visible to the screen. It waits for a certain input and then goes to the corresponding function. I'll give the Desktop-entry a go. Thanks!

Le_Moi
Posts: 6
Joined: Wed Dec 14, 2016 11:53 am

Re: Autostart python on reboot

Sat Dec 24, 2016 2:34 pm

This is probably going to be the stupidest question you've read in a lifetime... but here we go:

When I go to /home/pi/.config/lxsession/LXDE-pi I can see four files;
- autokey.sh
- autostart (plain text file)
- desktop.conf
- sshpwd.sh

in autostart I have the following content:

Code: Select all

@lxpanel --profile LXDE-pi
@psmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@point-rpi
I can't just put the .desktop file in a plaintext file, right?
So I've tried to put the content of the desktop-file in it, but with no luck.

I've also, based on the final comment of the thread you pointed to created a folder 'autostart' in the folder '/home/pi/.config' and put the .desktop file in there, which also had no effect.


I then looked into the .bashrc option where I'd edit the file: '/home/pi/.bashrc'; i'd then paste at the bottom the Exec command of my .desktop shortcut to launch the python script.
This is very close to giving me what I want, it still doesn't launch the application on itself, but it does when I open the terminal.

The command I pasted in was:

Code: Select all

lxterminal -e python /home/pi/Desktop/prog/prog.py
Happy holidays.

mattmiller
Posts: 2167
Joined: Thu Feb 05, 2015 11:25 pm

Re: Autostart python on reboot

Sat Dec 24, 2016 5:23 pm

The way I do is to stick the desktop file in
/home/pi/.config/autostart

On a plain image, autostart doesn't exist do it needs creating with
mkdir /home/pi/.config/autostart

Le_Moi
Posts: 6
Joined: Wed Dec 14, 2016 11:53 am

Re: Autostart python on reboot

Mon Dec 26, 2016 2:31 pm

I figured it out; in another thread I found that you have to put the line that you use to start a program at boot before "@xscreensaver -no-splash". It now works brilliantly.
Thank you so much.

mattmiller
Posts: 2167
Joined: Thu Feb 05, 2015 11:25 pm

Re: Autostart python on reboot

Mon Dec 26, 2016 4:52 pm

Can you post the file just for completeness :)

User avatar
legendisback
Posts: 11
Joined: Thu Sep 01, 2016 12:58 pm

Re: Autostart python on reboot

Tue Dec 27, 2016 9:59 am

here is how I start my python scripts in startup
firs create a launcher.sh in the same folder as your program is:
sudo nano launcher.sh
then write the fallowing code into it:

cd /
cd home/pi/ # "this is the directory of your program"
sudo python program.py # "change the name program to your program"
cd /

you can add sleep in the beginning, if you need time to start the python script
then use:
sudo chmod 755 launcher.sh

us cd to go back to main directory
then create logs file:

sudo mkdir logs
you need this to check if there are any errors when your program runs at the startup

then use
sudo nano crontab -e
and add the fallowning code to the end
@reboot sh /home/pi/launcher.sh >/home/pi/logs/cronlog 2>&1 # dont forget to change the directory (/home/pi/ wherever the launcher.sh is bur do not change the logs directory)

and then sudo reboot
all should be fine now
if your program is not working then go to logs directory (cd logs)
use
cd logs
cat cronlog

this will show you the error (if it is a program error)

thats the way i do my startup scripts
good luck
I am the terror that flaps in the night...
I am the surprise you find in your cereal box!
I am DARKWING DUCK!!!

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

Re: Autostart python on reboot

Tue Dec 27, 2016 11:55 am

A few points with that (plus I've added some code tags to make it easier to read)
legendisback wrote:sudo nano launcher.sh
You don't need sudo to edit a file. Just nano launcher.sh will do.
legendisback wrote:

Code: Select all

cd /
cd home/pi/ # "this is the directory of your program"
sudo python program.py # "change the name program to your program"
cd /
You don't need the first cd if you put the full path in the second cd.
The final cd command isn't required at all.
Only use "sudo" if your program won't run without it. Most programs don't need it and using "sudo" for everything is a bad habit to get into.
It is a good practice to put the full path of python.

All you need is

Code: Select all

cd /home/pi/ # "this is the directory of your program"
/usr/bin/python program.py # "change the name program to your program"
legendisback wrote:sudo chmod 755 launcher.sh
Again, you don't need sudo. You created the file, you own it. You can do whatever you want without having to hit it with a big stick!
legendisback wrote:sudo mkdir logs
Seriously? No need to sudo! That's the programming equivalent of calling 911 because your tea has gone cold.

Just drop all the sudo commands apart from...
legendisback wrote:and then sudo reboot
...finally. This one does need sudo because it requires root/superuser privileges to run as it affects the whole system.

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

Re: Autostart python on reboot

Tue Dec 27, 2016 12:11 pm

There must be a lot of websites out there that recommend "sudo" as a synonym for "run".

But the funny thing is that once you do use "sudo" (say, to edit, i.e., create a file), then you do have to use sudo thereafter. I can already hear OP saying "But when I don't use sudo, it says permission denied".

Ditto for the other commands. For example, if you used "sudo" to make the "logs" directory, then the program will fail if it:
a) Isn't run with sudo
b) Attempts to write into (i.e.,create a file in) the "logs" directory.

Adding "sudo" will "fix" this "problem".
If this post appears in the wrong forums category, my apologies.

User avatar
Cancelor
Posts: 759
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: Autostart python on reboot

Tue Dec 27, 2016 3:27 pm

Martin Frezman wrote:.....

Adding "sudo" will "fix" this "problem".
Just to repeat what you are saying but in a different way .... sudo is what caused that problem in the first place.

To really repair the problem of creating a simple file with elevated powers you would need to use

Code: Select all

chown pi filename
to once and for all chANGE to ownERSIP of the file so that pi (or what ever your user name is) owns it.

p.s. yes you probably will need sudo in front of that !!!
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

Return to “Automation, sensing and robotics”