Linux 如何设置 cron 作业以每小时运行一个可执行文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3474280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set up a cron job to run an executable every hour?
提问by Anonymous
I need to set up a cron job that runs an executable compiled using gcc once every hour.
我需要设置一个 cron 作业,该作业每小时运行一次使用 gcc 编译的可执行文件。
I logged in as root and typed crontab -e
我以 root 身份登录并输入 crontab -e
Then I entered the following and saved the file.
然后我输入以下内容并保存文件。
0 * * * * /path_to_executable
However, the cron job does not work.
但是,cron 作业不起作用。
I see that when I type /...path_to_executable
I get a segmentation fault.
I can only execute the executable from the folder it is located in.
Is there a way I can solve this problem?
我看到当我输入时,/...path_to_executable
我得到了一个分段错误。我只能从它所在的文件夹中执行可执行文件。有没有办法解决这个问题?
采纳答案by joast
0 * * * * cd folder_containing_exe && ./exe_name
should work unless there is something else that needs to be setup for the program to run.
应该可以工作,除非需要设置其他程序才能运行。
回答by jartieda
use
用
path_to_exe >> log_file
to see the output of your command also errors can be redirected with
要查看命令的输出,还可以使用以下命令重定向错误
path_to_exe &> log_file
also you can use
你也可以使用
crontab -l
crontab -l
to check if your edits were saved.
检查您的编辑是否已保存。
回答by Anonymous
Since I could not run the C
executablethat way, I wrote a simple shell script
that does the following
由于我无法以这种方式运行C
可执行文件,我写了一个简单的shell script
执行以下操作
cd /..path_to_shell_script
./c_executable_name
In the cron jobs list, I call the shell script.
在 cron 作业列表中,我调用了 shell 脚本。
回答by Jayan
Did you mean the executable fails to run , if invoked from any other directory? This is rather a bug on the executable. One potential reason could be the executable requires some shared libraires from the installed folder. You may check environment variable LD_LIBRARY_PATH
如果从任何其他目录调用,您的意思是可执行文件无法运行吗?这是可执行文件上的一个错误。一个潜在的原因可能是可执行文件需要安装文件夹中的一些共享库。您可以检查环境变量LD_LIBRARY_PATH
回答by David Thornley
The solution to solve this is to find out why you're getting the segmentation fault, and fix that.
解决此问题的解决方案是找出出现分段错误的原因,然后解决该问题。
回答by Marek Skiba
You can also use @hourly
instant of 0 * * * *
您还可以使用@hourly
即时0 * * * *
回答by James Gentes
If you're using Ubuntu, you can put a shell script in one of these folders: /etc/cron.daily
, /etc/cron.hourly
, /etc/cron.monthly
or /etc/cron.weekly
.
如果你正在使用Ubuntu,你可以把一个shell脚本,在这些文件夹中的一个:/etc/cron.daily
,/etc/cron.hourly
,/etc/cron.monthly
或/etc/cron.weekly
。
For more detail, check out this post: https://askubuntu.com/questions/2368/how-do-i-set-up-a-cron-job
有关更多详细信息,请查看此帖子:https: //askubuntu.com/questions/2368/how-do-i-set-up-a-cron-job