小编Het*_*tiu的帖子

system()偶尔会返回2

我用system()库函数编写了一个函数,如下所示:

int execute(const char* cmd)
{
  int ret = system(cmd);

  if (ret != -1)
  {
    if (WIFEXITED(ret))
      ret = WEXITSTATUS(ret);
    else
      ret = -1;
  }

  LINFO( "execute %s, ret = %d", cmd, ret);  // logging
  return ret;
}
Run Code Online (Sandbox Code Playgroud)

然后,我使用shell脚本调用它,如下所示:

#!/bin/sh
PATH=$PATH:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin
cd $(dirname $0)

agent_name=`grep 'agent_name' ../etc/config.ini |awk '{ print $3 }'`
py='../../python26/bin/python'

check_alive()
{
    status=`ps -ef | grep "$agent_name" | grep -v "grep" |wc -l`

    if [ $status -ne 0 ]; then
        # process exist
        echo "$agent_name …
Run Code Online (Sandbox Code Playgroud)

c shell system

8
推荐指数
1
解决办法
410
查看次数

标签 统计

c ×1

shell ×1

system ×1