OpenMarine

Full Version: How to run a program at startup?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, 
I am looking for a way to initiate a python script upon start-up (it is a 1sec restart/ 3 sec safe shutdown button script). On my other RPI sec installation it a simple edit of rc.local, but that does not seem to do the trick in Openplotter (v2).
Thanks for your help!
(2022-02-07, 10:37 PM)Newbie1979 Wrote: [ -> ]Hi, 
I am looking for a way to initiate a python script upon start-up (it is a 1sec restart/ 3 sec safe shutdown button script). On my other RPI sec installation it a simple edit of rc.local, but that does not seem to do the trick in Openplotter (v2).
Thanks for your help!

Have you tried cron?

In a terminal edit the cron file with the command

Code:
crontab -e

Select your preferred editor (you only do this once) and at the bottom of the file add the following (here I am assuming I have a script called test.py and it is located in /home/pi/, adjust this of course for your particular circumstances.

Code:
@reboot python3 /home/pi/test.py

If you reboot then the script should have executed.

If your script requires root privilege then precede the "python3" with "sudo"

To test it start perhaps with a trivial script which just creates a file called somefile.txt, here's an sample test.py to do this:

Code:
import os
os.system('touch somefile.txt')

I always like to test this manually. Run in a terminal window

Code:
python3 /home/pi/test.py

There should now be a file called somefile.txt in the directory you ran the command from.

DELETE IT then reboot and a new one should be created.

Having proved the process use the real script you want to execute.
Recent versions of Debian have switched over to systemd, look at how to write a systemd unit for your python script. It's waaaay easier than it used to be with rc files, just a few lines of configuration and a command to enable it.
(2022-02-14, 02:03 PM)emilecantin Wrote: [ -> ]Recent versions of Debian have switched over to systemd, look at how to write a systemd unit for your python script. It's waaaay easier than it used to be with rc files, just a few lines of configuration and a command to enable it.

Yes but a single line in crontab is even easier in my view. It works in any linux system so it isn't a concern if the distro you're using suddenly changes how it wants to handle things.
Another option is to run a command in node-red, this means it will only run  after openplotter & signalk have booted up & if you change to a new installation it will be inside the node-red flows backup. The inject node can be set to run once at startup which will then trigger the exec node.

Image below is a python 3 file with containing just "print("test worked")"

[Image: BCJkFmc.png]
(2022-02-13, 05:40 PM)baltika_no_9 Wrote: [ -> ]
(2022-02-07, 10:37 PM)Newbie1979 Wrote: [ -> ]Hi, 
I am looking for a way to initiate a python script upon start-up (it is a 1sec restart/ 3 sec safe shutdown button script). On my other RPI sec installation it a simple edit of rc.local, but that does not seem to do the trick in Openplotter (v2).
Thanks for your help!

Have you tried cron?

In a terminal edit the cron file with the command

Code:
crontab -e

Select your preferred editor (you only do this once) and at the bottom of the file add the following (here I am assuming I have a script called test.py and it is located in /home/pi/, adjust this of course for your particular circumstances.

Code:
@reboot python3 /home/pi/test.py

If you reboot then the script should have executed.

If your script requires root privilege then precede the "python3" with "sudo"

To test it start perhaps with a trivial script which just creates a file called somefile.txt, here's an sample test.py to do this:

Code:
import os
os.system('touch somefile.txt')

I always like to test this manually. Run in a terminal window

Code:
python3 /home/pi/test.py

There should now be a file called somefile.txt in the directory you ran the command from.

DELETE IT then reboot and a new one should be created.

Having proved the process use the real script you want to execute.

Awesome. Many thanks for taking the time to help!