简体   繁体   English

以cron形式运行的C脚本,授予权限被拒绝错误

[英]C script running as cron giving permission denied error

I have a .c file compiled and would like to run via a cron job but I end up getting this error: 我编译了一个.c文件,并希望通过cron作业运行,但最终出现此错误:

/bin/sh: /usr/local/bin/get1Receive.c: Permission denied. 

What is causing this error and how do I fix it? 是什么导致此错误,我该如何解决?

Should I be running the .c file in cron or a different compiled file? 我应该在cron中运行.c文件还是在其他编译文件中运行?

Results from /tmp/myvars / 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

Results from file get1Receive.c 来自文件get1Receive.c的结果

file get1Receive.c
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

Snippet of codes. 代码段。

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))
                {
                    //fprintf(stderr, "%s\n", mysql_error(localConn));
                    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));

If the output of this command: 如果此命令的输出:

file  get1Receive1.c

shows that file name to be a valid executable that part is very unusual, but okay. 显示文件名是有效的可执行文件,该部分非常不正常,但是可以。

Assuming you are using biz14 (or your real username's ) crontab try this: 假设您使用的是biz14(或您的真实用户名)crontab,请尝试以下操作:

use the command crontab -e to create this line in your crontab: 使用命令crontab -ecrontab -e中创建此行:

* * * * *  set > /tmp/myvars

Wait a few minutes, go back into crontab -e and delete that entry. 等待几分钟,返回crontab -e并删除该条目。

Use the set command from the command line to see what variables and aliases exist. 从命令行使用set命令查看存在哪些变量和别名。 Compare that with that you see in /tmp/myvars You have to change how your C code executes by changing the variables and aliases the cron job runs with. 与在/tmp/myvars看到的内容进行比较,您必须通过更改cron作业运行时使用的变量和别名来更改C代码的执行方式。

If you are running the cron job in someone else's crontab, then you have a bigger problem. 如果您正在其他人的crontab中运行cron作业,那么您会遇到更大的问题。 Check file permissions on get1Receive1.c. 检查get1Receive1.c上的文件权限。 and the directory it lives in. That other user (the one who wons the crontab) has to have permissions set on your directory and get1Receive1.c so the job can run. 另一个用户(赢得crontab的用户)必须在您的目录和get1Receive1.c上设置权限,这样才能运行作业。

Example crontab entry: crontab条目示例:

0 10 * * 1-5 /path/to/get1Receive1.c > /tmp/outputfile

Read /tmp/outputfile to see what you got. 阅读/tmp/outputfile查看您得到了什么。 You are using printf in your code. 您在代码中使用printf printf only writes to the controlling terminal. printf仅写入控制终端。 There is no controlling terminal, so redirect the printf stuff to a file. 没有控制终端,因此将printf内容重定向到文件。

Last effort on this problem: Check return codes on EVERYTHING. 解决此问题的最后努力:检查所有内容的返回码。 All C functions like fread(), any db function, etc. If a return code gives a fail response ( these are different for different function calls) then report the error number the line number and function - gcc provides LINE and func . 所有C函数,例如fread(),任何db函数等。如果返回代码给出失败响应(对于不同的函数调用,它们是不同的),则将错误号报告为行号和函数-gcc提供LINEfunc Example: 例:

printf("error on line %d in my code %s, error message =%s\n", __LINE__, __func__, [string of error message]);

If you do not check return codes you are writing very poor C code. 如果不检查返回码,则说明您编写的C代码很差。

CHECK return codes, please, now! 请立即检查返回码!

Permission wise you could have two issues. 权限方面,您可能会遇到两个问题。 1. The 'c' file's permissions don't allow who you are running it as to run it. 1.'c'文件的权限不允许您像谁一样运行它。 2. You are running the cron with a script which doesn't have permissions. 2.您正在使用没有权限的脚本运行cron。

Here's a helpful post: How to give permission for the cron job file? 这是一篇很有帮助的文章: 如何授予cron作业文件权限?

The fact that you are running a 'c' file and referring to it as a script makes me think you're using C shell and not writing it as a C language program which would need to be compiled and have the generated executable run by the cron. 您正在运行“ c”文件并将其称为脚本的事实使我认为您正在使用C shell,而不是将其编写为C语言程序,因此需要对其进行编译并使生成的可执行文件由C运行cron的。 If you're not using gcc or have never called gcc on your 'C' script then it's not C and call it C shell to avoid confusion. 如果您没有使用gcc或从未在'C'脚本中调用过gcc,那么它就不是C,而是将其命名为C shell以避免混淆。

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

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