Start, Stop, and Restart Services with Shell Scripts

Managing processes on a Unix/Linux system using shell scripts is a common task, and there are several commands and operations available for this purpose. Some examples are:

Start a service

You can use the “systemctl” command to start a service on the system. For example, the following command starts the Apache service:

systemctl start apache2


Stopping a service

You can use the “systemctl” command to stop a service on the system. For example, the following command for the Apache service:

systemctl stop apache2


Restart a service

You can use the “systemctl” command to restart a service on the system. For example, the following command restarts the Apache service:

systemctl restart apache2


List Services

You can use the “systemctl” command to list the services available on the system. For example, the following command lists all available services on the system:

systemctl list-unit-files –type=service


Checking the status of a service

You can use the “systemctl” command to check the status of a service on the system. For example, the following command checks the status of the Apache service:

systemctl status apache2


Kill a process

You can use the “kill” command to kill a running process. For example, the following command terminates the process with ID 123:

kill 123


Check running processes

You can use the “ps” command to list the processes running on the system. For example, the following command lists all processes running on the system:

ps -ef