
Operations on Virtual Private Servers
41
Running Commands in Virtual
Private Server
Usually, a Virtual Private Server administrator logs in to the VPS via network and executes any
commands in the VPS as on any other Linux box. However, you might need to execute
commands inside Virtual Private Servers bypassing the normal login sequence. This can happen
if:
You do not know the Virtual Private Server login information, and you need to run some
diagnosis commands inside the VPS in order to verify that it is operational.
Network access is absent for a Virtual Private Server. For example, the VPS administrator
might have accidentally applied incorrect firewalling rules or stopped SSH daemon.
OpenVZ allows you to execute commands in a Virtual Private Server in these cases. Use the
vzctl exec
VPS_ID
command for running a command inside the VPS with the given ID.
The session below illustrates the situation when SSH daemon is not started:
# vzctl exec 101 /etc/init.d/sshd status
sshd is stopped
# vzctl exec 101 /etc/init.d/sshd start
Starting sshd:[ OK ]
# vzctl exec 101 /etc/init.d/sshd status
sshd (pid 26187) is running...
Now VPS users can log in to the VPS via SSH.
When executing commands inside a Virtual Private Server from shell scripts, use the
vzctl
exec2
command. It has the same syntax as
vzctl exec
but returns the exit code of the
command being executed instead of the exit code of
vzctl
itself. You can check the exit code
to find out whether the command has completed successfully.
If you wish to execute a command in all running VPSs, you can use the following script:
# for i in `vzlist –o veid -H`; do \
echo "VPS $i"; vzctl exec $i <command>; done
where
<command>
is the command to be executed in all the running VPSs. For example:
# for i in `vzlist –o veid -H`; do\
echo "VPS $i"; vzctl exec $i uptime; done
VPS 101
2:26pm up 6 days, 1:28, 0 users, load average: 0.00, 0.00, 0.00
VPS 102
2:26pm up 6 days, 1:39, 0 users, load average: 0.00, 0.00, 0.00
[The rest of the output is skipped...]