Mysql 选择查询未在 cron 上生成结果,但可以通过终端

Mysql 选择查询未在 cron 上生成结果,但可以通过终端

我们有一个 C 程序(源代码如下)。我们使用以下命令对其进行编译:

gcc -Wall -Wextra -g $(mysql_config --cflags) get1ReceiveSource.c $(mysql_config --libs) -lrt -o get1Receive 

在终端上使用 运行时,select 可以完美地给出结果./get1Receive。现在我们使用 cron 条目转到每分钟的 cron 作业

/1 * * * * /usr/local/bin/get1Receive >> /var/log/myalert1.log 2>&1 

我只是看不到它得到任何结果。我已经使用进行了调试gdb,它运行得很好,我设置了断点并逐步执行,我可以看到它也很好地进入了循环。我还附加了环境值,我怀疑 mysql 在那里有问题?有什么想法吗?

C 代码:

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

#define SMALLSIZE 20;

int main () {
    openlog("get1recieve",LOG_PID|LOG_PERROR,LOG_LOCAL0); 
    time_t now = 0; 
    time(&now); 
    syslog(LOG_INFO, "started at %s", ctime(&now));
    syslog (LOG_INFO, "Program started by User %d", getuid ()); 
    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));
          syslog(LOG_ERR, "failed to connect to MySQL : %s", mysql_error(localConn));
          exit(EXIT_FAILURE); 
          //exit(1);
      }

        struct timeval tv;
    char queryBuf1[512];
    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);


    fflush(NULL);    
     if (snprintf(queryBuf1, sizeof queryBuf1,
        "SELECT ipDest, macDest, portDest, sum(totalBits)"
        " FROM dataReceive"
        " WHERE timeStampID between '%s' And '%s' "
        " GROUP BY ipDest, macDest, portDest ",
        buff1, buff2) >= (int) (sizeof queryBuf1))
     abort();

            syslog (LOG_INFO, "querybuf %s at line %d", queryBuf1, __LINE__);


            if(mysql_query(localConn, queryBuf1))
            {
                printf("Error in first query of select %s\n",mysql_error(localConn));
                exit(1);
            }
            localRes1 = mysql_use_result(localConn);
            //localRes1 = mysql_store_result(localConn);
            int num_fields = mysql_num_fields(localRes1);
            if (!localRes1)
            syslog(LOG_PERROR, "loop# mysql useresult failure %s",  mysql_error(localConn));
            printf("\nNumf of fields : %d",num_fields);
            printf("\nNof of row : %lld",mysql_num_rows(localRes1));

            while((localRow1 = mysql_fetch_row(localRes1)) !=NULL)
            {
              printf("TEST");

            } 
            fflush(NULL); 
              mysql_free_result(localRes1); 
        mysql_close(localConn);

       closelog ();        
      return 0;      

}

我们已经运行了这个命令文件 get1Receive 并得到

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

我们还运行了此命令 * * * * * set > /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

你可以尝试:

/1 * * * * /bin/bash -c'/usr/local/bin/get1Receive >> /var/log/myalert1.log 2>&1'

看看是否有效?

相关内容