0

I am running python commands on solaris environment. I am not able to execute subprocess.call on it as it gives me an error in execute_child "[Errno 2] No such file or directory". I tried alternative ways using popen2/3/4 but it throws an error "DeprecationWarning: The popen2 module is deprecated. Use the subprocess module." I dont want to use os.system as because of some limitations. Is there some other way to make this work? Any help will be appreciated.

2 Answers 2

2

Have you tried subprocess.popen()? It's as simple as:

output, error = subprocess.popen(your_command, shell=True, 
                stdout=subprocess.PIPE, stderr=subprocess.PIPE);

It replaces popen2(), which is indeed deprecated:

os.popen2(cmd[, mode[, bufsize]])

Deprecated since version 2.6: This function is obsolete. Use the subprocess module. Check especially the Replacing Older Functions with the subprocess Module section.

Regarding the No such file or directory error, try using absolute path.

0
0

Below is some code from a python program I wrote that manages Solaris Volume Manager (SVM) volumes on Solaris 10 systems. It uses the same approach decscribed earlier, but adds in the p.communicate(). Note in this case the stderr output is redirected to standard output because there was no need in this case to differentiate between error messages and normal output.

# Constants
DATADIR="/etc/lvm"
METASTAT_CMD="/usr/sbin/metastat"
MD_CF = DATADIR + "/md.cf"

def check_svm_volume_exists():
    # Check to see if any SVM volume(s) exist or not. If none exist, exit.
    syscmd = " ".join([METASTAT_CMD, "-p", ">", MD_CF])
    p = subprocess.Popen(syscmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    out_data, err_data = p.communicate()

    if out_data == "":
        return out_data 
    else:
        print("No Solaris Volume Manager volume(s) appear to be configured. Exiting.")
        sys.exit(1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.