简体   繁体   English

用Cron运行C ++程序

[英]Running c++ program with Cron

I'm trying to run a c++ program on my server, which, for now, is updating a MySQL database. 我正在尝试在服务器上运行C ++程序,目前该服务器正在更新MySQL数据库。 I'm running it with Cron. 我正在Cron上运行它。 The program is compiling and running on my own computer (Ubuntu 10.10), but I can seem to get something working on my server. 该程序正在编译并在我自己的计算机上运行(Ubuntu 10.10),但是我的服务器似乎可以正常工作。

I checked if cron is working for me with a PHP script, but I don't know what to do to run a c++ program. 我检查了cron是否正在使用PHP脚本工作,但是我不知道如何运行c ++程序。 In directAdmin I'm using the command: 在directAdmin中,我使用以下命令:

/home/rogier/domains/ditiseenwinkel.nl/public_html/test/program &>/home/rogier/domains/ditiseenwinkel.nl/public_html/test/testsql2.log  

Where program is my compiled program. program是我的编译程序。 Strange thing: the logfile is empty. 奇怪的是:日志文件为空。

What am I doing wrong? 我究竟做错了什么? And how should I run a c++ program via Cron? 我应该如何通过Cron运行c ++程序?

Thanks in advance! 提前致谢!

Was the program compiled on the same server? 程序是否在同一服务器上编译? If not - you should compile the sourcecode on the same server. 如果不是,则应在同一服务器上编译源代码。

A C++ program should be compiled into an executable. C ++程序应编译为可执行文件。 Once you get that far the language it was written in doesn't matter any more! 一旦达到目的,那么就不再用它所写的语言了!

Does the command line given run on the server and produce output (ie via command line, not cron)? 给定的命令行是否在服务器上运行并产生输出(即通过命令行而不是cron)? If so the app may be depending on some environment that cron doesn't have. 如果是这样,则该应用程序可能取决于cron没有的某些环境。

What about running it without redirection? 不用重定向运行该怎么办?

Do you have write permission to the logfile? 您对日志文件有写权限吗?

To Cron, it does not matter which type of program C++/PHP/Python/..etc you are running as far as you can execute that command from the shell. 对于Cron而言,您可以运行哪种程序C ++ / PHP / Python / .. etc都没有关系,只要您可以从外壳执行该命令即可。 If a command you write on the shell works, it should work for cron too. 如果您在Shell上编写的命令有效,那么它对cron也应该有效。

However be aware that the environment vars for cron might be completely different. 但是请注意,cron的环境变量可能完全不同。 That is the number 1 cause of problems with cron. 那是导致cron问题的第一大原因。 Even if you install a cron script for your own user, you might be surprised that the cron environment does not have any PATH. 即使为自己的用户安装了cron脚本,您也可能会对cron环境没有任何PATH感到惊讶。 Hence you have to specify the full path for simple system commands lie /bin or /usr/bin 因此,您必须为简单的系统命令lie / bin或/ usr / bin指定完整路径

Like other users said, there might be a case of access rights if the command is executed by other user than yours. 就像其他用户所说的那样,如果命令是由您以外的其他用户执行的,则可能会有访问权限。 I usually test my dump files with the safest path "/tmp" (except if the user is chrooted) 我通常使用最安全的路径“ / tmp”测试我的转储文件(除非用户是chroot)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM