在特定时间运行cronjob

时间:2021-06-09 01:05:48

I would like to run a specific script at a certain time (only once!). If I run it normally like this:

我想在某个时间运行一个特定的脚本(只有一次!)。如果我像这样正常运行:

marc@Marc-Linux:~/tennis_betting_strategy1/wrappers$ Rscript write_csv2.R

It does work. I however would like to program it in a cronjob to run at 10:50 and therefore did the following:

它确实有效。然而,我想在cronjob中编程以在10:50运行,因此执行以下操作:

50 10 11 05 * Rscript ~/csv_file/write_csv.R

50 10 11 05 * Rscript~ / csv_file / write_csv.R

This does not seem to work however. Any thoughts where I go wrong? These are the details of the cron package im using:

但这似乎不起作用。我哪里出错了?这些是我使用的cron包的细节:

PID COMMAND
1015 cron

My system time also checks out:

我的系统时间也检查出来:

marc@Marc-Linux:~/tennis_betting_strategy1/wrappers$ date
wo mei 11 10:56:46 CEST 2016

3 个解决方案

#1


0  

Use full path (started from /) for both Rscript and write_csv2.R. Check sample command as follows

对Rscript和write_csv2.R使用完整路径(从/开始)。检查示例命令如下

/tmp/myscript/Rscript /tmp/myfile/write_csv2.R

Ensure you have execution permission of Rscript and write permission in folder where write_csv2.R will be created(/tmp/myfile)

确保您在创建write_csv2.R的文件夹中具有Rscript和写权限的执行权限(/ tmp / myfile)

#2


3  

There is a special tool for running commands only once - at.

有一个特殊的工具只能运行一次命令 - at。

With at you can schedule a command like this:

有了你可以安排这样的命令:

at 09:05 am today
at> enter you commands...

Note, you'll need the atd daemon running.

注意,您需要运行atd守护程序。

Your crontab entry looks okay, however. I'd suggest checking if the cron daemon is running(exact daemon name depends on the cron package; it could be cron, crond, or vixie-cron, for instance). One way to check if the daemon is running is to use the ps command, e.g.:

但是,您的crontab条目看起来没问题。我建议检查cron守护程序是否正在运行(确切的守护程序名称取决于cron包;例如,它可以是cron,crond或vixie-cron)。检查守护程序是否正在运行的一种方法是使用ps命令,例如:

$ ps -C cron -o pid,args
  PID COMMAND
  306 /usr/sbin/cron

#3


1  

Some advices.

Read more about the PATH variable. Notice that it is set differently in interactive shells (see your ~/.bashrc) and in cron or at jobs. See also this about Rscript.

阅读有关PATH变量的更多信息。请注意,在交互式shell(请参阅〜/ .bashrc)和cron或作业中设置的方式不同。另见关于Rscript的内容。

Replace your command by a shell script, e.g. in ~/bin/myrscriptjob.sh

用shell脚本替换你的命令,例如在〜/ bin / myrscriptjob.sh中

That myrscriptjob.sh file should start with #!/bin/sh

那个myrscriptjob.sh文件应该以#!/ bin / sh开头

Be sure to make that shell script executable:

一定要使该shell脚本可执行:

chmod u+x ~/bin/myrscriptjob.sh

Add some logging in your shell script, near the start; either use logger(1) or at least some date(1) command suitably redirected, or even both:

在开始附近的shell脚本中添加一些日志记录;使用logger(1)或至少某些日期(1)命令适当重定向,或者甚至两者:

#!/bin/sh
# file myrscriptjob.sh
/bin/date +"myrscriptjob starting %c %n" > /tmp/myrscriptjob.start
/usr/bin/logger -t myrscript job starting $$
/usr/local/bin/Rscript $HOME/csv_file/write_csv.R

in the last line above, replace /usr/local/bin/Rscript by the output of which Rscript done in some interactive terminal.

在上面的最后一行中,用一些交互式终端中完成的Rscript输出替换/ usr / local / bin / Rscript。

Notice that you should not use ~ (but replace them with $HOME when appropriate) in shell scripts.

请注意,在shell脚本中不应使用〜(但在适当时用$ HOME替换它们)。

Finally, use at to run your script once. If you want to run it periodically in a crontab job, give the absolute path, e.g.

最后,使用at运行一次脚本。如果要在crontab作业中定期运行它,请提供绝对路径,例如

 5 09 11 05 * $HOME/bin/myrscriptjob.sh

and check in /tmp/myrscriptjob.start and in your system log if it has started successfully.

并检查/tmp/myrscriptjob.start并在系统日志中检查它是否已成功启动。

BTW, in your myrscriptjob.sh script, you might replace the first line #!/bin/sh with #!/bin/sh -vx (then the shell is verbose about execution, and cron or at will send you some email). See dash(1), bash(1), execve(2)

顺便说一句,在你的myrscriptjob.sh脚本中,你可以用#!/ bin / sh -vx替换#!/ bin / sh的第一行(然后shell是关于执行的详细信息,而cron或at会向你发送一些电子邮件)。见破折号(1),bash(1),execve(2)

#1


0  

Use full path (started from /) for both Rscript and write_csv2.R. Check sample command as follows

对Rscript和write_csv2.R使用完整路径(从/开始)。检查示例命令如下

/tmp/myscript/Rscript /tmp/myfile/write_csv2.R

Ensure you have execution permission of Rscript and write permission in folder where write_csv2.R will be created(/tmp/myfile)

确保您在创建write_csv2.R的文件夹中具有Rscript和写权限的执行权限(/ tmp / myfile)

#2


3  

There is a special tool for running commands only once - at.

有一个特殊的工具只能运行一次命令 - at。

With at you can schedule a command like this:

有了你可以安排这样的命令:

at 09:05 am today
at> enter you commands...

Note, you'll need the atd daemon running.

注意,您需要运行atd守护程序。

Your crontab entry looks okay, however. I'd suggest checking if the cron daemon is running(exact daemon name depends on the cron package; it could be cron, crond, or vixie-cron, for instance). One way to check if the daemon is running is to use the ps command, e.g.:

但是,您的crontab条目看起来没问题。我建议检查cron守护程序是否正在运行(确切的守护程序名称取决于cron包;例如,它可以是cron,crond或vixie-cron)。检查守护程序是否正在运行的一种方法是使用ps命令,例如:

$ ps -C cron -o pid,args
  PID COMMAND
  306 /usr/sbin/cron

#3


1  

Some advices.

Read more about the PATH variable. Notice that it is set differently in interactive shells (see your ~/.bashrc) and in cron or at jobs. See also this about Rscript.

阅读有关PATH变量的更多信息。请注意,在交互式shell(请参阅〜/ .bashrc)和cron或作业中设置的方式不同。另见关于Rscript的内容。

Replace your command by a shell script, e.g. in ~/bin/myrscriptjob.sh

用shell脚本替换你的命令,例如在〜/ bin / myrscriptjob.sh中

That myrscriptjob.sh file should start with #!/bin/sh

那个myrscriptjob.sh文件应该以#!/ bin / sh开头

Be sure to make that shell script executable:

一定要使该shell脚本可执行:

chmod u+x ~/bin/myrscriptjob.sh

Add some logging in your shell script, near the start; either use logger(1) or at least some date(1) command suitably redirected, or even both:

在开始附近的shell脚本中添加一些日志记录;使用logger(1)或至少某些日期(1)命令适当重定向,或者甚至两者:

#!/bin/sh
# file myrscriptjob.sh
/bin/date +"myrscriptjob starting %c %n" > /tmp/myrscriptjob.start
/usr/bin/logger -t myrscript job starting $$
/usr/local/bin/Rscript $HOME/csv_file/write_csv.R

in the last line above, replace /usr/local/bin/Rscript by the output of which Rscript done in some interactive terminal.

在上面的最后一行中,用一些交互式终端中完成的Rscript输出替换/ usr / local / bin / Rscript。

Notice that you should not use ~ (but replace them with $HOME when appropriate) in shell scripts.

请注意,在shell脚本中不应使用〜(但在适当时用$ HOME替换它们)。

Finally, use at to run your script once. If you want to run it periodically in a crontab job, give the absolute path, e.g.

最后,使用at运行一次脚本。如果要在crontab作业中定期运行它,请提供绝对路径,例如

 5 09 11 05 * $HOME/bin/myrscriptjob.sh

and check in /tmp/myrscriptjob.start and in your system log if it has started successfully.

并检查/tmp/myrscriptjob.start并在系统日志中检查它是否已成功启动。

BTW, in your myrscriptjob.sh script, you might replace the first line #!/bin/sh with #!/bin/sh -vx (then the shell is verbose about execution, and cron or at will send you some email). See dash(1), bash(1), execve(2)

顺便说一句,在你的myrscriptjob.sh脚本中,你可以用#!/ bin / sh -vx替换#!/ bin / sh的第一行(然后shell是关于执行的详细信息,而cron或at会向你发送一些电子邮件)。见破折号(1),bash(1),execve(2)