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

My program runs fine but requires that I use putty.exe and type sudo python myprog.py to run it. I have been reading about systemctl and have written a service file and put it in /lib/systemd/system. It’s called paint-flow.service, listing below. I have… enabled it with systemctl enable paint-flow.service with no errors and tried systemctl start paint-flow.service with no errors but my program doesn’t seem to start. When I check the status I get “code=exited, status = 203/EXEC” which doesn’t tell me anything, at least nothing that I understand.

Also I tried the same thing with a one-line Hello.py program and same results.

Any help with this newbie problem, please?

[Unit]
Description=Paint flow control program

[Service]
WorkingDirectory=/home/debian/Desktop/
ExecStart=/home/debian/Desktop/python SimB.py
SyslogIdentifier=SimB
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.targetI am using Debian 3.8.13

John

On Thu, 7 Apr 2016 21:18:43 -0700 (PDT), John Baker
<bakerengineeringco@gmail.com> declaimed the
following:

ExecStart=/home/debian/Desktop/python SimB.py

  The way I read that line says to run a Python interpreter located in
/home/debian/Desktop, passing it a file named SimB.py (located who knows
where).

  I doubt you've installed a Python interpreter into your home directory.
I could see the line being:

ExecStart=python /home/debian/Desktop/SimB.py

to use the interpreter located on the regular search path (for something
starting as a service, likely the system Python).

  Though one page found on Google mentions that systemd starts up with
fully "clean" environments -- so possibly not even the system search path
will be available and you need to provide both the path to the python
executable and the path to the script.

  My other concern is: what do services use for stdout? Where would
anything from a print statement be sent? Maybe you need to provide output
redirection?

You are right Dennis. Excellent suggestions. Now I can get systemd to work and can run my simple Python program and write file to memory using stdout.
Now to get my GUI program to run. I think I may have clobbered something in the Debian or Tkinter or something since my GUI program is giving me a Tkinter error, the same one I had a couple of weeks ago and got past. But it's reared its ugly head again. Very annoying. I suspect I have to reload the Tkinter.

Ugly error:
_tkinter.TclError: no display name and no $DISPLAY environment variable

Thanks very much,
John

On Fri, 8 Apr 2016 21:11:22 -0700, John Baker
<johnbaker@ieee.org> declaimed the following:

You are right Dennis. Excellent suggestions. Now I can get systemd to
work and can run my simple Python program and write file to memory using
stdout.

  Out of curiosity, which suggestion did it (incorrect Python invocation,
or stdout redirection), or were both required?

Now to get my GUI program to run. I think I may have clobbered something
in the Debian or Tkinter or something since my GUI program is giving me
a Tkinter error, the same one I had a couple of weeks ago and got past.
But it's reared its ugly head again. Very annoying. I suspect I have to
reload the Tkinter.

Ugly error:
_tkinter.TclError: no display name and no $DISPLAY environment variable

  Since Tkinter just invokes the external Tcl/Tk libraries, I'd interpret
the above to mean just what it says... Tk can't find an environment
variable providing the reference to the X-window display server. Did you
log-in through the windowing system?

  If you're trying to run a GUI via systemd I don't think you'll have
much luck -- systemd (or other init process system) don't have
displays/consoles (which is why stdio likely had to be redirected; one is
probably supposed to be using the system logging functions for such).

  I'll admit I'm very stale on Linux -- I'm still pondering what changes
occurred between the March 2015 image and the November 2015 image (I'm
still running Wheezy -- Jessie appears to have broken too much stuff, at
least with regards to books). I know I used to have a "task bar" at the
bottom of the screen with the former (along with the Beagle), but the
latter just has a context menu offering console terminal, web browser, and
switching desktop screens (and is otherwise completely blank/black -- if it
wasn't for the mouse pointer I'd have thought the display wasn't even
connected). I'm also not certain Cloud 9 is still functioning in the
November image.

Dennis,
I did both since I realized you were right on both.

I had fixed the Tkinter problem a couple or three weeks ago and somehow when I was trying to get systemd to work, I must have clobbered Tkinter or something. So now I am reloading Debian Wheezy, bone50. I also was having trouble with the latest Wheezy, 79. Don't know why. I was using putty to start my GUI program with a keyboard and mouse connected to the BBB thru a USB hub, per suggestion by Steve Plant on the BBB users forum. I have myProg.py loaded at /home/debian/Desktop and found that I had to start it with sudo python myProg.py. That has worked fine for a couple of weeks or so and I was dismayed to see the Tkinter error show up again. I have a 4D Systems 4DCAPE-70T attached to the BBB to give me the GUI display and like I say, it was working terrifically for the past two or three weeks.

Hopefully I will be able to get systemd to start my GUI program. I would like it to start up upon resetting the BBB. Or maybe I can have a script on the Debian desktop that I can get to trigger my GUI program? Somehow I need to make it easy for the users to start the code, not requiring the mouse and keyboard. Any suggestions?

I couldn't make any sense of the Debian bone79, just seeing the blank screen like you said. Very strange, the bone79.
Thanks, John

On Sat, 9 Apr 2016 21:12:00 -0700, John Baker
<johnbaker@ieee.org> declaimed the following:

Hopefully I will be able to get systemd to start my GUI program. I would
like it to start up upon resetting the BBB. Or maybe I can have a script
on the Debian desktop that I can get to trigger my GUI program? Somehow
I need to make it easy for the users to start the code, not requiring
the mouse and keyboard. Any suggestions?

  I'm not accustomed to systems that "start" in a user account (at least,
not since my TRS-80 and Amiga days -- even W95 required a login, although
it was a blank password, as I recall).

  Possibly something in the account .profile can determine if an X-window
environment is available and start your program as a background task?

  I don't know under what conditions the X-window environment is
started... Does it detect HDMI/LCD connection, or does it always create an
auto-login X-window session regardless...

W95 required a login, although
it was a blank password, as I recall).

Getting way off topic here but . . . Windows 95 had zero passwd protection. You could set a passwd, but it would not do anything . . . so a person could enter a blank passwd at the screen saver, and just log back in - heh.

A cron job maybe?

I was also looking to run my python script on boot so I did a couple things that worked as a a short term solution. I added 2 lines to my crontab, an @reboot line to run the program when the device starts up as well as a bash script to ensure the program is still running.

(Replace main.py with your program name and make sure it doesn’t match anything else that may be running)

crontab -e

Runs when the system boots:

@reboot sudo python /path/to/program.py

Runs every minute and checks that your program is running:

          • sudo bash /path/to/check_process.bash

(Just a note, if you need multiple programs to run on boot, replace “@reboot sudo python /path/to/program.py” with a bash script, and place the things you need to run on boot in that bash file.)

check_process.bash:

#!/bin/bash

process_id=ps aux | grep "YOURPROGRAM.py" | grep -v "grep";

if [ -z “$process_id” ]; then

echo “process not running for certain.”

cd /PATH/TO

cd /PATH/TO/YOURPROGRAM.py &

else

echo “YOURPROGRAM.py seems to be running”;

fi

The only down side is that if your program crashes, you have to wait up to a minute for the crontab to run the check_processes.bash, but otherwise is pretty solid.

Now I am also looking for how to run my script as a system service.

John Minton,
This looks really helpful. Also I was thinking about making an icon on the Desktop to run a bash script, probably something very easy but I am very ignorant of bash and Linux, so will have to study up on cron and bash, and here I thought all I had to learn was Python and Tkinter and Beaglebone I/O. :slight_smile:
Thanks,
John

John Minton,
I gave crontab a try and of course it didn’t work. It seems like this should be a piece of cake to do. I probably did something wrong. I checked the syslog and see an error: “No MTA installed.”

My crontab file says:
@reboot sudo python /home/debian/Desktop/myProg.py

          • sudo python /home/debian/Desktop/myProg.py

On boot, the Demian desktop shows up and myProg does not run. Waiting for a minute, still no joy.

Do I need to install a mail system (I’m completely ignorant, of course)?
John

John Minton,
I gave crontab a try and of course it didn't work. It seems like this
should be a piece of cake to do. I probably did something wrong. I checked
the syslog and see an error: "No MTA installed."


If u​

​have an error in ​crontab the crontab tries to send you an Email... And of
coarse cannot without MTA;)

My crontab file says:

​​
@reboot sudo python /home/debian/Desktop/myProg.py
* * * * * sudo python /home/debian/Desktop/myProg.py

​The are two crontabs, users and system crontab.
Users crontab you can change with
$ crontab -e
System crontab with
$ sudo nano /etc/crontab

If u want to run a process as root, then best is, if u add

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

to /etc/crontab

HTH

In the "old" days, you would create an entry in /etc/inittab and let
init start your program at the appropriate time. This also has the
benefit that init will restart your program if it crashes for some reason.

You can do something similar with systemd, but you need to make a
small service file. Here's a fairly simple walk-through:

http://n3mesisfixx.blogspot.com/2012/08/migrate-etcinittab-job-to-systemd-in.html

In the “old” days, you would create an entry in /etc/inittab and let
init start your program at the appropriate time. This also has the
benefit that init will restart your program if it crashes for some reason.

You can do something similar with systemd, but you need to make a
small service file. Here’s a fairly simple walk-through:

http://n3mesisfixx.blogspot.com/2012/08/migrate-etcinittab-job-to-systemd-in.html

Great link Charles, thanks for sharing. I did not even think about the inittab respawn option. . .

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.

Here’s my crontab in /etc:

/etc/crontab: system-wide crontab

Unlike any other crontab you don’t have to run the `crontab’

command to install the new version when you edit this file

and files in /etc/cron.d. These files also have username fields,

that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

m h dom mon dow user command

17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

If this is a GUI only program you need to start your program
after the GUI is up, I don't see this check in your crontab file
Been too long since i have done this but i am sure google is your friend

Hi Wulf Man,
My GUI is part of a simulation program. There’s a lot of calculation and the GUI to allow me to enter some parameters and display the results on a graph. You’re saying that I have to start up my calculation part of the program and then the GUI?
John

I Am saying that if your depending on the GUI for any part of your
program it may expect the xwindow system to be active before
it starts, then seeing that the GUI is not running just exits.

It's hard to second guess what your program is doing here but it "may"
be the issue.

try checking for the xwindow system being running before starting your
program.

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.
Thanks for your suggestion,
John

For debugging u can redirect the output into a file, eg:

          • root /usr/bin/python /home/debian/Desktop/SimB.py >> /home/debian/err.txt 2>&1

(write this all in one line)

However, if your program is an X program the error will be something like:
“cannot open display”

But: All Window Managers i know provide methods to autostart a x-program…
If your WM is lxde the u will find a good explanation here:
https://wiki.archlinux.org/index.php/LXDE#Autostart