shell脚本没有通过crontab运行,手动运行正常

时间:2022-09-04 08:00:03

I have tried exporting my paths and variables and crontab still will not run my script. I'm sure I am doing something wrong.

我已经尝试导出我的路径和变量,而crontab仍然不会运行我的脚本。我确定我做错了什么。

I have a shell script which runs a jar file. This is not working correctly.

我有一个运行jar文件的shell脚本。这不能正常工作。

After reading around I have read this is commonly due to incorrect paths due to cron running via its own shell instance and therefore does not have the same preferences setup as my profile does.

阅读后我读到这通常是由于cron通过自己的shell实例运行而导致的路径不正确,因此没有与我的配置文件相同的首选项设置。

Here is what my script looks like today after several modifications:

以下是经过多次修改后我的脚本今天的样子:

#!/bin/bash --

. /root/.bash_profile

/usr/bin/java -jar Pharmagistics_auto.jar -o

...

those are the most important pieces of the script, the rest are straightforward shell based.

那些是脚本中最重要的部分,其余部分都是基于shell的。

Can someone tell me what I am doing wrong?

有人能告诉我我做错了什么吗?

6 个解决方案

#1


13  

Try specifying the full path to the jar file:

尝试指定jar文件的完整路径:

/usr/bin/java -jar /path/to/Pharmagistics_auto.jar -o

#2


3  

I would just tell you what you have already ruled out: Check your path and environment.

我只想告诉你你已经排除了什么:检查你的路径和环境。

Since you have alredy done this, start debugging. Like write checkpoints into a logfile to see how far your script gets (if even started at all), check the cronjob log file for errors, check your mail (cron sends mails on errors) and so on ...

既然已经完成了这项工作,就开始调试。就像将检查点写入日志文件以查看脚本获取的距离(如果甚至根本没有启动),检查cronjob日志文件是否有错误,检查邮件(cron发送错误邮件)等等......

Not very specific, sorry.

不是很具体,抱歉。

#3


2  

"exporting my paths and variables" won't work since crontab runs in a different shell by a different user.

“导出我的路径和变量”将无法工作,因为crontab由不同的用户在不同的shell中运行。

Also, not sure if this is a typo in how you entered the question, but I see:

此外,不确定这是否是您输入问题的错误,但我看到:

usr/bin/java

...and I can't help but notice you're not specifying the fully qualified path. It's looking for a directory named "usr" in the current working directory. Oft times for crontab, the cwd is undefined, hence your reference goes nowhere.

......我不禁注意到你没有指定完全合格的路径。它正在寻找当前工作目录中名为“usr”的目录。对于crontab来说,cwd是未定义的,因此你的引用无处可去。

Try specifying the full path from root, like so:

尝试从root指定完整路径,如下所示:

/usr/bin/java

Or, if you want to see an example of relative pathing in action, you could also try:

或者,如果您想查看相关路径的示例,您还可以尝试:

cd /

usr/bin/java

#4


1  

A few thoughts.

一些想法。

  1. Remove the -- after the #!/bin/bash
  2. 删除#后面的#!/ bin / bash

  3. Make sure to direct script output seen by cron to mail or somewhere else where you can view it (e.g. MAILTO=desiredUser)
  4. 确保将cron看到的脚本输出定向到邮件或您可以查看的其他位置(例如MAILTO = desiredUser)

  5. Confirm that your script is running and not blocked by a different long-running script (e.g. on the second line, add touch /tmp/MY_SCRIPT_RAN && exit)
  6. 确认您的脚本正在运行且未被其他长时间运行的脚本阻止(例如,在第二行,添加touch / tmp / MY_SCRIPT_RAN &&退出)

  7. Debug the script using set -x and set -v once you know it's actually running
  8. 一旦知道它实际正在运行,就使用set -x调试脚本并设置-v

#5


0  

Do you define necessary paths and env vars in your personal .profile (or other script)? Have you tried sourcing that particular file (or is that what you're doing already with /root/.bash_profile?)

您是否在个人.profile(或其他脚本)中定义了必要的路径和环境变量?您是否尝试过采购该特定文件(或者您正在使用/root/.bash_profile做什么?)

Another way of asking this is: are you certain that whatever necessary paths and env vars you expect are actually available?

另一种问这个问题的方法是:你确定你所期望的任何必要的路径和环境实际可用吗?

If nothing else, have you tried echo'ing individual values or just using the "env" command in your script and then reviewing the stdout?

如果没有别的,您是否尝试过回显单个值或只是在脚本中使用“env”命令然后查看标准输出?

#6


0  

provide full paths to your jar file, and what user are you running the crontab in? If you set it up for a normal user, do you think that user has permission to source the root's profile?

提供jar文件的完整路径,以及运行crontab的用户是什么?如果您为普通用户进行设置,您认为该用户是否有权获取根的配置文件?

#1


13  

Try specifying the full path to the jar file:

尝试指定jar文件的完整路径:

/usr/bin/java -jar /path/to/Pharmagistics_auto.jar -o

#2


3  

I would just tell you what you have already ruled out: Check your path and environment.

我只想告诉你你已经排除了什么:检查你的路径和环境。

Since you have alredy done this, start debugging. Like write checkpoints into a logfile to see how far your script gets (if even started at all), check the cronjob log file for errors, check your mail (cron sends mails on errors) and so on ...

既然已经完成了这项工作,就开始调试。就像将检查点写入日志文件以查看脚本获取的距离(如果甚至根本没有启动),检查cronjob日志文件是否有错误,检查邮件(cron发送错误邮件)等等......

Not very specific, sorry.

不是很具体,抱歉。

#3


2  

"exporting my paths and variables" won't work since crontab runs in a different shell by a different user.

“导出我的路径和变量”将无法工作,因为crontab由不同的用户在不同的shell中运行。

Also, not sure if this is a typo in how you entered the question, but I see:

此外,不确定这是否是您输入问题的错误,但我看到:

usr/bin/java

...and I can't help but notice you're not specifying the fully qualified path. It's looking for a directory named "usr" in the current working directory. Oft times for crontab, the cwd is undefined, hence your reference goes nowhere.

......我不禁注意到你没有指定完全合格的路径。它正在寻找当前工作目录中名为“usr”的目录。对于crontab来说,cwd是未定义的,因此你的引用无处可去。

Try specifying the full path from root, like so:

尝试从root指定完整路径,如下所示:

/usr/bin/java

Or, if you want to see an example of relative pathing in action, you could also try:

或者,如果您想查看相关路径的示例,您还可以尝试:

cd /

usr/bin/java

#4


1  

A few thoughts.

一些想法。

  1. Remove the -- after the #!/bin/bash
  2. 删除#后面的#!/ bin / bash

  3. Make sure to direct script output seen by cron to mail or somewhere else where you can view it (e.g. MAILTO=desiredUser)
  4. 确保将cron看到的脚本输出定向到邮件或您可以查看的其他位置(例如MAILTO = desiredUser)

  5. Confirm that your script is running and not blocked by a different long-running script (e.g. on the second line, add touch /tmp/MY_SCRIPT_RAN && exit)
  6. 确认您的脚本正在运行且未被其他长时间运行的脚本阻止(例如,在第二行,添加touch / tmp / MY_SCRIPT_RAN &&退出)

  7. Debug the script using set -x and set -v once you know it's actually running
  8. 一旦知道它实际正在运行,就使用set -x调试脚本并设置-v

#5


0  

Do you define necessary paths and env vars in your personal .profile (or other script)? Have you tried sourcing that particular file (or is that what you're doing already with /root/.bash_profile?)

您是否在个人.profile(或其他脚本)中定义了必要的路径和环境变量?您是否尝试过采购该特定文件(或者您正在使用/root/.bash_profile做什么?)

Another way of asking this is: are you certain that whatever necessary paths and env vars you expect are actually available?

另一种问这个问题的方法是:你确定你所期望的任何必要的路径和环境实际可用吗?

If nothing else, have you tried echo'ing individual values or just using the "env" command in your script and then reviewing the stdout?

如果没有别的,您是否尝试过回显单个值或只是在脚本中使用“env”命令然后查看标准输出?

#6


0  

provide full paths to your jar file, and what user are you running the crontab in? If you set it up for a normal user, do you think that user has permission to source the root's profile?

提供jar文件的完整路径,以及运行crontab的用户是什么?如果您为普通用户进行设置,您认为该用户是否有权获取根的配置文件?