Running Scripts On Boot Using Systemd

Running Scripts On Boot Using Systemd

One of the best ways to run scripts on boot in Linux by using systemd.

Table of contents

Sometimes one may wish to run scripts at boot. I looked at a lot of solutions including crontab but the only solution that worked for me was systemd. This can be done for all Linux systems but since I did this on the Raspberry Pi I have included it here.

Getting started

We will be creating a .service file in /usr/lib/systemd/system.

This will be followed by sudo nano /etc/systemd/system/discord-webhook.service. In the file you will include the following lines of code

Description=Discord Webhook IP sender


ExecStart=python /bin/

Once done hit Ctrl+X followed by Y and Enter

Before actually starting the service you may want to ensure that you have the code. Here is the code (you may change it to your needs)

Run this - sudo nano /bin/

import socket
import requests
import urllib.request

webhookURL = '<webhook url>'
hostname = socket.gethostname()
external_ipv6 = urllib.request.urlopen('').read().decode('utf8')
external_ipv4 = urllib.request.urlopen('').read().decode('utf8')

data = {
    'username': 'Server IP Webhook',
    'embeds': [{
        'title': 'Server started!',
        'description': hostname + '\n' + 'IPV6' + ': ' + '`' + external_ipv6 + '`'  + '\n\n' + 'IPV4 ' + ':'  + '`' + external_ipv4 + '`',

result =, json = data)

  1. Run sudo systemctl daemon-reload
  2. Run sudo systemctl start discord-webhook.service (if there is an output then your code is functioning successfully!)
  3. Finally sudo systemctl enable discord-webhook.service

And you're good to go! Your script will now run at startup!

Credits - StackOverFlow