Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am attempting to run a command on a CentOS virtual machine, and the API that I am using to run this command does not seem to allow the >, >>, or | operators. Also, I must use fully qualified paths for commands (eg. /bin/ls instead of just ls).

I want to be able to send the output of a certain command to a file. How can I do this under these conditions? I could in theory execute a script that will do the redirect in a "proper" shell, but that's a pretty inconvenient option here.

share|improve this question
    
What shell are you running? It sounds horribly broken or misconfigured. –  jw013 Jul 25 '12 at 15:40
    
yum -y install bash, chsh -s /bin/bash, profit? –  Tim Jul 25 '12 at 15:43
    
I'm executing a command to run a program in a guest VM using the vmrun command in the VMware VIX API. –  smcg Jul 25 '12 at 15:43
1  
I am pretty sure the API doesn't run a shell at all thats the reason you can't use the redirect operations, just execute something like /bin/sh -c "command > redirect" –  Ulrich Dangel Jul 25 '12 at 15:49

2 Answers 2

up vote 6 down vote accepted

The redirects are interpreted by the shell. If you are using an API the commands executed are probably not executed via an shell but directly (man 3 system vs. man 3 exec).

To use redirects just start a shell and use the redirects there, e.g:

/bin/sh -c "command > redirect"
share|improve this answer
1  
You might need to use /bin/sh -c 'command>redirect' if the application is simple-minded and splits the string into separate arguments at every space character. –  Gilles Jul 25 '12 at 23:07

If you have multiple VMs, you might want to look into a program called pdsh. It can be used to run commands (via ssh) on multiple remote machines (real or virtual) in parallel.

It was written for use in HPC clusters, but it's a useful sysadmin tool for working with any group of linux/unix machines.

https://computing.llnl.gov/linux/pdsh.html

e.g. if I have a bunch of machines defined with the attribute "compute" (nova-compute nodes in an openstack deployment), I can run the following command to find out how many nodes have 16 or more cores free (out of 24 cores per node) and are this capable of running an xxlarge 16-core VM:

# pdsh -g compute "ps hwwu -Ckvm | sed -e 's/.*-smp //' -e 's/,.*//' | 
awk '{VMS += 1 ; CPUS += \$1} END {print VMS, CPUS, 24-CPUS}'" | \
awk '$4 > 15 {print}' | sort -k4 -n
comp17: 6 8 16
comp19: 6 8 16
comp23: 6 8 16
comp26: 7 8 16
comp51: 7 8 16
comp56: 6 8 16
comp71: 7 8 16
comp78: 7 8 16
comp81: 6 8 16
comp11: 6 7 17
comp47: 6 7 17
comp79: 6 7 17
comp09: 5 6 18
comp29: 5 5 19
comp27: 4 4 20

(real usage output, hostnames edited for anonymity)

The command inside the double-quotes is executed on the remote machines (shell quoting rules apply, hence the \$1 in the first awk), and the remainder (the second awk and the sort) is executed on the local machine.

share|improve this answer
    
Sometimes I have to run commands on guest VMs that don't have IP addresses yet, which rules out SSH. –  smcg Jul 26 '12 at 13:17

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.