How to make a python program start on boot? May need to use sudo.

random infos

http://stackoverflow.com/questions/637005/x-server-running

On Sat, 16 Apr 2016 16:53:16 -0700 (PDT), John Baker
<bakerengineeringco@gmail.com> declaimed the
following:

Hi Dieter,
Still not working. I have a hunch that the problem is with Tkinter but
can't tell. I have to run my GUI program SimB.py with the terminal program
on the BBB, typing sudo python SimB.py, then it runs happily.

  The key phrase is "GUI program".

*Here's my crontab in /etc:*

@reboot root /usr/bin/python /home/debian/Desktop/SimB.py
* * * * * root /usr/bin/python /home/debian/Desktop/SimB.py

  "cron" jobs do not have a console, much less a graphical session
manager.

Apr 16 23:37:14 beaglebone /USR/SBIN/CRON[2292]: (CRON) info (No MTA
installed, discarding output)

  You do not have local mail transfer agent (SMTPd or equivalent) so
whatever error trace cron was going to send is being dropped on the floor.

I can successfully run my program with a keyboard attached to my BBB using
sudo python SimB.py, have to use the sudo, otherwise gets a Tkinter error.
I just now double-checked and my GUI program SimB.py runs very happily.
Cannot run SimB.py thru putty as it gives the Tkinter error.

  putty implies a text console connection -- unless you have an X-server
running on the computer AND have configured the login via putty to use the
computer X-server (environment variable for DISPLAY pointing to the
computer) there is no graphical environment in which to open the GUI
program.

  When using a direct keyboard/mouse and HDMI connection, the BBB is
running a local X-server and the (automatic) login is running a
desktop/window manager that connects to that X-server. Not sure why you'd
need the sudo -- Tkinter should be able to open a window from the account
that is running the session manager.

I just now discovered the problem requiring sudo to start my python program. It’s related to PyBBIO library code to write to the PWM interface on the BBB. I am trying to find out if there is a way around this requirement (to run the code as root) but I may be stuck with it. :frowning:

I get a permission denied error related to the capemgr.9 if I don’t run my code as root.

John

OK, so you need to learn about unix groups. In essence, you need to create a group, say “cape” and then make your userid a member of this group. After that, do the following:

sudo chown root:cape /sys/devices/bone_capemgr.9/slots

Now, as a member of the group “cape”, you are permitted to write to /sys/devices/bone_capemgr.9/slots.

Regards,
John

On Sat, 16 Apr 2016 20:35:44 -0700, John Baker
<johnbaker@ieee.org> declaimed the following:

Really good idea evilwulfie. That, xwindows, sounds like the problem.

Now I'm thinking that I should put a startup icon on the Debian desktop
to start my program, since I know that I can start it with the terminal
program with sudo python myProgram.py. That has always worked, whereas I
cannot start it via putty which apparently requires xwindow. It doesn't
seem like this should be so hard, but probably just hard as I am not
knowledgeable with Linux.

  Putty does not require an X-window environment -- it's your program
using Tkinter that needs to have an X-window environment. That is, it has
to have someplace to send the drawing commands for the GUI you want to use.
Putty is basically a text console, it does not know X-window. (In fact,
putty is likely running on a M$ Windows box and uses the Win32 runtime --
only text transfers between it and the BBB login)

http://support.objectplanet.com/esupport/index.php?_m=knowledgebase&_a=viewarticle&kbarticleid=17

  When you connect via keyboard and HDMI, you are running the X-server on
localhost (the BBB). (It actually gets set up for that session on boot)

  From putty -- you'd have to have an X-server running on your M$ Windows
box, and then issue command through putty to set the display to the Windows
box, before starting the Tkinter program.

My Python program SimB.py runs fine if I start it with sudo python SimB.py using a keyboard attached to my BBB.
So I gave crontab a try again, putting it in /etc. Here’s my crontab and crontab -l which seems to say my python program is running but I don’t see that it has done anything, don’t see anything running on my BBB 4DCAPE screen. I saw something in the CronHowTo () page about GUI’s but haven’t tried that yet, figuring that I would just try to start SimB.py and see what happened. So what to do next?

Thanks in advance for help,
John

On Tue, 19 Apr 2016 21:52:27 -0700 (PDT), John Baker
<bakerengineeringco@gmail.com> declaimed the
following:

My Python program SimB.py runs fine if I start it with sudo python SimB.py
using a keyboard attached to my BBB.

  Which, by default, is connected to the X-Window server running on
"localhost" (and which is also connected to the HDMI/LCD output). IOWs, you
have a full graphical environment preset for you, and Tkinter can connect
to the display server.

So I gave crontab a try again, putting it in /etc. Here's my crontab and
crontab -l which seems to say my python program is running but I don't see

  A timed background job not connected to any form of console/display.

that it has done anything, don't see anything running on my BBB 4DCAPE
screen. I saw something in the CronHowTo () page about GUI's but haven't
tried that yet, figuring that I would just try to start SimB.py and see
what happened. So what to do next?

  Uhm... Read that how-to page? Or maybe this:

Thanks for your help and your patience with me.
Unfortunately, still no joy and no error message but then I’m illiterate with Linux.

I forgot to mention that I am using PyBBIO to drive PWM output and to read ADC channels and it requires me to start my GUI program with sudo python SimB.py on the Debian terminal and an attached keyboard to the BBB.

I tried the following, first without the “root” in the command and then with the “root” in the command as shown in this crontab -l output:
fcjfibaj.png

ffieffjc.png

I will set this aside for now and try systemd, to see if I can get some kind of error message at least.
John

John,

I wrote this around 3 years ago, and should technically still work on Jessie.

http://www.embeddedhobbyist.com/2013/06/beaglebone-black-init-scripts-default-gatewayand-ntpdate/

If you ignore everything before#### The fix( hack ? ) Killing two birds with one stone.You can technically copy paste the script text I have there, remove the commands I’m running, and replace with command needed to run your script. You should also not have

use sudo inside this script file, as I believe init scripts run as root already.

Anyway I’m not 100% sure this will work, sometimes systemd steps on sysv init scripts, but it’s probably worth a try.

ffieffjc.png

fcjfibaj.png

Some glimmer of hope: I was able to get my Python code running, at least for a few seconds. I put an icon of sorts on the BBB Debian desktop; the icon/shortcut called an sh that started my Python code using sudo python myProg.py. But unfortunately it shut down in a few seconds and an LXDE window showed up saying something about Shut Down or Cancel or Something Else but the BBB shut down and I was not able to recover from the glitch. I started from the info by Cyril Laury at http://askubuntu.com/questions/299052/how-to-execute-sh-script-from-a-desktop-shortcut and hacked around to make a startup script. So it seems that my Python program is clobbering Debian, possibly with my write to a logfile. But that’s to be determined. So hopefully I can fix the problem and then I’ll be happy with this means to start up my Python code and will happily forget about crontab’s and systemd’s.
Thanks for all the help and suggestions. You-all kept me going.
John

I had more trouble but finally got my desktop icon working to start up my Python program. It turned out to be quite simple. Here’s my icon file:

[Desktop Entry]
Version=1.0
Exec=sudo /home/debian/Desktop/paint.sh
Name=Paint
Comment=Start up SimB.py
Encoding=UTF-8
Terminal=false
Type=Application

and here’s my script to start my Python program:
#!/bin/bash
sudo python /home/debian/Desktop/SimB.py

I tried and tried and tried to get crontab running and systemd running but was stymied by Tkinter errors. Tkinter requires the sudo to run my python GUI program and I couldn’t figure out how to do that with crontab and systemd. I think there must be a way to get crontab or systemd to work to start my SimB.py program on boot, but I couldn’t figure out how to do so.

The problems I was having a few days ago were with the Beaglebone ADC, that it requires some delay time before re-measuring a voltage and I was running a For loop to measure the voltage four times to get an average. Somehow, the lack of a delay clobbered Debian. I cut out the For loop, finding that I didn’t need to do the averaging, and was able to get the PyBBIO adcRead function to run ok. The ADC problem had not shown up previously and of course the problem would pop up when I was trying to get the startup icon to start my program. Very weird!

Thanks again for all the help.
John
johnbakeree.blogspot.com

The problem with sudo, is that it’s root running for a single executable - Typically. so in cases like this:

$ sudo apt-get update && apt-get upgrade

sudo wont work for both commands, only the first. But this is a very simplified example.

Additionally, one thing that many may not know, or just may not think of is: sudo is root. So if you sudo a command, that requires a path to work. Something that is only in the users path environment won’t work. Because when you run sudo, you’re not your user for that single command - You’re root :wink: