[英]Synology Scheduler .sh java command not found
I have a bash script thats only task is to execute a jar file.我有一个 bash 脚本,唯一的任务是执行 jar 文件。
sms.sh短信
java -jar /volume1/homes/jar/smssender.jar
Using my Synology NAS I set up a task.我使用 Synology NAS 设置了一项任务。
Adding the command to execute bash script.添加执行bash脚本的命令。 Adding log output.
添加日志输出。
Executing my new Task.执行我的新任务。
Checking the log to see the following error:查看日志,看到如下错误:
/volume1/homes/jar/sms.sh: line 1: java: command not found
/volume1/homes/jar/sms.sh: line 1: java: command not found
Checking Java version/installation:检查 Java 版本/安装:
Checking execution of sh script manually (working):手动检查 sh 脚本的执行(工作):
Anyone with this same strange case?有同样奇怪案例的人吗? Any workarounds/ideas?
任何解决方法/想法?
I tried我试过
but none worked.但没有一个工作。
When the Synology task scheduler executes the script sms.sh
the PATH setting is taken from script /etc/crontab
.当 Synology 任务调度程序执行脚本
sms.sh
,PATH 设置取自脚本/etc/crontab
。 Which does not contain the Java path.其中不包含 Java 路径。
The default login shell environment is defined int /etc/profile
.默认登录 shell 环境定义为 int
/etc/profile
。 At the end there is a section to add the Java path.最后有一个部分来添加 Java 路径。
PATH=$PATH:/var/packages/Java8/target/j2sdk-image/bin # Synology Java runtime enviroment
PATH=$PATH:/var/packages/Java8/target/j2sdk-image/jre/bin # Synology Java runtime enviroment
JAVA_HOME=/var/packages/Java8/target/j2sdk-image/jre # Synology Java runtime enviroment
CLASSPATH=.:/var/packages/Java8/target/j2sdk-image/jre/lib # Synology Java runtime enviroment
LANG=en_US.utf8 # Synology Java runtime enviroment
export CLASSPATH PATH JAVA_HOME LANG # Synology Java runtime enviroment
As already stated in already given comments sourcing a profile script which is meant for an interactive shell isn't suggested.正如已经给出的评论中所述,不建议采购用于交互式 shell 的配置文件脚本。 You might mimic the behavior of the
/etc/profile
script in your sms.sh
script to set CLASSPATH PATH JAVA_HOME LANG.您可以在
sms.sh
脚本中模仿/etc/profile
脚本的行为来设置 CLASSPATH PATH JAVA_HOME LANG。
The raised points about hardcoding the path in your script and the resulting reduced portability might have a lover precedence in this specific case.在这种特定情况下,关于在脚本中硬编码路径以及由此导致的可移植性降低的提出的观点可能具有情人优先权。
I'm not familiar with Synology
so fwiw ...我不熟悉
Synology
所以fwiw ...
The shell script works when executed at the command line because the particular login session has already loaded a set of environment variables (eg, upon logging in the .profile/.bashrc
script(s) in the home directory is sourced and the various java-specific environment variables are loaded - PATH, JAVA_HOME, CLASSPATH
, etc) that allow java
and the script to run without issue. shell 脚本在命令行执行时可以工作,因为特定的登录会话已经加载了一组环境变量(例如,在登录主目录中的
.profile/.bashrc
脚本时,以及各种 java-加载了特定的环境变量 - PATH, JAVA_HOME, CLASSPATH
等),允许java
和脚本毫无问题地运行。
The failed Synology
job error indicates that the java-specific environment variables have not been loaded and therefore the job/script is unable to locate java
. Synology
作业失败错误表示尚未加载特定于 java 的环境变量,因此作业/脚本无法定位java
。
Assuming Synology
does not have a configuration setting/flag that stipulates to pre-load a login's profile, the 'easy' solution would be to edit the script ( sms.sh
) and have it source the appropriate resource file before performing any operations (eg, calling java
).假设
Synology
没有规定预加载登录配置文件的配置设置/标志,“简单”的解决方案是编辑脚本( sms.sh
)并在执行任何操作(例如, 调用java
)。 A simple example:一个简单的例子:
$cat sms.sh
#!/usr/bin/bash
. ~root/.bashrc # load the root account profile before continuing ...
java ...
NOTES :注意事项:
root
with the name of the login under which the script is to be run (in the example Synology
images it appears you've chosen the root
user hence my example references ~root
)root
替换为运行脚本的登录名(在示例Synology
图像中,您似乎选择了root
用户,因此我的示例引用了~root
)~root/.bashrc
with the path to user's profile in order to pre-load the environment variables needed to allow the script to find java
~root/.bashrc
替换为用户配置文件的路径,以便预加载允许脚本查找java
所需的环境变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.