system执行shell命令

摘要:
很容易阻止系统进程。您可以在命令后添加&以使系统立即返回。返回值如果命令为NULL,则非零值表示外壳可用,零值表示外壳不可用。如果子进程的创建失败,或无法获得子进程状态,则返回-1。通常情况下,这不会发生,但您应该判断此异常,并判断返回值不是-1。例如,执行的shell命令不存在。系统返回0x7F00,shell退出代码为127。系统调用成功。返回值是执行命令的shell的结束状态。

system - execute a shell command

#include <stdlib.h>

int system (const char *command);

描述

The system() uses fork to create a child process that executes a command specified in command using execl as follows:

execl("/bin/sh", "sh", "-c", command, (char *) 0);

and returns after the command has been completed.

During excution of the comman, SIGCHLD will be bocked, and SIGINT and SIGQUIT will be ignored.

If command is NULL, the system() returns a status indicating whether a shell is available on the system.

注意:system在执行完command后才会返回。易造成system进程阻塞,可在command后增加&,以让system立即返回。

返回值

》假如command是NULL,非零值指示shell(执行cmd的/bin/sh)可用,0指示shell不可用。一般情况都返回非0(NULL时),因为系统shell可用。

》假如chilid进程创建失败(fork失败),或child进程状态不能获取(不知fork进程是否创建成功,如内存不足等或未知原因),返回-1。一般不会出现此情况,但应判断此异常情况,判断返回值不为-1。

》假如shell不能在child子进程中执行,此时类似子进程调用_exit(127)退出。比如执行的shell命令不存在。system返回0x7F00(32512),shell退出代码为127.

》system调用成功,返回值是执行command的shell的结束状态(shell的退出状态是shell执行的最后command的终止状态)。

最后两情况,返回值是“wait status"。wait status可以用waitpid宏检测(如WIFEXITED(),WEXITSTATUS()等)。

system() does not affect the wait status of any other children.

注意

As mentioned, system() ignores SIGINT and SIGQUIT. This may make programs that call it from a loop uninterruptible, unless they take care themselves to check the exit status of the child. E.g.

  while(something) {

    int ret = system("foo");

    if(WIFSIGNALED(ret) && (WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT))

      break;

  }

It is possible for the shell command to return 127, so that code is not a sure indication that the execve call failed.

一般应用举例(system执行时,fork必须成功(不能返回-1);一般shell成功返回0,判断不为0退出):

ret = system(cmd);

if(ret < 0){

  exit (1); 

}


if(WEXITSTATUS(ret) != 0){

  printf("cmd failed.
");

  exit(1);

}

system立刻返回的举例:

#include <stdio.h>
#include <stdlib.h>
#include <wait.h>

#define CMD "./hao.sh&"

int main(int argc, char *argv[])
{

    int ret = 0;
    if(argc == 1){ 
        ret = system(CMD);
//      ret = system(NULL);
    } else {
        ret = system(argv[1]);
    }   

    printf("return:%d
", ret);
    if(ret == 0){ 
        printf("Success
");
    } else {
        printf("Fail
");
        if(WIFEXITED(ret)){
            printf("error status:%d
", WEXITSTATUS(ret));
        }   
        if(WIFSIGNALED(ret)){
            printf("error signal:%d
", WTERMSIG(ret));
        }   
    }   
    return 0;
}

免责声明:文章转载自《system执行shell命令》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇python 基础 5.1 python 构造器jsp导出excel 解决文件名中文乱码下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

eclipse下搭建shell脚本编辑器--安装开发shell的eclipse插件shelled

eclipse下搭建shell脚本编辑器--安装开发shell的eclipse插件shelled 1、安装“man-page viewer”插件(ShellEd需要,有的机器不需要,先装上) 安装地址:http://download.eclipse.org/technology/linuxtools/updates-nightly/ 2、安装“shel...

(转)java enum枚举

转载自: 原理:http://singleant.iteye.com/blog/686349 应用:http://www.cnblogs.com/happyPawpaw/archive/2013/04/09/3009553.html Java Enum 基本原理 1. 关于 Java Enum 学过 C/C++ 等语言的人,应该都对 Enum 类型略知一二...

adb命令大全

基本用法 命令语法 adb 命令的基本语法如下: 1 adb [-d|-e|-s <serialNumber>] <command> 如果只有一个设备/模拟器连接时,可以省略掉[-d|-e|-s <serialNumber>]这一部分,直接使用adb <command>。 为命令指定目标设备 如果有多个设备...

ssh secure Shell连接阿里云服务器,报错的问题,openssh无法登录:server responded "algorithm negotiation failed”

1、putty登录连接到阿里云服务器 2、修改ssh的配置文件 vi /etc/ssh/sshd_config 切换编辑模式(i),设置参数 PasswordAuthentication yes 在配置文件末尾添加: Ciphers aes128-cbc,aes192-cbc,aes256-cbc,aes128-ctr,aes192-ctr,aes256-...

adb shell 查看系统属性(用来判断特殊的操作系统)

一般来讲,在android程序开发中进行需要判断设备类型和系统版本 1、设备类型判断(android.os.Build.MODEL) 比如判断属于Google Nexus 5,Nexus 7,MIUI v5, MIUI v6,三星设备,魅族设备等; 这类型的问题都使用的android.os.Build.MODEL来判断,android.os.Build.M...

oracle sql*plus常用命令

一、sys用户和system用户Oracle安装会自动的生成sys用户和system用户(1)、sys用户是超级用户,具有最高权限,具有sysdba角色,有create database的权限,该用户默认的密码是change_on_install(2)、system用户是管理操作员,权限也很大。具有sysoper角色,没有create database的权...