0

I've followed this guide to get a 4G hat networked with my pi zero, and everything seems fine. I'm able to ping out once pon has started in the background and a route has been added.

What I want is for the pi to boot and for pon to be running already, and the route to be already added.

I'm currently doing this in the rc.local file, but I'm pretty sure it's quite a dirty approach:

sleep 20
sudo pon &
sleep 20
sudo route add -net "0.0.0.0" ppp0

exit 0

Ultimately, I want to be sure that the network is up on the newly defined ppp0 route before executing any python.

I've had a look at this similar question but unsure of the correct approach: could this be done with pre-up.d or some kind of interfaces change?

Any help would be greatly appreciated.

2 Answers2

0

You can create a script that executes on startup via systemd.

First create a script for the commands. Ensure that it has execution permissions.

Next create a service file in /etc/systemd/system with a filename that ends with .service and containing the following sections:

[Unit]
Description=Description of script/service

[Service] ExecStart=/bin/bash [path to script]

[Install] WantedBy=multi-user.target

Then reload the systemctl daemon

systemctl daemon-reload

Finally, install the script using systemctl, with the filename as the servicename:

systemctl enable --now [servicename]

Related:

  • So I'm thinking that this will need x3 services? x1 for pon, x1 for the route add after pon has started, and then the final for the python that has to be ran when the connection is up? I have amended my question with a work in progress. What do you think? – Harry Lincoln Jan 12 '23 at 16:28
  • @HarryLincoln I didn't realize you could add the commands directly into the ExecStart Looks pretty good. Only thing I can see that may be problematic is the space between /usr/bin and source in the third service. Not sure if the path is needed since source is an internal command. Shouldn't hurt to try it though. Provided the Pi can be rebooted for testing without interrupting production usage. – Jonathan Heady Jan 12 '23 at 17:14
  • 1
    Yeah, that should be something like ExecStart=/bin/bash -c ' source /home/you/projects/bike-shed/venv/bin/activate && python3 /home/you/do-the-thing.py' but in practice you can probably simplify to just ExecStart=/home/you/projects/bike-shed/venv/bin/python3 /home/you/do-the-thing.py. Notice that systemd runs as root so ~ would expand to /root, not your own home directory. – tripleee Jan 12 '23 at 17:21
  • (Properly speaking, system scripts should be owned by root and stored in a system location like /usr/local/bin or perhaps e.g. /usr/local/lib) – tripleee Jan 12 '23 at 17:22
0

Answering my own question:

How I did it was to create x2 systemd services and a change to the routing table.

  1. 4gpon.service used to start pon in the background:
[Unit]
Description=4G hat networing

[Service] Type=forking ExecStart=/usr/bin/pon ExecStop=/usr/bin/poff Restart=always

[Install] WantedBy=multi-user.target

  1. Updating the routing table to enable ppp0 by default
sudo ip route delete default
sudo ip route add default dev ppp0
sudo nano /etc/network/interfaces.d/ppp0

add:

auto ppp0 iface ppp0 inet provider up ip route add default dev ppp0

Then in addition to the values defined here for this file, add this one too:

sudo nano /etc/ppp/peers/provider

add at bottom:

replacedefaultroute

  1. The garage-motion.service that calls the python upon knowing the first service is up
[Unit]
Description=Motion detector service
After=network.target network-online.target 4gpon.service

[Service] Type=simple Environment="AWS_DEFAULT_REGION=*your-region" Environment="AWS_ACCESS_KEY_ID=your-key" Environment="AWS_SECRET_ACCESS_KEY=your-secret-key*" ExecStart=/bin/sh -c '/home/pi/projects/garage-motion/venv/python3 /home/pi/projects/garage-motion/venv/garage-motion.ph --serve-in-foreground' Restart=on-abort

[Install] WantedBy=multi-user.target