InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

PHP

    PHP strings

    • PHP - split string examples
    • PHP - take last n characters of a string
    • PHP check if string contains something

    PHP Array

    • PHP - associative array value in double quoted string
    • PHP - check if a value is in array
    • PHP - convert array to associative array
    • PHP - print array in one line
    • PHP array foreach - code snippets
    • PHP array map example
    • PHP check if key exists in array
    • PHP sort associative array using custom compare function
    • php get array value with default

    PHP Regex

    • PHP Regex

    PHP Cookbook

    • PHP - assign multi lines string to a variable
    • PHP - call a function with arguments in array examples
    • PHP - get calling file name and line number using debug_backtrace
    • PHP - get class name and file name from an object
    • PHP - get function arguments examples
    • PHP - how to catch errors using set_error_handler
    • PHP - how to initialize static variables
    • PHP - how to use function static variable as cache
    • PHP check if a filename is valid
    • PHP elapsed time
    • PHP execute command and capture shell exit status
    • PHP how to get current url
    • PHP print number to two decimal places
    • PHP remove trailing whitespaces and newline
    • Php equal (==) vs identical (===)
    • Running php eval on code with tags

    PHP functions online

    • PHP functions online
     
    • Home
    • > Tutorials
    • > Linux

    PHP execute command and capture shell exit status

    on Feb 2, 2016

    PHP exec and shell_exec can be used to execute a shell command on server side. exec also populates shell exit return status in one of the argument. shell_exec can also be used to get exit status by redirecting stdout and strerr and then echoing $?.

    Capture return status using php exec

    Here exit status will be set in $ret_var.

    <?php
    exec("ls /etc/passwd", $out, $ret_var);
    echo "successful command ret_var=$ret_var\n";
    
    exec("ls /etc/passwd_non_existing", $out, $ret_var);
    echo "failed command ret_var=$ret_var\n";
    ?>
    successful command ret_var=0
    failed command ret_var=2
    
    Env: PHP 7.4.33 (Linux)

    Capture return status using php shell_exec

    Here main command stderr and stdout will be ignored by redirected to /dev/null (we can use a file also here). After the main command, 2nd command will echo exit status using $? which will be printed on stdout and hence will be returned by shell_exec.

    <?php
    $cmd = "ls /etc/passwd > /dev/null 2>&1; echo $?";
    $status = shell_exec("$cmd");
    $status = rtrim($status);
    echo "successful command shell status=$status\n";
    
    $cmd = "ls /etc/passwd_non_existing > /dev/null 2>&1; echo $?";
    $status = shell_exec("$cmd");
    $status = rtrim($status);
    echo "failed command shell status=$status\n";
    ?>
    successful command shell status=0
    failed command shell status=2
    
    Env: PHP 7.4.33 (Linux)

    Suggested posts:

    1. Bash – how to redirect stderr to stdout or file
    2. Mac – how to open apps not from App Store
    3. Selenium Phantomjs – check browser errors using pytest
    4. Svn – how to make file executable on Linux
    5. How to show environment variable for a process id (pid)
    6. Git show details of a commit hash
    7. How to display auto fade out message using jQuery
    8. PHP remove trailing whitespaces and newline
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged Linux, PHP, PHP cookbook, Tutorials
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress