【问题标题】:bash script can't find source file in script running in systemdbash脚本在systemd中运行的脚本中找不到源文件
【发布时间】:2021-04-14 06:00:50
【问题描述】:

我设置了一个 systemd.service,它在系统启动时启动一个 bash 脚本。脚本启动,但似乎找不到引用的源文件,但是当我从终端启动相同的脚本时,我工作正常。所有脚本都在同一个目录中。

有人有解决此问题的建议吗?

systemd.service 文件:

[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

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

源配置.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"

源函数.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"
}

【问题讨论】:

  • printf "%s\n"". - 为什么没有空间?你不是说printf "%s\n" "..."吗? it seems that it cannot find the referenced source files“看起来”?从哪里?你有错误信息吗? Anyone have any suggestions to fix this?从 systemd 运行时,您认为脚本的当前工作目录是什么?
  • 引用源脚本时使用完整路径
  • 也许您可以尝试将WorkingDirectory=/home/user/release/.scripts/ 添加到您的systemd.service 文件中。这将为执行的进程设置工作目录。
  • 您的 get_location 函数未在任何地方定义。
  • 顺便说一句,回复:function,见wiki.bash-hackers.org/scripting/obsolete

标签: bash shell ubuntu startup systemd


【解决方案1】:

解决方案是将WorkingDirectory=/home/user/release/.scripts/ 添加到服务文件中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    相关资源
    最近更新 更多