Cron不会执行ruby脚本

时间:2022-08-30 23:59:08

I have a cron job set to execute a shell script telling the server to run a single ruby command (jekyll build --source /path/to/source/dir --destination /path/to/dest/dir) 2 minutes past every hour. The script executes just fine when I run it via the terminal, but cron doesn't seem to fire it. I know cron's environment isn't the same as the user's and I've set its source path to my .bash_profile, where the user ruby environment is defined, as per advice elsewhere. I'm rather at a loss now.

我有一个cron作业集来执行一个shell脚本,告诉服务器运行一个ruby命令(jekyll构建——源/路径/到/源/目录/目录/路径/最大/目录)每小时2分钟。当我通过终端运行脚本时,它执行得很好,但是cron似乎没有启动它。我知道cron的环境与用户的环境不一样,我已经将它的源路径设置为.bash_profile,在这里,用户ruby环境被定义,就像其他地方的建议一样。我现在有点不知所措。

The crontab entry looks like this:

crontab条目如下所示:

2 * * * * . $HOME/.bash_profile ~/jek.sh

2 * * * * *。$ HOME /。bash_profile ~ / jek.sh

FWIW, the relevant section of .bash_profile, set up automatically when RVM was installed (on shared hosting, with somewhat outdated ruby no less, getting Jekyll up and running without being able to write to the server's own ruby directory means using RVM; this seems to work absolutely fine), is:

FWIW是.bash_profile的相关部分,它是在RVM安装时自动设置的(在共享主机上,使用过时的ruby),让Jekyll启动并运行,而无法写入服务器自己的ruby目录意味着使用RVM;这似乎非常有效),是:

PATH=$PATH:$HOME/bin

export PATH

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

Am I calling my user profile for use in the cron environment wrongly? Am I missing something glaringly obvious in the syntax? Any help would be much appreciated.

我在cron环境中调用我的用户配置文件是错误的吗?我是否遗漏了语法中显而易见的东西?如有任何帮助,我们将不胜感激。

3 个解决方案

#1


1  

In your question, the cron you are running goes like

在你的问题中,你正在运行的cron是这样的

2 * * * * . $HOME/.bash_profile ~/jek.sh

There are multiple things you need to correct/verify with this entry

有很多东西你需要更正/验证这个条目

  1. Check that file permissions for both is 777 or 775 (rwx). If not, then change the file permission using chmod 777 ~/jek.sh
  2. 检查这两个文件的文件权限是777或775 (rwx)。如果没有,则使用chmod 777 ~/jek.sh更改文件权限。
  3. Check that each of them define in their shebang line which language script they are (#!/usr/local/env sh)
  4. 检查他们每个人在他们的shebang行中定义了他们是哪个语言脚本(#!/usr/local/env sh)
  5. Separate the two scripts by an && or ; so that both of them are run properly. Currently, the second script's name will be treated as a parameter for the first.
  6. 用&&或将两个脚本分开;这样它们都能正常运行。目前,第二个脚本的名称将作为第一个脚本的参数。
  7. There is a . after the 2 * * * * part. I am not sure why you added it - it has to be removed.
  8. 有一个。在2 * * * * *部分之后。我不知道你为什么添加它——它必须被删除。


In case @psny's answer doesn't work for you, try exporting your path variable in your cron entry. After that, the whole thing should work properly. Steps

如果@psny的答案对您不起作用,请尝试在cron条目中导出路径变量。在那之后,一切都应该正常工作。步骤

1) Find the value of $PATH

1)找到$PATH的值。

echo $PATH #Lets call the string :some/path/:another/path

2) Manually set the path in your crontab entry

2)手动设置crontab条目中的路径

2 * * * * export PATH=:some/path/:another/path && /bin/bash /home/username/jek.sh

#2


3  

What do you think $HOME will evaluate to in cron's environment?

你认为$HOME在cron的环境中会评估什么?

Since you are using rvm. This is the correct way to run cron jobs

因为您正在使用rvm。这是运行cron作业的正确方式

#3


1  

You may need to use absolute paths because cron can be very picky.

您可能需要使用绝对路径,因为cron可能非常挑剔。

Try this after 'crontab -e':

在“crontab -e”之后试试这个:

2 * * * * /bin/bash /home/username/jek.sh

If you want to string commands together, use && between each:

如果您想将命令串在一起,请使用&&在每个命令之间:

2 * * * * /bin/bash /home/user_name/.bash_profile && /bin/bash /home/username/jek.sh

As a note, besides /bin/ you can find many commands in /usr/bin. To find the absolute location of a command, use 'which'. For example, :

作为一个说明,除了/bin/您可以在/usr/bin中找到许多命令。要找到命令的绝对位置,请使用“which”。例如,:

user@computer:~$ which bash
 /bin/bash

Finally, you can probably use cron to run the ruby script directly, with an absolute path to jekyll, and probably after && in order to set up the environment first.

最后,您可能可以使用cron直接运行ruby脚本,并具有绝对的jekyll路径,可能在&&之后,以便首先设置环境。

#1


1  

In your question, the cron you are running goes like

在你的问题中,你正在运行的cron是这样的

2 * * * * . $HOME/.bash_profile ~/jek.sh

There are multiple things you need to correct/verify with this entry

有很多东西你需要更正/验证这个条目

  1. Check that file permissions for both is 777 or 775 (rwx). If not, then change the file permission using chmod 777 ~/jek.sh
  2. 检查这两个文件的文件权限是777或775 (rwx)。如果没有,则使用chmod 777 ~/jek.sh更改文件权限。
  3. Check that each of them define in their shebang line which language script they are (#!/usr/local/env sh)
  4. 检查他们每个人在他们的shebang行中定义了他们是哪个语言脚本(#!/usr/local/env sh)
  5. Separate the two scripts by an && or ; so that both of them are run properly. Currently, the second script's name will be treated as a parameter for the first.
  6. 用&&或将两个脚本分开;这样它们都能正常运行。目前,第二个脚本的名称将作为第一个脚本的参数。
  7. There is a . after the 2 * * * * part. I am not sure why you added it - it has to be removed.
  8. 有一个。在2 * * * * *部分之后。我不知道你为什么添加它——它必须被删除。


In case @psny's answer doesn't work for you, try exporting your path variable in your cron entry. After that, the whole thing should work properly. Steps

如果@psny的答案对您不起作用,请尝试在cron条目中导出路径变量。在那之后,一切都应该正常工作。步骤

1) Find the value of $PATH

1)找到$PATH的值。

echo $PATH #Lets call the string :some/path/:another/path

2) Manually set the path in your crontab entry

2)手动设置crontab条目中的路径

2 * * * * export PATH=:some/path/:another/path && /bin/bash /home/username/jek.sh

#2


3  

What do you think $HOME will evaluate to in cron's environment?

你认为$HOME在cron的环境中会评估什么?

Since you are using rvm. This is the correct way to run cron jobs

因为您正在使用rvm。这是运行cron作业的正确方式

#3


1  

You may need to use absolute paths because cron can be very picky.

您可能需要使用绝对路径,因为cron可能非常挑剔。

Try this after 'crontab -e':

在“crontab -e”之后试试这个:

2 * * * * /bin/bash /home/username/jek.sh

If you want to string commands together, use && between each:

如果您想将命令串在一起,请使用&&在每个命令之间:

2 * * * * /bin/bash /home/user_name/.bash_profile && /bin/bash /home/username/jek.sh

As a note, besides /bin/ you can find many commands in /usr/bin. To find the absolute location of a command, use 'which'. For example, :

作为一个说明,除了/bin/您可以在/usr/bin中找到许多命令。要找到命令的绝对位置,请使用“which”。例如,:

user@computer:~$ which bash
 /bin/bash

Finally, you can probably use cron to run the ruby script directly, with an absolute path to jekyll, and probably after && in order to set up the environment first.

最后,您可能可以使用cron直接运行ruby脚本,并具有绝对的jekyll路径,可能在&&之后,以便首先设置环境。