OpenMarine
How to run a program at startup? - Printable Version

+- OpenMarine (https://forum.openmarine.net)
+-- Forum: OpenPlotter (https://forum.openmarine.net/forumdisplay.php?fid=1)
+--- Forum: How do I...? (https://forum.openmarine.net/forumdisplay.php?fid=3)
+--- Thread: How to run a program at startup? (/showthread.php?tid=3915)



How to run a program at startup? - Newbie1979 - 2022-02-07

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!


RE: How to run a program at startup? - baltika_no_9 - 2022-02-13

(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.


RE: How to run a program at startup? - emilecantin - 2022-02-14

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.


RE: How to run a program at startup? - baltika_no_9 - 2022-02-14

(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.


RE: How to run a program at startup? - PaddyB - 2022-02-14

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]


RE: How to run a program at startup? - Newbie1979 - 2022-02-15

(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!