This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to run a program at startup?
#1
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!
Reply
#2
(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.
Reply
#3
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.
Reply
#4
(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.
Reply
#5
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]
Reply
#6
(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!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)