0

------------- instructions to configure systemctl -------------

[Running a script at bootup : Good practices?, rc.local falling, Other options?

The following is the selected answer to the above question. It was written by Ingo. I can't make it work.

Create a new systemd unit with:

pi@raspberrypi:~ $ sudo systemctl --force --full edit thonny.service

In the editor insert these statements, save it and quit the editor:

[Unit]
Description=Service to start thonny
Wants=graphical.target
After=graphical.target

[Service]
User=pi     # or must it run as root? Then omit this line
#ExecStart=/home/pi/thonny   # or where your program is
ExecStart=/home/pi/testAutostart.py   # replaced the above line with this

[Install]
WantedBy=graphical.target

Enable the new service:

pi@raspberrypi:~ $ sudo systemctl enable thonny.service

reboot.

Check with:

pi@raspberrypi:~ $ systemctl status thonny.service

pi@raspberrypi:~ $ systemctl cat thonny.service

Edit again with:

pi@raspberrypi:~ $ sudo systemctl --full edit thonny.service

--------------- testAutostart.py -------------

#!/usr/bin/thonny
# added the above line
# testAutostart.py

import time

cmdfile = "cmdfile.txt"
cf = open (cmdfile,"w")
# The flag cmd is changed external to this program, and forces a restart.
cmd = 1
cf.write (str(cmd))
cf.close
isRunning = cmd
cntr = 0
wait = 10

while isRunning == cmd:
    cntr += 1
    print("Hello World:", cntr)
    cf = open (cmdfile,"r")
    cmd = int(cf.read())
    cf.close
    if isRunning != cmd:
        print ("check interrupt")
        if cmd == 0:
            import os
            os.system("sudo reboot -h now")
        else: # incremented
            exit()

    time.sleep(wait)

---------------- cmdfile.txt ----------------

1

-------------- error 203--------------------

pi@raspberrypi:~ $ systemctl status thonny.service

● thonny.service - Service to start thonny
   Loaded: loaded (/etc/systemd/system/thonny.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Fri 2018-12-14 10:27:26 MST; 8min ago
  Process: 549 ExecStart=/home/pi/testAutostart.py (code=exited, status=203/EXEC)
 Main PID: 549 (code=exited, status=203/EXEC)

Dec 14 10:27:26 raspberrypi systemd[1]: Started Service to start thonny.
Dec 14 10:27:26 raspberrypi systemd[1]: thonny.service: Main process exited, code=exited, status=203/EXEC
Dec 14 10:27:26 raspberrypi systemd[1]: thonny.service: Unit entered failed state.
Dec 14 10:27:26 raspberrypi systemd[1]: thonny.service: Failed with result 'exit-code'.

------------ analysis --------------------

status=203 is file not found
what file is not found ?
testAutostart.py 
or
/usr/bin/thonny
or 
/usr/bin/python
or 
/usr/bin/python3
or 
/usr/bin/python3.5
or
is status=203 something else

------------ modifications --------------------

in systemctl replaced:
ExecStart=/home/pi/thonny   # or where your program is
with
ExecStart=/home/pi/testAutostart.py   # or where your program is
tried
ExecStart=/home/pi/thonny testAutostart.py   # or where your program is


in testAutostart.py top line added:
#!/usr/bin/python
then it changed to 
#!/usr/bin/thonny
Ingo
  • 42,107
  • 20
  • 85
  • 197
  • 2
    Hello and welcome. Please take the tour and visit the helpcenter to see how things work here. I wonder why you want to autostart your python script through thonny? – Ghanima Dec 22 '18 at 20:50
  • I can't make it work. is a very poor description of the problem that you are having ..... why did you post the selected answer when it does not resolve your problem? ..... you have not asked a question and you have not provided any information that could be used to solve the problem – jsotola Dec 22 '18 at 23:21
  • @Ghanima - The above script is for my learning. The actual script is a real time monitor and will be used by non-programmers. I thought thonny would be a good selection. – Gerard Gilliland Dec 23 '18 at 04:17
  • @jsotola -- Guilty as charged. I wanted to ask. "How can I autostart a python script in thonny on boot?" and I felt that you would point me to the one I found and say "The answer is already posted here." It would look as though I hadn't looked for the answer. – Gerard Gilliland Dec 23 '18 at 04:23
  • @jsotola -- My real question is, "I believe status=203 means file not found. In the above code, ExecStart=/home/pi/testAutostart.py, what file is not found?" – Gerard Gilliland Dec 23 '18 at 04:33
  • found this ... https://unix.stackexchange.com/questions/472950/systemd-status-203-exec-error-when-creating-new-service – jsotola Dec 23 '18 at 06:26

1 Answers1

1

The error message (code=exited, status=203/EXEC) is often seen when the script itself or its interpreter cannot be executed. It could have this reasons:
- wrong path to script (e.g. /home/py/testAutostart.py)
- script not executable
- no shebang (first line)
- wrong path in shebang (e.g. /bin/thonny)
- thonny cannot execute a script the same way than the python interpreter

I do not have thonny installed so I can't tell much about it. I would test with a two liner script testAutostart.py:

rpi ~$ mv testAutostart.py testAutostart.py~
rpi ~$ cat > testAutostart.py <<EOF
#!/usr/bin/thonny
print("hello world")
EOF

rpi ~$ chmod 755 testAutostart.py
rpi ~$ ./testAutostart.py   # first char is a dot
hello world   # don't know how thonny this print

rpi ~$ sudo systemctl start thonny.service
rpi ~$ systemctl status thonny.service

To check if it is an issue with thonny just replace the shebang with !#/usr/bin/python3 and repeat the tests. Then you should see "hello world" in the status.

Ingo
  • 42,107
  • 20
  • 85
  • 197
  • Thank you for your comments. I appreciate them. I was hoping you would find this page since I was quoting you. I didn't start thonny from boot. No room here to show the errors. But at this point it looks like it is in thonny's court or there could be an option I am overlooking. You were wise to limit the file to two lines. I AutoStarted the larger file with python3 and got to the first file write when I got an access permission error. Un-related, I've heard you could delete a file using the move function. After seeing it, it makes sense. Thank you and Peace, Gerard – Gerard Gilliland Dec 24 '18 at 02:45
  • @GerardGilliland No problem to find your comment. I get a message if someone comments on my answer. But that's only true on my answer. Otherwise you have to address me (and others) with its name how I did it with your name here. With mv I have made a backup from your script because the next command would overwrite it. – Ingo Dec 24 '18 at 08:40