我目前正在编写一个程序,它将使用C在CLI中运行多个命令(在一行中)。
我需要运行的第一个命令是sudo -s
。所以我单独运行此命令来测试程序是否正在运行,但程序在运行此命令时挂起。我想知道问题是程序还是命令,所以我运行ls
。当我运行ls
命令时,程序运行正常,所以我认为sudo -s
命令有问题,也许我需要对该命令执行某些操作,以便它在CLI中运行。
这是接受命令的函数:
int executeCommand(char *command, char *result)
{
/*This function runs a command./*/
/*The return value is the output of command*/
int nSuccess = -1;
FILE * fp = NULL;
char buffer[1035];
if (command == NULL)
render("Command is null");
if (result == NULL)
render("result is null");
if (command!=NULL && result!=NULL)
{
fp=popen(command,"r");
if(fp!=NULL)
{
strcpy(result,"\0");
while(fgets(buffer, sizeof(buffer)-1,fp)!=NULL)
{
strcat(result,buffer);
}
pclose(fp);
} nSuccess=0;
}
return nSuccess;
}
顺便说一句,我正在做一个Web应用程序,用户将编写他想要执行的命令。输入将使用ajax
请求通过POST
发送到服务器。服务器在Linux上运行。
答案 0 :(得分:0)
sudo -s
将提示输入密码,不会立即返回结果。
答案 1 :(得分:0)
sudo -s
将需要密码,因此运行此命令的程序可能会等待输入密码