我有一个运行 bash.sh 文件的 cron 作业。如果我在终端中运行该文件,它工作正常。但当我尝试将其作为 cron 作业运行时,发生了一些事情,我不知道是什么。Cron 作业如下所示:
*/5 * * * * bash /home/vlado/custom-scripts/selenium-server/run-selenium-hub-and-nodes.sh 2>> /var/log/cron/selenium-server.log
run-selenium-hub-and-nodes.sh 在这里
#!/bin/bash
cd /opt/selenium-server/
# -c returns number of lines in grep result
countHub=$(ps -x | grep -v "grep" | grep "selenium-server" | grep "role hub" -c)
countNodes=$(ps -x | grep -v "grep" | grep "selenium-server" | grep "role node" -c)
# if selenium server HUB is in ps -x result
if [ $countHub -eq 1 ]
then
# if there is NO NODE
if [ $countNodes -eq 0 ]
then
# start two new nodes
java -jar selenium-server-standalone-3.141.59.jar -role node -hub http://173.249.58.30:4444/grid/register/ &
java -jar selenium-server-standalone-3.141.59.jar -role node -hub http://173.249.58.30:4444/grid/register/ &
exit 0
# if there is only ONE NODE
elif [ $countNodes -eq 1 ]
then
# start one new node
java -jar selenium-server-standalone-3.141.59.jar -role node -hub http://173.249.58.30:4444/grid/register/ &
exit 0
fi
else # if there is NO HUB start new hub and two new nodes
# kill all possible hubs and nodes for sure
pkill -9 -f "selenium-server"
# Start new hub
java -jar selenium-server-standalone-3.141.59.jar -role hub &
java -jar selenium-server-standalone-3.141.59.jar -role node -hub http://173.249.58.30:4444/grid/register/ &
java -jar selenium-server-standalone-3.141.59.jar -role node -hub http://173.249.58.30:4444/grid/register/ &
exit 0
fi
我可以在 syslog 中看到脚本正在运行并尝试发送电子邮件。我为此安装了 postfix,但我看不到该消息。syslog 文件中没有更多信息。还有其他日志可以收集有关 cron 作业的更多信息吗?几个小时后,我决定提出这个问题。谢谢帮助。
答案1
简单的解决方案是使用 java 的完整路径来使其工作。正如评论中提到的那样,用户设置中没有任何东西被带入 crontab。您需要自己手动设置这些内容。