Run a python script on boot up in Debian

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
0miker0
 
Posts: 60
Joined: Mon Sep 12, 2011 8:13 am

Run a python script on boot up in Debian

Post by 0miker0 »

Following the Adafruit 128X64 Oled Display tutorial located here on the BeagleBone Black running Debian worked without issues :http://www.adafruit.com/blog/2014/02/05 ... -over-spi/
Next I'd like to run a python script on boot up but i'm not having any luck.

This is what i'm supposed to do:

Create a file in /etc/systemd/system/ named example.service (for example)
It should contain the following

[Unit]
Description=Example of a service

[Service]
Type=oneshot
ExecStart=/bin/bash -c 'cd /home/example ; python example.py 2> example.err > example.out'
TimeoutSec=0
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

then give the command systemctl enable example.service
When you reboot, your script will execute.

I modified a few lines but it's not working:

created a file in /etc/systemd/system/ named example.service

[Unit]
Description=Example of a service

[Service]
Type=oneshot
ExecStart=/bin/bash -c 'cd /home/root/py-gaugette/samples/ ; python ssd1306_test.py 2> example.err > example.out'
Nothing. So then I tried
ExecStart=/bin/bash -c 'cd /home/root/py-gaugette/samples/ ; /usr/bin/python new_test.py 2> example.err > example.out'
TimeoutSec=0
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Then gave the command
systemctl enable example.service

User avatar
tdicola
 
Posts: 1074
Joined: Thu Oct 17, 2013 9:11 pm

Re: Run a python script on boot up in Debian

Post by tdicola »

What if you try configuring the systemd service similar to how this DS1307 RTC service is setup in this guide: https://learn.adafruit.com/adding-a-rea ... t-rtc-time

In particular, put the code to run the python script into a shell script and then call that from the service. I'd also try switching to the simple service type too. I'm not super knowledgeable on systemd but have had luck getting services to work using the service template in the above guide.

User avatar
0miker0
 
Posts: 60
Joined: Mon Sep 12, 2011 8:13 am

Re: Run a python script on boot up in Debian

Post by 0miker0 »

No luck...

Tried:

nano /lib/systemd/system/oled3.service

********
[Unit]
Description=OLED Service

[Service]
Type=simple
WorkingDirectory=/home/root/pu-gaugette/samples/
ExecStart=/home/root/py-gaugette/samples/python ssd1306_test.py
SyslogIdentifier=oled3

[Install]
WantedBy=multi-user.target

*******
systemctl enable oled3.service
and
systemctl start oled3.service

User avatar
tdicola
 
Posts: 1074
Joined: Thu Oct 17, 2013 9:11 pm

Re: Run a python script on boot up in Debian

Post by tdicola »

Looking a little closer a few things to check:

- Your paths reference /home/root, but the root user's home directory is actually /root on BBB Debian. Check that the /home/root/ path really is pointing at the py-gaugette and other files and update to /root if it's incorrect.

- For the WorkingDirectory line it shows folder 'pu-gaugette', but did you really mean 'py-gaugette'?

- For the ExecStart line:

ExecStart=/home/root/py-gaugette/samples/python ssd1306_test.py

It looks like this is trying to run python located at /home/root/py-gaugette/samples/ which seems wrong (unless you have a shell script or symbolic link to python in that directory). Try changing ExecStart to look like:

ExecStart=/usr/bin/python ssd1306_test.py

This will run Python from its normal location in /usr/bin, and because the working directory is set to the py-gaugette samples folder it will pick up the ssd1306.py file there. Be careful again about /home/root vs /root paths, I think you really mean to have /root.

After attempting to start the service, also check the syslog for errors in the oled3 service by running:

cat /var/log/syslog | grep oled3

Copy the output of that if here if you still see errors as it might help to debug further.

User avatar
0miker0
 
Posts: 60
Joined: Mon Sep 12, 2011 8:13 am

Re: Run a python script on boot up in Debian

Post by 0miker0 »

After fixing the typo and removing /home so the line to ExecStart=/bin/bash -c 'cd /root/py-gaugette/samples
it now works!

Thanks for the help,
Mike

User avatar
tdicola
 
Posts: 1074
Joined: Thu Oct 17, 2013 9:11 pm

Re: Run a python script on boot up in Debian

Post by tdicola »

Awesome, good to confirm it works.

Locked
Please be positive and constructive with your questions and comments.

Return to “Beagle Bone & Adafruit Beagle Bone products”