From 1f647415aee860843e20c618cd876ab990855aba Mon Sep 17 00:00:00 2001 From: bitSheriff Date: Sun, 17 Sep 2023 17:01:03 +0200 Subject: [PATCH 1/6] added script to a service --- autostart_shelfslide.sh | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/autostart_shelfslide.sh b/autostart_shelfslide.sh index e467ed6..d0ba875 100755 --- a/autostart_shelfslide.sh +++ b/autostart_shelfslide.sh @@ -1,4 +1,38 @@ #!/bin/bash -# copy this file to "~/.config/autostart/" -python3 ~/ShelfSlide/shelfslide.py +# Path to the Python program +PROGRAM_PATH="$HOME/ShelfSlide/shelfslide.py" + +# Name of the systemd service +SERVICE_NAME="shelfslide" + +# Get the username of the person running the script +CURRENT_USER="$USER" + +# Create the unit file +cat < /etc/systemd/system/${SERVICE_NAME}.service +[Unit] +Description=ShelfSlide Python Program +After=network.target + +[Service] +ExecStart=/usr/bin/python3 ${PROGRAM_PATH} +WorkingDirectory=$HOME/ShelfSlide +StandardOutput=inherit +StandardError=inherit +Restart=always +User=$CURRENT_USER + +[Install] +WantedBy=multi-user.target +EOF + +# Reload systemd +systemctl daemon-reload + +# Enable and start the service +systemctl enable ${SERVICE_NAME} +systemctl start ${SERVICE_NAME} + +# Display the status of the service +systemctl status ${SERVICE_NAME} From 1efe1a793f7400262dfadd623340a8f75551b885 Mon Sep 17 00:00:00 2001 From: bitSheriff Date: Sun, 17 Sep 2023 17:01:53 +0200 Subject: [PATCH 2/6] added additional check if already exists --- autostart_shelfslide.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/autostart_shelfslide.sh b/autostart_shelfslide.sh index d0ba875..a01122f 100755 --- a/autostart_shelfslide.sh +++ b/autostart_shelfslide.sh @@ -9,6 +9,12 @@ SERVICE_NAME="shelfslide" # Get the username of the person running the script CURRENT_USER="$USER" +# Check if the service already exists +if systemctl is-active --quiet ${SERVICE_NAME}.service; then + echo "The service ${SERVICE_NAME} already exists." + exit 1 +fi + # Create the unit file cat < /etc/systemd/system/${SERVICE_NAME}.service [Unit] From 2cd48a47b4e3b6ce12e959c4faf44771de28ab1e Mon Sep 17 00:00:00 2001 From: bitSheriff Date: Sun, 17 Sep 2023 17:08:47 +0200 Subject: [PATCH 3/6] user service not root service --- autostart_shelfslide.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autostart_shelfslide.sh b/autostart_shelfslide.sh index a01122f..30c1abe 100755 --- a/autostart_shelfslide.sh +++ b/autostart_shelfslide.sh @@ -16,7 +16,7 @@ if systemctl is-active --quiet ${SERVICE_NAME}.service; then fi # Create the unit file -cat < /etc/systemd/system/${SERVICE_NAME}.service +cat < ~/.config/systemd/user/${SERVICE_NAME}.service [Unit] Description=ShelfSlide Python Program After=network.target From 9569fa1d43dda44a91e447adf3423431304a2299 Mon Sep 17 00:00:00 2001 From: bitSheriff Date: Sun, 17 Sep 2023 17:12:19 +0200 Subject: [PATCH 4/6] create folder id not exists --- autostart_shelfslide.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/autostart_shelfslide.sh b/autostart_shelfslide.sh index 30c1abe..4272f49 100755 --- a/autostart_shelfslide.sh +++ b/autostart_shelfslide.sh @@ -15,6 +15,12 @@ if systemctl is-active --quiet ${SERVICE_NAME}.service; then exit 1 fi +# Vcreate config folder structure if it doesn't exist +CONFIG_DIR="$HOME/.config/systemd/user" +if [ ! -d "$CONFIG_DIR" ]; then + mkdir -p "$CONFIG_DIR" +fi + # Create the unit file cat < ~/.config/systemd/user/${SERVICE_NAME}.service [Unit] From c1619eaf367a44c48d1dd5f349ab7cda4635b07a Mon Sep 17 00:00:00 2001 From: bitSheriff Date: Sun, 17 Sep 2023 16:30:42 +0100 Subject: [PATCH 5/6] added fix for relative includes --- shelfslide.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/shelfslide.py b/shelfslide.py index 9c055a3..a1dd748 100644 --- a/shelfslide.py +++ b/shelfslide.py @@ -260,4 +260,5 @@ def main(): ## # @brief Main function if __name__ == "__main__": - main() \ No newline at end of file + os.chdir(os.path.dirname(os.path.abspath(__file__))) + main() From 63b15eb98f798d779b484b6d33e4ca4b472bde2e Mon Sep 17 00:00:00 2001 From: bitSheriff Date: Tue, 26 Sep 2023 07:51:24 +0200 Subject: [PATCH 6/6] fixed systemd path (now execute as system not user) --- autostart_shelfslide.sh | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/autostart_shelfslide.sh b/autostart_shelfslide.sh index 4272f49..a3a1821 100755 --- a/autostart_shelfslide.sh +++ b/autostart_shelfslide.sh @@ -15,25 +15,22 @@ if systemctl is-active --quiet ${SERVICE_NAME}.service; then exit 1 fi -# Vcreate config folder structure if it doesn't exist -CONFIG_DIR="$HOME/.config/systemd/user" -if [ ! -d "$CONFIG_DIR" ]; then - mkdir -p "$CONFIG_DIR" +# Check if the script is executed with root privileges +if [ "$EUID" -ne 0 ]; then + echo "This script requires root privileges. Please run it as root." + exit 1 fi # Create the unit file -cat < ~/.config/systemd/user/${SERVICE_NAME}.service +cat < /etc/systemd/system/${SERVICE_NAME}.service [Unit] Description=ShelfSlide Python Program -After=network.target [Service] ExecStart=/usr/bin/python3 ${PROGRAM_PATH} WorkingDirectory=$HOME/ShelfSlide -StandardOutput=inherit -StandardError=inherit -Restart=always User=$CURRENT_USER +Group=$CURRENT_USER [Install] WantedBy=multi-user.target @@ -43,8 +40,8 @@ EOF systemctl daemon-reload # Enable and start the service -systemctl enable ${SERVICE_NAME} -systemctl start ${SERVICE_NAME} +systemctl enable ${SERVICE_NAME}.service +systemctl start ${SERVICE_NAME}.service # Display the status of the service -systemctl status ${SERVICE_NAME} +systemctl status ${SERVICE_NAME}.service