Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Systemd startup #295

Merged
merged 9 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ build-1284/*
.vscode
.pio
*.o
!build.sh
!startOpenSprinkler.sh
!updater.sh
150 changes: 0 additions & 150 deletions OpenSprinkler.launch

This file was deleted.

15 changes: 15 additions & 0 deletions OpenSprinkler.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[Unit]
Description=OpenSprinkler
Wants=network-online.target
After=network.target network-online.target

[Service]
Type=simple
Restart=always
RestartSec=1
User=root
WorkingDirectory=__OpenSprinkler_Path__
ExecStart=/bin/bash -c ./startOpenSprinkler.sh

[Install]
WantedBy=multi-user.target
32 changes: 18 additions & 14 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,14 @@ else
g++ -o OpenSprinkler -DOSPI $USEGPIO -DSMTP_OPENSSL -std=c++14 -include string.h main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp mqtt.cpp smtp.c -Iexternal/TinyWebsockets/tiny_websockets_lib/include $ws -Iexternal/OpenThings-Framework-Firmware-Library/ $otf -lpthread -lmosquitto -lssl -lcrypto $GPIOLIB
fi

if [ ! "$SILENT" = true ] && [ -f OpenSprinkler.launch ] && [ ! -f /etc/init.d/OpenSprinkler.sh ]; then
if [ -f /etc/init.d/OpenSprinkler.sh ]; then
echo "Detected the only init.d start up script, removing."
echo "If you still want OpenSprinkler to launch on startup make sure when you run the build script to answer \"Y\" to the following question."
/etc/init.d/OpenSprinkler.sh stop
rm /etc/init.d/OpenSprinkler.sh
fi

if [ ! "$SILENT" = true ] && [ -f OpenSprinkler.service ] && [ -f startOpenSprinkler.sh ] && [ ! -f /etc/systemd/system/OpenSprinkler.service ]; then

read -p "Do you want to start OpenSprinkler on startup? " -n 1 -r
echo
Expand All @@ -72,28 +79,25 @@ if [ ! "$SILENT" = true ] && [ -f OpenSprinkler.launch ] && [ ! -f /etc/init.d/O
exit 0
fi

echo "Adding OpenSprinkler launch script..."
echo "Adding OpenSprinkler launch service..."

# Get current directory (binary location)
pushd `dirname $0` > /dev/null
DIR=`pwd`
pushd "$(dirname $0)" > /dev/null
DIR="$(pwd)"
popd > /dev/null

# Update binary location in start up script
sed -e 's,\_\_OpenSprinkler\_Path\_\_,'"$DIR"',g' OpenSprinkler.launch > OpenSprinkler.sh
sed -e 's,\_\_OpenSprinkler\_Path\_\_,'"$DIR"',g' OpenSprinkler.service > /etc/systemd/system/OpenSprinkler.service

# Make file executable
chmod +x OpenSprinkler.sh

# Move start up script to init.d directory
sudo mv OpenSprinkler.sh /etc/init.d/

# Add to auto-launch on system startup
sudo update-rc.d OpenSprinkler.sh defaults
chmod +x startOpenSprinkler.sh

# Start the deamon now
sudo /etc/init.d/OpenSprinkler.sh start
# Reload systemd
systemctl daemon-reload

# Enable and start the service
systemctl enable OpenSprinkler
systemctl start OpenSprinkler
fi

echo "Done!"
3 changes: 3 additions & 0 deletions startOpenSprinkler.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
cd "$(dirname "$0")"
./OpenSprinkler
2 changes: 1 addition & 1 deletion updater.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

git pull
./build.sh -s ospi
/etc/init.d/OpenSprinkler.sh restart
systemctl restart OpenSprinkler.service