Autostart python folder

Topics about the Software of Revolution Pi
Post Reply
waischa
Posts: 8
Joined: 06 Jun 2023, 11:30
Answers: 0

Autostart python folder

Post by waischa »

Hi all,

I have been trying to autostart my revpi python scripts and the only thing working is with GUI. However, I want to run my python script as a script or command prompt since it might lessen the workload of the revpi (I want Revpi to run 24/7).

Anyone can help guide me to the right direction to autostart my python script in RevPi?

Thank you! :)
User avatar
RevPiModIO
KUNBUS
Posts: 322
Joined: 20 Jan 2017, 08:44
Answers: 0
Contact:

Re: Autostart python folder

Post by RevPiModIO »

Hi waischa.

Did I understand you correctly that your Python program does not contain any GUI elements, so it could actually run on the command-line?

Then you would have the possibility to use RevPiPyLoad. The service starts your program automatically and also monitors it. If it crashes, it can be restarted and you can view the logs in `/var/log/revpipyloadapp`. The service on the RevPi must be enabled in the revpi-webstatus web GUI under "Services" "Enable/Disable RevPiPyLoad". Then, for example, the program can be stored in `/var/lib/revpipyload` with the name `program.py` or you can transfer it with the RevPiCommander.

https://revpimodio.org/en/revpipyplc-2/revpipyload/

The RevPiCommander can find your RevPi over the network and offers other functions such as an IO monitor in addition to transferring your Python programs. It is available for any platform.

https://revpimodio.org/en/sources/revpipycommander/
python3-RevPiModIO - https://revpimodio.org/ || Der RevPi ist das Beste, was passieren konnte!
kjkoster
Posts: 87
Joined: 12 Feb 2022, 10:42
Answers: 2

Re: Autostart python folder

Post by kjkoster »

Dear waischa,

For Python scripts on bare metal we use systemd: https://www.makeuseof.com/what-is-syste ... pberry-pi/ The advantage is that this is how it works on all Raspberry Pi based and even most other Linux systems, not just Revolution Pi. Also, you can have as many autostarting scripts as you like.

Alternatively you can use Docker, but that may be more suitable if you find yourself building a bunch of Python and other services. https://github.com/kjkoster/revpi-docke ... qtt-blinky

Finally, there is RevPiModIo's suggestion, which works well for a single script. This is the most PLC-like solution.

Kees Jan
waischa
Posts: 8
Joined: 06 Jun 2023, 11:30
Answers: 0

Re: Autostart python folder

Post by waischa »

RevPiModIO wrote: 08 Jun 2023, 07:21 Hi waischa.

Did I understand you correctly that your Python program does not contain any GUI elements, so it could actually run on the command-line?

Then you would have the possibility to use RevPiPyLoad. The service starts your program automatically and also monitors it. If it crashes, it can be restarted and you can view the logs in `/var/log/revpipyloadapp`. The service on the RevPi must be enabled in the revpi-webstatus web GUI under "Services" "Enable/Disable RevPiPyLoad". Then, for example, the program can be stored in `/var/lib/revpipyload` with the name `program.py` or you can transfer it with the RevPiCommander.

https://revpimodio.org/en/revpipyplc-2/revpipyload/

The RevPiCommander can find your RevPi over the network and offers other functions such as an IO monitor in addition to transferring your Python programs. It is available for any platform.

https://revpimodio.org/en/sources/revpipycommander/
Hi, thank you for the information, sorry for the late reply because I was busy on other things. I have tried this method, I activated RevPiPyLoad via web and have inserted my program into the folder. I have already rename my program to "program.py" and I reboot my Revpi. The program didn't run, any idea why?

I tried running the program using the terminal

Code: Select all

sudo python3 /var/lib/revpipyload/program.py 
and the program can be executed, this means that there is no problem with my code right? Am I missing something here?
User avatar
RevPiModIO
KUNBUS
Posts: 322
Joined: 20 Jan 2017, 08:44
Answers: 0
Contact:

Re: Autostart python folder

Post by RevPiModIO »

Please check the `autostart=1` parameter in /etc/revpipyload/revpipyload.conf. It must be `1`.

Checkout `plcuid` and `plcgid` maybe you have to change the values, if you need your script running as root.

https://revpimodio.org/en/revpipyplc-2/revpipyload/

Regards
Sven
python3-RevPiModIO - https://revpimodio.org/ || Der RevPi ist das Beste, was passieren konnte!
waischa
Posts: 8
Joined: 06 Jun 2023, 11:30
Answers: 0

Re: Autostart python folder

Post by waischa »

RevPiModIO wrote: 15 Aug 2023, 15:27 Please check the `autostart=1` parameter in /etc/revpipyload/revpipyload.conf. It must be `1`.

Checkout `plcuid` and `plcgid` maybe you have to change the values, if you need your script running as root.

https://revpimodio.org/en/revpipyplc-2/revpipyload/

Regards
Sven
Hi Sven,

Yes, I need it to run with root. I have already set the things I think is correct, below is my current setting for revpipyreload:

Code: Select all

[DEFAULT]
autoreload = 1
autoreloaddelay = 15
autostart = 1
plcworkdir = /var/lib/revpipyload
plcworkdir_set_uid = 1
plcprogram = program.py
plcprogram_stop_timeout = 5
plcprogram_watchdog = 1
plcarguments =
plcuid = 0
plcgid = 0
pythonversion = 3
replace_ios = /etc/revpipyload/replace_ios.conf
reset_driver_action = 2
rtlevel = 0
zeroonerror = 1
zeroonexit = 0

[PLCSLAVE]
plcslave = 0
aclfile = /etc/revpipyload/aclplcslave.conf
bindip = *
port = 55234
watchdog = 1

[XMLRPC]
xmlrpc = 1
aclfile = /etc/revpipyload/aclxmlrpc.conf
bindip = 127.0.0.1

[MQTT]
mqtt = 0
basetopic = revpi0000
sendinterval = 15

[PLCSLAVE]
plcslave = 0
aclfile = /etc/revpipyload/aclplcslave.conf
bindip = *
port = 55234
watchdog = 1

[XMLRPC]
xmlrpc = 1
aclfile = /etc/revpipyload/aclxmlrpc.conf
bindip = 127.0.0.1

[MQTT]
mqtt = 0
basetopic = revpi0000
sendinterval = 15
send_on_event = 0
write_outputs = 0
broker_address = localhost
port = 1883
tls_set = 0
username =
password =
client_id =
Thank you! :)
User avatar
RevPiModIO
KUNBUS
Posts: 322
Joined: 20 Jan 2017, 08:44
Answers: 0
Contact:

Re: Autostart python folder

Post by RevPiModIO »

And it's running now?

Are the sections [MQTT], [XMLRPC], etc. also double in your configuration file? If so, please delete the duplicate occurrences completely.

The value `plcprogram_watchdog = 1` is not good! The number is the timeout in seconds. If you activate the software watchdog, it must also be triggered in the program (which is the same BIT as the RevPi Connect uses for the hardware watchdog) or you use the functions of RevPiModIO. The value of 1 means that the software watchdog must be detected within a second, that is too close! If you use the watchdog in your program, you should set it higher about 10-30 or disable it with a 0.

Please also check the logs: `cat /var/log/revpipyload`

Greeting
Sven
python3-RevPiModIO - https://revpimodio.org/ || Der RevPi ist das Beste, was passieren konnte!
waischa
Posts: 8
Joined: 06 Jun 2023, 11:30
Answers: 0

Re: Autostart python folder

Post by waischa »

RevPiModIO wrote: 16 Aug 2023, 08:08 And it's running now?

Are the sections [MQTT], [XMLRPC], etc. also double in your configuration file? If so, please delete the duplicate occurrences completely.

The value `plcprogram_watchdog = 1` is not good! The number is the timeout in seconds. If you activate the software watchdog, it must also be triggered in the program (which is the same BIT as the RevPi Connect uses for the hardware watchdog) or you use the functions of RevPiModIO. The value of 1 means that the software watchdog must be detected within a second, that is too close! If you use the watchdog in your program, you should set it higher about 10-30 or disable it with a 0.

Please also check the logs: `cat /var/log/revpipyload`

Greeting
Sven
Hi Sven,

Sorry, it seems that I copied wrongly and it duplicates, the one in my revpi is the one with no duplications, please refer below:

Code: Select all

[DEFAULT]
autoreload = 1
autoreloaddelay = 15
autostart = 1
plcworkdir = /var/lib/revpipyload
plcworkdir_set_uid = 1
plcprogram = program.py
plcprogram_stop_timeout = 5
plcprogram_watchdog = 0
plcarguments = 
plcuid = 0
plcgid = 0
pythonversion = 3
replace_ios = /etc/revpipyload/replace_ios.conf
reset_driver_action = 2
rtlevel = 0
zeroonerror = 1
zeroonexit = 0

[PLCSLAVE]
plcslave = 0
aclfile = /etc/revpipyload/aclplcslave.conf
bindip = *
port = 55234
watchdog = 1

[XMLRPC]
xmlrpc = 1
aclfile = /etc/revpipyload/aclxmlrpc.conf
bindip = 127.0.0.1

[MQTT]
mqtt = 0
basetopic = revpi0000
sendinterval = 15
send_on_event = 0
write_outputs = 0
broker_address = localhost
port = 1883
tls_set = 0
username = 
password = 
client_id = 
I have followed your advice for now and disabled watchdog. This is what I get from `cat /var/log/revpipyload` previously:

Code: Select all

2023-05-24 23:27:12 [WARNING ] restart plc program after crash
2023-05-24 23:27:13 [ERROR   ] plc program crashed - exitcode: 1
2023-05-24 23:27:13 [WARNING ] set piControl0 to ZERO after PLC program error
2023-05-24 23:27:28 [WARNING ] restart plc program after crash
2023-05-24 23:27:29 [ERROR   ] plc program crashed - exitcode: 1
2023-05-24 23:27:29 [WARNING ] set piControl0 to ZERO after PLC program error
After making the changes, I have key in these command:

Code: Select all

sudo service revpipyload reload
sudo service revpipyload start
However, the script still didn't run.. my code actually suppose to send data to a register and it suppose to show up on the other side (read register), do i need to add specific revpi command into my code for the auto start to work?

Thank you! :)
User avatar
RevPiModIO
KUNBUS
Posts: 322
Joined: 20 Jan 2017, 08:44
Answers: 0
Contact:

Re: Autostart python folder

Post by RevPiModIO »

Could you check also the log file: `cat /var/log/revpipyloadapp`?

It looks like the program crashes, the outputs from the program itself are in the file `revpipyloadapp`, I forgot to mention in the last post, sorry. :D
python3-RevPiModIO - https://revpimodio.org/ || Der RevPi ist das Beste, was passieren konnte!
waischa
Posts: 8
Joined: 06 Jun 2023, 11:30
Answers: 0

Re: Autostart python folder

Post by waischa »

RevPiModIO wrote: 16 Aug 2023, 09:48 Could you check also the log file: `cat /var/log/revpipyloadapp`?

It looks like the program crashes, the outputs from the program itself are in the file `revpipyloadapp`, I forgot to mention in the last post, sorry. :D
Hi Sven,

It is running now, that log file is very useful! It seems that when I was running the code in terminal or thonny, there are no errors but I see the problem inside the log file. I have already solved the issue and it autoruns at start up!!

Now, I have 2 follow up questions:
1) I want to activate watchdog, if I am understanding this correctly, watchdog will restart the revpi if the revpi freezes or crash right? Do I do it here in revpipyload?
2) I see the logfile with all the error on it, is it safe to delete them because I need to check the latest logfile instead of reading the old ones.

Thank you very much for the help so far! :)
Post Reply