我用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)