Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lua os.execute() return wrong value

Tags:

lua

have a problem:

 local stat = assert(os.execute("/usr/bin/pgrep -f 'tail -F /opt/aaa' >& /dev/null"))
 print(stat)  --> 0

But when I type pgrep -f 'tail -F /opt/aaa' >& /dev/null in bash, and then call echo $? it returns 1. Has anybody encountered this before, or know the reason why ;-) what happened?

like image 304
ms2008 Avatar asked Jul 25 '26 20:07

ms2008


1 Answers

Doesn't seem to be a Lua problem to me, os.execute is just wrapping a call to system:

 static int os_execute (lua_State *L) {
    lua_pushinteger(L, system(luaL_optstring(L, 1, NULL)));
    return 1;
 }

If you try the C alternative you have the correct result code?

 #include <stdio.h>
 #include <string.h>

 int main ()
 {
    char command[100];
    int result;

    strcpy( command, "/usr/bin/pgrep -f 'tail -F /opt/aaa' >& /dev/null" );
    result = system(command);

    return(0);
  } 
like image 131
guilespi Avatar answered Jul 27 '26 18:07

guilespi