C代码无法在cron中运行select查询

时间:2013-03-17 09:29:27

标签: mysql c linux shell cron

我们有一个C代码如下。这就是我们编译它的方法gcc -o get1Receive $(mysql_config --cflags)get1ReceiveSource.c $(mysql_config --libs)-lrt。我们从终端跑步时工作正常。然后我们尝试使用cron job运行它,当我们查看这两行printf(“\ nNumf of fields:%d”,num_fields);和printf(“\ nNofof of row:%lu”,mysql_num_rows(localRes1));.第一行显示4作为值,第二行显示永不给出任何值,并且始终为0.我们采用相同的选择查询并在数据库上运行并确认有值但是在通过cron job运行时它不会提供。脚本也被赋予了可执行权限。

#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
#include <time.h>
#include <signal.h>
#include <mysql.h>
#include <string.h>

int flag = 0;



int main () {
                  MYSQL *localConn;
                  MYSQL_RES *localRes1;
                  MYSQL_ROW localRow1;
                  char *server = "localhost";
                  char *user = "user1";
                  char *password = "*****"; 
                  char *database = "test1";
                  localConn = mysql_init(NULL);
                  if (!mysql_real_connect(localConn, server,
                         user, password, database, 0, NULL, 0)) {
                      fprintf(stderr, "%s\n", mysql_error(localConn));
                      exit(1);
                  }

            struct timeval tv;
          char queryBuf1[500],queryBuf2[500];
          char buff1[20] = {0};
          char buff2[20] = {0};
          gettimeofday (&tv, NULL);
          //fprintf (stderr, "[%d.%06d] Flag set to 1 on ", tv.tv_sec, tv.tv_usec);
          //tv.tv_sec -= 5;
          strftime(buff1, 20, "%Y-%m-%d %H:%M:00", localtime(&tv.tv_sec));
          strftime(buff2, 20, "%Y-%m-%d %H:%M:59", localtime(&tv.tv_sec));
          printf("\nTime from %s", buff1);
          printf("\nTime to %s", buff2);

          sprintf(queryBuf1,"SELECT ipDest, macDest,portDest, sum(totalBits) FROM dataReceive WHERE timeStampID between '%s' And '%s'  GROUP BY ipDest, macDest, portDest ",buff1,buff2);
                printf("\nQuery receive %s",queryBuf1);


                if(mysql_query(localConn, queryBuf1))
                {
                    printf("Error in first query of select %s\n",mysql_error(localConn));
                    exit(1);
                }

                localRes1 = mysql_store_result(localConn);
                int num_fields = mysql_num_fields(localRes1);

                printf("\nNumf of fields : %d",num_fields);
                printf("\nNof of row : %lu",mysql_num_rows(localRes1));

                while((localRow1 = mysql_fetch_row(localRes1)) !=NULL)
                {
                  int totalBits = atoi(localRow1[3]);

                  printf("totalBits %d\n", totalBits);
                  printf("RECEIVE %s,%s\n", localRow1[0], localRow1[1]);
                  if(totalBits>5000)
                  {
                    sprintf(queryBuf1,"INSERT INTO alertReceive1 (timeStampID,ipDest, macDest, portDest, totalBits)VALUES ('%s','%s','%s','%s',%s)",buff1, localRow1[0],localRow1[1],localRow1[2],localRow1[3]);
                    printf("Query 1 before executing %s\n",queryBuf1);
                    if (mysql_real_query(localConn,queryBuf1,strlen(queryBuf1))) {
                   printf("Error in first insert %s\n",mysql_error(localConn));
                   fprintf(stderr, "%s\n", mysql_error(localConn));
                   exit(1);
                   }
                    //printf("Query 1 after executing %s\n",queryBuf1);*/
                   }    
                } 


          mysql_free_result(localRes1); 
          mysql_close(localConn);


}

我们已运行此命令文件get1Receive并导致

file get1Receive
get1Receive.c: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, not stripped

我们还运行此命令* * * * * set&gt; / tmp / myvars及以下是结果。

GROUPS=()
HOME=/root
HOSTNAME=capture
HOSTTYPE=x86_64
IFS='
'
LOGNAME=root
MACHTYPE=x86_64-redhat-linux-gnu
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/bin:/bin
POSIXLY_CORRECT=y
PPID=11086
PS4='+ '
PWD=/root
SHELL=/bin/sh
SHELLOPTS=braceexpand:hashall:interactive-comments:posix
SHLVL=1
TERM=dumb
UID=0
USER=root
_=/bin/sh

1 个答案:

答案 0 :(得分:4)

通用提示(另见我的评论):

  • Advanced Linux Programmingman pages(您可以通过输入man manman 2 intro获取阅读文档。在终端等等...)和MySQL 5.5 reference。请务必了解GIYFSTFW的含义。

  • \n放在printf格式字符串的末尾,而不是开头。

  • 此外,如果合适,请致电fflush(NULL),特别是在任何MySQL查询之前,例如在mysql_real_query来电之前,以及while循环结束

  • 之前
  • 使用gcc -Wall -g进行编译,例如使用终端中的以下命令

    gcc -Wall -g $(mysql_config --cflags) get1ReceiveSource.c \
               $(mysql_config --libs) -lrt -o get1Receive
    
  • 改进代码,直到没有给出警告。 (您甚至可能想要-Wall -Wextra而不只是-Wall)。不要忘记使用像git这样的版本控制系统。

  • 使用gdb调试器(您需要learn如何使用它)。

    (只有当您确定代码中没有更多错误在编译命令中用-g替换-O2 -g时)

  • 使用sizeof;大多数20出现sizeof,或者至少使用#define SMALLSIZE 20,然后只有SMALLSIZE而不是20

  • 使用snprintf而不是sprintf(并测试其结果大小,这应该适合!)。 snprintf(3)需要额外的大小参数,例如

     if (snprintf(querybuf, sizeof querybuf,
                  "SELECT ipDest, macDest, portDest, sum(totalBits)"
                  " FROM dataReceive"
                  " WHERE timeStampID between '%s' And '%s' "
                  " GROUP BY ipDest, macDest, portDest ",
                  buff1, buff2) >= (int) (sizeof querybuf))
         abort();
    
  • 考虑将syslog(3)openlog一起使用,并查看系统日志。

我不知道queryBuf1是如何宣布的。 (你的代码,如发布的,可能甚至不编译!)。您可能需要char querybuf[512]; ...

之类的内容

最重要的是,在mysql_real_query循环中调用mysql_fetch_row 错误:您应该在发出下一个MySQL查询之前获取所有行。详细了解MySQL C API

您也忘了测试localRes1的结果mysql_store_result(localConn);当syslogmysql_error(localConn)时,localRes1显示某种方式(可能是NULL} ....