I am unable to run a crontab job , under a different user.(For e.g sudo -u someuser crontab -e) It runs absolutely fine under my user profile. I know what is the issue, but cannot find the resolution for it. The issue is , when i configure this crontab job for other user, crontab is not able to find java ,as a result even simple java -version is not working.
我无法在不同的用户下运行crontab作业。(例如sudo -u someuser crontab -e)它在我的用户配置文件下运行绝对正常。我知道这是什么问题,但无法找到它的解决方案。问题是,当我为其他用户配置这个crontab作业时,crontab无法找到java,因此即使简单的java -version也无法正常工作。
Below is my script.
以下是我的脚本。
#!/bin/bash
export JAVA_HOME=/usr/jdk/jdk1.6.0_31
export PATH=/usr/local/bin:/bin:/usr/bin:/opt/dell/srvadmin/bin:/home/VishalS/bin
echo "JAVA_HOME is: " $JAVA_HOME >> log.out
echo "PATH is: " $PATH >> log.out
which java >> log.out
/usr/bin/java -version >> log.out
/usr/jdk/jdk1.6.0_31/bin/java -version >> log.out
output of above script :
上述脚本的输出:
JAVA_HOME is: /usr/jdk/jdk1.6.0_31
PATH is: /usr/local/bin:/bin:/usr/bin:/opt/dell/srvadmin/bin:/home/VishalS/bin
/usr/bin/java
so obviously, the below lines did not work.
很明显,下面的行不起作用。
/usr/bin/java -version >> log.out
/usr/jdk/jdk1.6.0_31/bin/java -version >> log.out
Could somebody please help me here? I do not understand why even after setting jdk path crontab does not executes java -version ?
有人可以帮我吗?我不明白为什么即使设置jdk路径后crontab也不执行java -version?
4 个解决方案
#1
1
Try setting the paths in the other users crontab directly. See 'man 5 crontab'.
尝试直接在其他用户crontab中设置路径。见'man 5 crontab'。
#2
0
The only that comes to my mind is that, the java
command might not have the Executable permission to the user you are trying to execute it from.
我唯一想到的是,java命令可能没有对您尝试执行它的用户的可执行权限。
So use chmod
to give necessary permissions to execute.
因此,使用chmod为执行提供必要的权限。
#3
0
Crontab(5) runs without ENV, so you need to source an environment (you are building the JAVA_HOME and PATH, but crontab gives you (almost) nothing. notice that the output of "which java" did not appear in your log file.
Crontab(5)在没有ENV的情况下运行,所以你需要获取一个环境(你正在构建JAVA_HOME和PATH,但是crontab几乎没有给你任何东西。请注意,“java java”的输出没有出现在你的日志文件中。
- build an environment script you can source for your crontab script(s), ". path/to/env.sh"
- 构建一个可以为crontab脚本提供源代码的环境脚本,“。path / to / env.sh”
- Put full paths on all programs executed in shell scripts etc. You cannot rely upon environment in cron
- 在shell脚本等中执行所有程序的完整路径。您不能依赖于cron中的环境
- run "which which", did you get /usr/bin/which? then put that in your script.
- 运行“哪个”,你得到/ usr / bin /哪个?然后把它放在你的脚本中。
-
we often omit paths from scripts for convenience, but give paths in scripts run from crontab
为方便起见,我们经常从脚本中省略路径,但是从crontab运行脚本中的路径
-
does the java run when logged in as the other user?
当以其他用户身份登录时,java是否运行?
#4
0
Thanks everyone for your helpful comments. However the actual fix which worked in my-case , was mix of steps as mentioned below :- 1. Setup Sun JDK path under root's user profile.(earlier open jdk was setup) 2. Gave permission to logs folder where logs were being written.(earlier the permission were not correctly set) 3. Tweaked my cronjob(i think there was an extra space there)
感谢大家的有益评论。然而,在我的情况下工作的实际修复是下面提到的步骤混合: - 1.在root用户配置文件下设置Sun JDK路径。(早期打开jdk已设置)2。授予日志文件夹的权限。(早先的权限没有正确设置)3。调整我的cronjob(我认为那里有一个额外的空间)
#1
1
Try setting the paths in the other users crontab directly. See 'man 5 crontab'.
尝试直接在其他用户crontab中设置路径。见'man 5 crontab'。
#2
0
The only that comes to my mind is that, the java
command might not have the Executable permission to the user you are trying to execute it from.
我唯一想到的是,java命令可能没有对您尝试执行它的用户的可执行权限。
So use chmod
to give necessary permissions to execute.
因此,使用chmod为执行提供必要的权限。
#3
0
Crontab(5) runs without ENV, so you need to source an environment (you are building the JAVA_HOME and PATH, but crontab gives you (almost) nothing. notice that the output of "which java" did not appear in your log file.
Crontab(5)在没有ENV的情况下运行,所以你需要获取一个环境(你正在构建JAVA_HOME和PATH,但是crontab几乎没有给你任何东西。请注意,“java java”的输出没有出现在你的日志文件中。
- build an environment script you can source for your crontab script(s), ". path/to/env.sh"
- 构建一个可以为crontab脚本提供源代码的环境脚本,“。path / to / env.sh”
- Put full paths on all programs executed in shell scripts etc. You cannot rely upon environment in cron
- 在shell脚本等中执行所有程序的完整路径。您不能依赖于cron中的环境
- run "which which", did you get /usr/bin/which? then put that in your script.
- 运行“哪个”,你得到/ usr / bin /哪个?然后把它放在你的脚本中。
-
we often omit paths from scripts for convenience, but give paths in scripts run from crontab
为方便起见,我们经常从脚本中省略路径,但是从crontab运行脚本中的路径
-
does the java run when logged in as the other user?
当以其他用户身份登录时,java是否运行?
#4
0
Thanks everyone for your helpful comments. However the actual fix which worked in my-case , was mix of steps as mentioned below :- 1. Setup Sun JDK path under root's user profile.(earlier open jdk was setup) 2. Gave permission to logs folder where logs were being written.(earlier the permission were not correctly set) 3. Tweaked my cronjob(i think there was an extra space there)
感谢大家的有益评论。然而,在我的情况下工作的实际修复是下面提到的步骤混合: - 1.在root用户配置文件下设置Sun JDK路径。(早期打开jdk已设置)2。授予日志文件夹的权限。(早先的权限没有正确设置)3。调整我的cronjob(我认为那里有一个额外的空间)