Starting Greengrass V1 automatically on a Raspberry Pi

Greengrass core on a Raspberry Pi needs to be started manually.

The following script will create a system-D control file and use it to start the service.

The service will then restart automatically at any reboot. Non disclaimer.


Preconditions

  • This script with Greengrass V1 only. Greengrass V2 doesn't need it anymore.
  • The script has been tested on a Raspberry Pi 4B with Debian. The script will most likely work on all distributions using system-D
  • Have Greengrass V1 installed.

Non Disclaimer

  • The scripts needs to be executed with elevate super privileges. This is mandatory. It may however created harm to your system. Check the script upfront. There is no warranty.
  • The script will delete any old versions of the system-D service file /etc/systemd/system/aws-ggc.service
  • The script will need a bash shell and the following commands installed (very likely to be preinstalled):
    • echo, systemctl, if

The Installation Script

Create a file with the name installSystemD.sh:

#!/usr/bin/env bash
# version 1.1
# March, 2021
SERVICEFILE="/etc/systemd/system/aws-ggc.service"
if [[ -e ${SERVICEFILE} ]];then
   echo "AWS Greengrass Service is already installed as SYSTEMD, trying to stop
the service."
   echo "*****";
   systemctl stop aws-ggc.service
   rm ${SERVICEFILE}
fi
echo "[Unit]" >  ${SERVICEFILE};
echo "Description=AWS Greengrass Service" >>  ${SERVICEFILE};
echo "After=syslog.target network.target" >>  ${SERVICEFILE};
echo " " >>  ${SERVICEFILE};
echo "[Service]" >>  ${SERVICEFILE};
echo "Type=forking" >>  ${SERVICEFILE};
echo "User=root" >>  ${SERVICEFILE};
echo "WorkingDirectory=/greengrass/ggc/core" >>  ${SERVICEFILE};
echo "ExecStart=/greengrass/ggc/core/greengrassd start" >>  ${SERVICEFILE};
echo "ExecStop=/greengrass/ggc/core/greengrassd stop" >>  ${SERVICEFILE};
echo "Restart=always" >>  ${SERVICEFILE};
echo "RestartSec=60" >>  ${SERVICEFILE};
echo " " >>  ${SERVICEFILE};
echo "[Install]" >>  ${SERVICEFILE};
echo "WantedBy=multi-user.target" >>  ${SERVICEFILE};
systemctl daemon-reload
systemctl enable aws-ggc.service
systemctl start aws-ggc.service
echo "Done installing prerequisites (SYSTEMD)"

Installation of the Service

Make the file executable:

$ chmod u+x installSystemD.sh

Execute the file with super user privileges:

$ sudo ./installSystemD.sh

Check for Success

This command checks whether the service started. It doesn't check whether Greengrass Core is working!
Execute the following command:

pi@raspberrypi:~ $ systemctl status aws-ggc.service
● aws-ggc.service
   Loaded: loaded (/etc/systemd/system/aws-ggc.service; enabled; vendor preset:
   Active: active (running) since Mon 2020-11-23 11:53:08 CET; 30min ago
  Process: 396 ExecStart=/greengrass/ggc/core/greengrassd start (code=exited, st
 Main PID: 452 (5)
    Tasks: 132 (limit: 4915)
   Memory: 236.8M
   CGroup: /system.slice/aws-ggc.service
           ├─ 452 /greengrass/ggc/packages/1.11.0/bin/daemon -core-dir /greengra
           ├─ 634 /lambda/greengrassSystemComponents -runAs=tes
           ├─ 639 /lambda/greengrassSystemComponents -runAs=shadow
           ├─ 647 /lambda/greengrassSystemComponents -runAs=cloudSpooler
           ├─ 665 /lambda/greengrassSystemComponents -runAs=connectionManager
           ├─ 679 /lambda/greengrassSystemComponents -runAs=deviceCertificateMan
           ├─ 692 /lambda/greengrassSystemComponents -runAs=shadowSync
           ├─ 694 /lambda/greengrassSystemComponents -runAs=secretManager
           ├─ 772 /lambda/ipdetector
           ├─ 773 java -cp /lambda/stream_manager/AWSGreengrassStreamManager.jar
           └─1209 python3.7 -u /runtime/python/lambda_runtime.py --handler=green

Nov 23 11:52:54 raspberrypi systemd[1]: Starting aws-ggc.service...
Nov 23 11:52:54 raspberrypi greengrassd[396]: Setting up greengrass daemon
Nov 23 11:52:54 raspberrypi greengrassd[396]: Validating hardlink/softlink prote
Nov 23 11:52:54 raspberrypi greengrassd[396]: Waiting for up to 1m10s for Daemon
Nov 23 11:53:08 raspberrypi greengrassd[396]: Greengrass successfully started wi
Nov 23 11:53:08 raspberrypi systemd[1]: Started aws-ggc.service.

Abort with <ctrl> c

Stopping the Service

$ sudo systemctl stop aws-ggc.service