0

I have set up a systemd.service that starts a bash script when the system boots. The script starts, but it seems that it cannot find the referenced source files but when i start the same script from the terminal i works fine. Al scripts are in one and the same directory.

Anyone have any suggestions to fix this?

systemd.service file:

[Unit]
Description=RIB
After=network-online.target

[Service]
Type=simple
ExecStart=/home/user/release/.scripts/start-script.sh start
Restart=on-failure

[Install]
WantedBy=multi-user.target

part of the start-script.sh

#!/bin/bash

source config.sh
source function.sh


check_connection #function

# GET LOCATION IN .JSON FILE
get_location #function
if [[ $? -ne 0 ]]
then
    printf "%s\n""Can't get location\n\n"
else
    printf "%s\n""Location successfully retrieved\n\n"
fi

source config.sh:

version="2021.01.25"
date=`date '+%Y-%m-%d %H:%M:%S'`
time=`date '+%H:%M:%S'`
temp_path="/home/$USER/temp"
release_path="/home/$USER/release"
info_path="$release_path/to/info"
content_path="$release_path/to/content"
ftp_url="ftp.test.com"

source function.sh:

function check_connection ()
{
    printf "%s\n""${Green}Check connection with $ftp_url.${Color_Off}\n\n"
    while ! timeout 0.2 ping -c 1 -n $ftp_url &> /dev/null
    do
    printf "%s\n""${Green}RIB cannot reach $ftp_url, check WIFI connection.${Color_Off}\n\n"
    sleep 5
    done
    printf "%s\n""${Green}RIB can reach $ftp_url.${Color_Off}\n\n"
}

12
  • printf "%s\n"". - why is there no space? Didn't you mean printf "%s\n" "..."? it seems that it cannot find the referenced source files "it seems"? From where? Do you have an error message? Anyone have any suggestions to fix this? What do you think current working directory of the script is when run from systemd? Commented Jan 8, 2021 at 14:19
  • 1
    Use full paths when referencing the source scripts Commented Jan 8, 2021 at 15:06
  • 1
    Perhaps you could try adding WorkingDirectory=/home/user/release/.scripts/ to your systemd.service file. This sets the working directory for executed processes. Commented Jan 8, 2021 at 15:09
  • Your get_location function isn't defined anywhere. Commented Jan 8, 2021 at 17:29
  • BTW, re: function, see wiki.bash-hackers.org/scripting/obsolete Commented Jan 8, 2021 at 17:30

1 Answer 1

1

the solution is adding WorkingDirectory=/home/user/release/.scripts/ to the service file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.