There are different ways to control services in an OpenVZ container. Please remember that root or sudo access is required to achieve these.
Method 1: Using `service` Command
The ‘service’ command in Linux is used to start, restart, and stop services. Its use is widespread on systems that use System V init system (mostly older versions of RHEL, CentOS).
Here are the necessary commands:
1. To start a service:
\`\`\` service SERVICE\_NAME start \`\`\` Replace “SERVICE\_NAME” with the name of the service you want to start.1. To stop a service:
\`\`\` service SERVICE\_NAME stop \`\`\` Replace “SERVICE\_NAME” with the name of the service you want to stop.1. To restart a service:
\`\`\` service SERVICE\_NAME restart \`\`\` Replace “SERVICE\_NAME” with the name of the service you want to restart.Method 2: Using `systemctl` Command
The ‘systemctl’ command is used to manage systemd system and service. It’s primarily used in the most recent versions of modern Linux distributions.
These are the commands to start, stop, and restart services with systemctl:
1. To start a service:
\`\`\` systemctl start SERVICE\_NAME \`\`\` Replace “SERVICE\_NAME” with the name of the service you want to start.1. To stop a service:
\`\`\` systemctl stop SERVICE\_NAME \`\`\` Replace “SERVICE\_NAME” with the name of the service you want to stop.1. To restart a service:
\`\`\` systemctl restart SERVICE\_NAME \`\`\` Replace “SERVICE\_NAME” with the name of the service you want to restart.Please note that you need to execute the above commands inside the OpenVZ container where the specific service resides.
Method 3: Using OpenVZ commands
From the host, you can execute commands in a container using ‘vzctl exec’. The syntax is:
```
vzctl exec CTID COMMAND
```
Replace ‘CTID’ with the ID or name of your container, and ‘COMMAND’ with the command that you want to run. You can use this with the previous methods to control services inside the container:
```
vzctl exec 101 service SERVICE_NAME restart
```
or
```
vzctl exec 101 systemctl restart SERVICE_NAME
```