linuxsystemdarchlinux-arm

Trigger CURL Request after boot using systemd


I'm trying to create a service that will trigger every time a raspberry pi boots. Currently the service runs a really simple script that sends a POST request to a web service endpoint I control. I can trigger said script manually and that part all works perfectly.

I'm struggling with the next step which is to get that script to run after the pi has finished booting. I also need to be able to get it to run without a user logging in.

CURL Script (algiers-startup.local)

#! /bin/bash

echo "Attempting CURL Request"
curl --data "param1=value1&param2=value2" http://10.68.159.28:3000/device

Systemd Service

[Unit]
Description=Algiers RaspberryPi Startup
After=network.target
Before=getty@tty1.service

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/algiers-startup.local
TimeoutSec=30
StandardOutput=tty
RemainAfterExit=no

[Install]
WantedBy=multi-user.target

I see no errors or outputs in the console, no hint that anything has happened at all.


Solution

  • I’ll assume your machine is already set up with Systemd. It’s controlled primarily through the systemctl command. I alias it as such since it’s awful to type all the time:

    alias sc=systemctl
    alias ssc='sudo systemctl'
    

    You just need to “enable” your service to have it run at boot:

    sc enable algiers-startup
    

    I’m not sure what distro you’re using, but on Arch and CentOS, you’ll want algiers-startup to live down in /usr/lib/systemd/system/.

    You can test your service with sc start algiers-start. journalctl can show you what’s happening.