How to make python program to start automatically after RevPi Core power on

Topics about the Software of Revolution Pi
Post Reply
hikaru2001
Posts: 38
Joined: 18 Feb 2019, 04:13
Answers: 0

How to make python program to start automatically after RevPi Core power on

Post by hikaru2001 »

Hello,

I make a small python program and want to make it start automatically after RevPi Core 3 power on.
Could you tell me how to do it. Thank you!
User avatar
dirk
KUNBUS
Posts: 1947
Joined: 15 Dec 2016, 13:19
Answers: 4

Re: How to make python program to start automatically after RevPi Core power on

Post by dirk »

Hi the quick and dirty solution is to put the command to execute your program into "/etc/rc.local" just before line "exit 0".
The better choice is to create a Systemd process which may have dependencies and logging and monitoring capabilities.
hikaru2001
Posts: 38
Joined: 18 Feb 2019, 04:13
Answers: 0

Re: How to make python program to start automatically after RevPi Core power on

Post by hikaru2001 »

Hi Dirk,
I try the second way. I create a myscript.service file.
----------------------------------------------------------------------------------
[Unit]
Description=My service
After=network.target

[Service]
ExecStart=/usr/bin/python3 -u main.py
WorkingDirectory=/home/pi/myscript
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi

[Install]
WantedBy=multi-user.target
--------------------------------------------------------------------------
And copy the file to /etc/systemd/system

I also create a simply main.py
------------------------------------------------------------
print("PILZ")
-----------------------------------------------------------

However when I use command"sudo systemctl start myscript.service" and enter, there is nothing happen on my terminal.
Could you pls find the reason?
Thank you!
Attachments
b.jpg
b.jpg (91.14 KiB) Viewed 5809 times
User avatar
dirk
KUNBUS
Posts: 1947
Joined: 15 Dec 2016, 13:19
Answers: 4

Re: How to make python program to start automatically after RevPi Core power on

Post by dirk »

Hey thats a good start. Usually background processes use to print to log files and avoid stdout. There are many options available for the parameter "StandardOutput" have a look here: https://www.freedesktop.org/software/sy ... .exec.html
hikaru2001
Posts: 38
Joined: 18 Feb 2019, 04:13
Answers: 0

Re: How to make python program to start automatically after RevPi Core power on

Post by hikaru2001 »

Hi Dirk,

I want to realize the function when RevPi Core 3 power on, it will go into Python interface and show me the status of running. Then I can monitor the function like the picture.
Attachments
b.jpg
b.jpg (147.46 KiB) Viewed 5799 times
hikaru2001
Posts: 38
Joined: 18 Feb 2019, 04:13
Answers: 0

Re: How to make python program to start automatically after RevPi Core power on

Post by hikaru2001 »

Something to append:

My real python program will access to Internet service and write to a cloud server. Should the python program start automatically after the network of RevPi is recovered when it is powered on?
Attachments
c.jpg
c.jpg (211.97 KiB) Viewed 5794 times
User avatar
RevPiModIO
KUNBUS
Posts: 322
Joined: 20 Jan 2017, 08:44
Answers: 0
Contact:

Re: How to make python program to start automatically after RevPi Core power on

Post by RevPiModIO »

Alternatively you can use our RevPiPyLoad service, which will autostart you Python program and monitors it.
Have a look: https://revpimodio.org/en/revpipyplc-2/revpipyload/

You can install a GUI on a computer in your network to send the program to the RevPi and read the logs and things like that :D

Regards, Sven
python3-RevPiModIO - https://revpimodio.org/ || Der RevPi ist das Beste, was passieren konnte!
hikaru2001
Posts: 38
Joined: 18 Feb 2019, 04:13
Answers: 0

Re: How to make python program to start automatically after RevPi Core power on

Post by hikaru2001 »

Hi Sven,

I have tried RevPiPyLoad service and use revpipycontrol tool to monitor the log of my program. It works well!
Thank you so much!
Post Reply