Allow running service under root

This commit is contained in:
suchmememanyskill
2025-01-28 21:50:21 +01:00
parent 9bfbb1cb0e
commit d616ef5a6d
2 changed files with 43 additions and 24 deletions

View File

@@ -1,9 +1,16 @@
#!/bin/bash
if [ "$EUID" -eq 0 ]; then
echo "Please do not run as root"
read -r -p "Are you sure you want to run this service as root? [y/N] " response
response=${response,,} # tolower
if ! [[ "$response" =~ ^(yes|y)$ ]]; then
exit
fi
SERVICE_PATH="/etc/systemd/system/cyd-klipper-serial.service"
else
mkdir -p ~/.config/systemd/user
SERVICE_PATH="~/.config/systemd/user/cyd-klipper-serial.service"
fi
set -e
@@ -15,20 +22,26 @@ source ./env/bin/activate
pip3 install -r requirements.txt
# Create systemd unit file
mkdir -p ~/.config/systemd/user
echo "[Unit]" > ~/.config/systemd/user/cyd-klipper-serial.service
echo "Description=CYD Klipper serial server" >> ~/.config/systemd/user/cyd-klipper-serial.service
echo "After=network.target" >> ~/.config/systemd/user/cyd-klipper-serial.service
echo "" >> ~/.config/systemd/user/cyd-klipper-serial.service
echo "[Service]" >> ~/.config/systemd/user/cyd-klipper-serial.service
echo "ExecStart=$(pwd)/run.sh" >> ~/.config/systemd/user/cyd-klipper-serial.service
echo "WorkingDirectory=$(pwd)" >> ~/.config/systemd/user/cyd-klipper-serial.service
echo "Restart=always" >> ~/.config/systemd/user/cyd-klipper-serial.service
echo "" >> ~/.config/systemd/user/cyd-klipper-serial.service
echo "[Install]" >> ~/.config/systemd/user/cyd-klipper-serial.service
echo "WantedBy=multi-user.target" >> ~/.config/systemd/user/cyd-klipper-serial.service
echo "[Unit]" > $SERVICE_PATH
echo "Description=CYD Klipper serial server" >> $SERVICE_PATH
echo "After=network.target" >> $SERVICE_PATH
echo "" >> $SERVICE_PATH
echo "[Service]" >> $SERVICE_PATH
echo "ExecStart=$(pwd)/run.sh" >> $SERVICE_PATH
echo "WorkingDirectory=$(pwd)" >> $SERVICE_PATH
echo "Restart=always" >> $SERVICE_PATH
echo "" >> $SERVICE_PATH
echo "[Install]" >> $SERVICE_PATH
echo "WantedBy=multi-user.target" >> $SERVICE_PATH
# Start the service
if [ "$EUID" -eq 0 ]; then
systemctl daemon-reload
systemctl enable cyd-klipper-serial
systemctl start cyd-klipper-serial
else
systemctl --user daemon-reload
systemctl --user enable cyd-klipper-serial
systemctl --user start cyd-klipper-serial
fi

View File

@@ -1,13 +1,19 @@
#!/bin/bash
if [ "$EUID" -eq 0 ]; then
echo "Please do not run as root"
exit
fi
set -e
if [ "$EUID" -eq 0 ]; then
systemctl stop cyd-klipper-serial
systemctl disable cyd-klipper-serial
rm /etc/systemd/system/cyd-klipper-serial.service
systemctl daemon-reload
else
systemctl --user stop cyd-klipper-serial
systemctl --user disable cyd-klipper-serial
rm ~/.config/systemd/user/cyd-klipper-serial.service
systemctl --user daemon-reload
fi