0

I'm looking for a solution to launch a program when the Raspberry boot, but i need to have an internet connection before launching this program.

I saw few exemple with post-up command in interface file, but it's no more used with Stretch.

How to do this?

Edit: My program is doing a loop continuously after boot. I need waiting the network up, because program need to publish MQTT topic.

Edit2: not working anymore. Service file:

[Unit]
Description=mqtt2 Sartup Service
After=network-online.target

[Service]
ExecStart=/home/pi/python3 mqtt2.py

[Install]
WantedBy=network-online.target

Status:

● mqtt2.service - mqtt2 Sartup Service
   Loaded: loaded (/etc/systemd/system/mqtt2.service; enabled; vendor preset: enabled)
   Active: inactive (dead)

I've tested with a C-complied program and here with a Python3 script. But if I launch manually the script it's ok

  • Hi, ok, i've made a mistake it's not Jessie but Stretch ;) (sorry wrong Rspi) – morbak Jul 15 at 6:52
2

To manage to start programs you should use systemd Unit files. Because we don't know anything about your specific needs I can only give a general idea. Here is a very simple template you can use to start to solve your problem. Create a new service with:

rpi ~$ sudo systemctl --full --force edit yourstartup.service

In the empty editor insert these statements, save them and quit the editor:

[Unit]
Description=Your Startup Service
After=network-online.target

[Service]
ExecStart=/full/path/to/yourprogram

[Install]
WantedBy=network-online.target

Enable the new service with:

rpi ~$ sudo systemctl enable yourstartup.service

and reboot.

This simple template checks if your network is online assuming that with this an internet connection is also established. It does not check if you can really reach the destination on the internet.

  • Hi, ok i'll try. Is my program will be executed in sudo? i need it – morbak Jul 15 at 10:31
  • I've done it but doesn't seems to have my service running: pi@raspberrypi:~ $ systemctl -l status spa8 ● spa8.service - spa8 Loaded: loaded (/etc/systemd/system/spa8.service; enabled; vendor preset: enabled) Active: inactive (dead – morbak Jul 15 at 11:02
  • @morbak Please don't use comments for formated output. It isn't readable. Instead edit your question and extend it with additional information. What does your program do? Does it run only once after boot up or does it run permanent as a service in the background? – Ingo Jul 15 at 12:41
  • @morbak As you updated the question I have modified the Unit file to run your program as service always in the background. If it also does not run then edit your question and add the output of this command to it: systemctl status spa8.service. Does your program run from the command line? – Ingo Jul 15 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.