Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is it possible to execute terminal commands using PHP? I need to display cores alloted to each VM and some other details of the virtual machine. Am using KVM. can anyone help to do so. Am using Ubuntu server 11.04. Can anyone explain with example.

share|improve this question
    
refer stackoverflow.com/questions/10598561/… –  saji89 Oct 8 '12 at 8:20
    
there is no clear idea on that –  Poorni Ma Oct 8 '12 at 8:48
    
Are you looking for exact method to solve your KVM issue or the way to run terminal commands from PHP? –  saji89 Oct 8 '12 at 8:51
    
@saji89 i need to know how to run terminal commands in php. is there any other way to make link with KVM virtual manager by php coding ??? –  Poorni Ma Oct 8 '12 at 8:55

2 Answers 2

you can use back-ticks, exec or system command to run terminal commands.

Here is the sample script which might help you

<?php

# back-ticks
$cpuinfo1=`lscpu`;
echo $cpuinfo1;
echo "\n===================\n";

#exec
echo "\n nproc details are here " . exec('nproc');
echo "\n===================\n";

#system
$cpuinfo2 = system('cat /proc/cpuinfo');
echo $cpuinfo2;

?>
share|improve this answer
    
sorry it doesn't works. It displays "===================nproc details are here =================== " –  Poorni Ma Oct 8 '12 at 9:25
    
Do you get any error? –  devav2 Oct 8 '12 at 9:28
    
It displays "===================nproc details are here =================== " –  Poorni Ma Oct 8 '12 at 9:29
    
Do you get any results when you open a terminal and run nproc? –  devav2 Oct 8 '12 at 9:34

the answer above from devav2 works for me on a ubuntu 12 vps. i think it may not work for you guys above because you are on shared hosting?

code:

<?php

# back-ticks
$cpuinfo1=`lscpu`;
echo $cpuinfo1;
echo "\n===================\n";

#exec
echo "\n nproc details are here " . exec('nproc');
echo "\n===================\n";

#system
$cpuinfo2 = system('cat /proc/cpuinfo');
echo $cpuinfo2;

?>

output:

Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 1 On-line CPU(s) list: 0 Thread(s) per core: 1 Core(s) per socket: 1 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 2 Stepping: 3 CPU MHz: 2299.998 BogoMIPS: 4599.99 Virtualization: VT-x Hypervisor vendor: KVM Virtualization type: full L1d cache: 32K L1i cache: 32K L2 cache: 4096K NUMA node0 CPU(s): 0 =================== nproc details are here 1 =================== processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 2 model name : QEMU Virtual CPU version 1.0 stepping : 3 microcode : 0x1 cpu MHz : 2299.998 cache size : 4096 KB fpu : yes fpu_exception : yes cpuid level : 4 wp : yes flags : fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pse36 clflush mmx fxsr sse sse2 syscall nx lm rep_good nopl pni vmx cx16 popcnt hypervisor lahf_lm bogomips : 4599.99 clflush size : 64 cache_alignment : 64 address sizes : 40 bits physical, 48 bits virtual power management
share|improve this answer
1  
Welcome to Ask Ubuntu! Please don't add comments as answers. Invest some time in the site and you will gain sufficient privileges to upvote answers you like, or to add actual comments when seeking clarification of any issues. If you have a new question please ask it. –  guntbert 3 hours ago
    
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. –  muru 1 hour ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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