A malfunctioning service in Linux can be restarted using systemctl, which also shows its current status. If you’re wondering how to restart service in linux, the process is straightforward once you understand the core commands. This guide covers everything from basic restarts to troubleshooting common issues.
Linux services are background processes that keep your system running smoothly. When they fail, knowing the right restart method saves time and prevents downtime. Let’s dive into the practical steps.
Understanding Linux Service Management
Linux uses init systems to manage services. The most common one today is systemd, but older systems might use SysVinit or Upstart. Most modern distributions like Ubuntu, CentOS, and Debian rely on systemd.
Services can be network daemons, web servers, database engines, or system utilities. Restarting them applies configuration changes or recovers from errors.
Checking Your Init System
Before restarting, confirm which init system your Linux distro uses. Run this command in your terminal:
ps -p 1 -o comm=
If it returns “systemd,” you’re using systemd. If it shows “init” or “upstart,” you have an older system. Most modern guides focus on systemd.
How To Restart Service In Linux Using Systemctl
The systemctl command is the primary tool for managing services under systemd. Here’s the exact syntax:
sudo systemctl restart service_name
Replace “service_name” with the actual service, like nginx, apache2, or sshd. You need sudo privileges for most service operations.
Step-By-Step Restart Process
- Open a terminal window
- Type
sudo systemctl restart sshd(example for SSH service) - Enter your password when prompted
- No output means success
- Verify with
sudo systemctl status sshd
The status command shows whether the service is active, inactive, or failed. Look for “active (running)” in green text.
Common Service Names
- Web server: nginx, apache2, httpd
- Database: mysql, mariadb, postgresql
- SSH: sshd, ssh
- Networking: networking, network-manager
- Firewall: ufw, firewalld
If you’re unsure of the exact name, use systemctl list-units --type=service to see all active services.
Restarting Services On Older Systems
For SysVinit systems, the command differs. Use the service command instead:
sudo service service_name restart
For example: sudo service apache2 restart. This works on Ubuntu versions before 15.04 and some older enterprise distros.
Some systems also support /etc/init.d/service_name restart as an alternative. Both achieve the same result.
Restarting With Systemctl Reload
Sometimes you don’t need a full restart. The reload option applies configuration changes without stopping the service:
sudo systemctl reload nginx
This is faster and avoids downtime. Not all services support reload, but web servers often do. Check with systemctl status service_name for supported operations.
Handling Failed Services
When a service fails to restart, you’ll see error messages. Common issues include:
- Permission denied
- Port already in use
- Configuration file syntax errors
- Missing dependencies
Use journalctl -u service_name to view detailed logs. This command shows the service’s recent log entries, helping you identify the root cause.
Forcing A Restart
If a service hangs, you can force it:
sudo systemctl kill service_name
sudo systemctl restart service_name
The kill command sends a SIGTERM signal. For stubborn services, add --signal=SIGKILL to the kill command.
Restarting Multiple Services
You can restart several services at once by listing them:
sudo systemctl restart nginx php7.4-fpm mysql
This is useful after updating multiple components. Services restart sequentially, so order matters if one depends on another.
For a list of all services, use a loop:
for service in nginx php7.4-fpm mysql; do sudo systemctl restart $service; done
Automating Service Restarts
Systemd can automatically restart failed services. Edit the service unit file:
sudo systemctl edit service_name
Add these lines:
[Service]
Restart=on-failure
RestartSec=5
This tells systemd to restart the service if it fails, with a 5-second delay. Save and reload the daemon: sudo systemctl daemon-reload.
Cron Jobs For Scheduled Restarts
Some admins schedule regular restarts using cron. Edit the crontab:
sudo crontab -e
Add a line like:
0 3 * * * /usr/bin/systemctl restart nginx
This restarts nginx daily at 3 AM. Use with caution as it may cause brief downtime.
Restarting Services Without Root
Standard users can’t restart services by default. However, you can configure sudoers to allow specific commands:
username ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart nginx
Add this line to /etc/sudoers using visudo. The user can then run sudo systemctl restart nginx without a password.
For non-systemd systems, use the service command path instead.
Restarting Network Services
Network services require special attention. Restarting networking can disconnect your SSH session. Always restart network services locally or with a backup connection.
For NetworkManager:
sudo systemctl restart NetworkManager
For systemd-networkd:
sudo systemctl restart systemd-networkd
Test connectivity after restart with ping google.com.
Common Pitfalls And Solutions
Here are frequent issues users face when restarting services:
Service Name Not Found
If systemctl says “Unit not found,” check the spelling. Service names are case-sensitive. Use systemctl list-unit-files | grep -i keyword to find the correct name.
Permission Denied
You need root or sudo access. If you get permission errors, ensure your user is in the sudo group. Run groups to check.
Dependency Failures
Some services depend on others. For example, Apache may need network.target. Check dependencies with systemctl list-dependencies service_name.
Using Systemctl Status Effectively
After restarting, always verify the service state:
sudo systemctl status service_name
The output shows:
- Loaded: unit file location
- Active: running, dead, or failed
- Main PID: process ID
- Tasks: number of threads
- Memory: RAM usage
- CGroup: control group details
Look for “active (running)” in green. If it shows “failed,” check the logs immediately.
Restarting Services In Containers
In Docker containers, service management works differently. Use docker restart container_name instead. Inside the container, you might not have systemd available.
For LXC containers, use lxc-attach to run systemctl commands from the host.
Scripting Service Restarts
Automate restarts with bash scripts. Here’s a simple example:
#!/bin/bash
SERVICES=("nginx" "php7.4-fpm" "mysql")
for svc in "${SERVICES[@]}"; do
if systemctl is-active --quiet "$svc"; then
echo "Restarting $svc..."
sudo systemctl restart "$svc"
else
echo "$svc is not running, starting it..."
sudo systemctl start "$svc"
fi
done
Save as restart_services.sh, make executable with chmod +x, and run with sudo.
Monitoring Service Health
Proactive monitoring prevents unexpected failures. Use tools like Monit, Nagios, or simple cron checks:
#!/bin/bash
if ! systemctl is-active --quiet nginx; then
sudo systemctl restart nginx
echo "Nginx restarted at $(date)" >> /var/log/service_restarts.log
fi
This script checks every minute via cron and restarts if needed.
When To Restart Vs. Reload
Choose wisely:
- Restart: Full stop and start. Use for major config changes or crashes.
- Reload: Graceful config reload. Use for minor tweaks without downtime.
- Restart: Clears all connections and caches.
- Reload: Maintains active connections.
For production servers, prefer reload when possible.
Restarting Services On Boot
To enable a service to start automatically:
sudo systemctl enable service_name
To disable:
sudo systemctl disable service_name
This doesn’t affect the current state; it only changes boot behavior.
Advanced Systemctl Options
Explore these useful flags:
--now: Start or stop immediately with enable/disable--no-block: Don’t wait for operation to complete--quiet: Suppress output--fail: Return non-zero exit code on failure
Example: sudo systemctl enable --now nginx enables and starts in one command.
Restarting Services With Ansible
For multiple servers, use Ansible:
- name: Restart nginx
service:
name: nginx
state: restarted
This works on both systemd and SysVinit systems automatically.
Frequently Asked Questions
What Is The Difference Between Restart And Reload In Linux?
Restart stops and starts the service completely, closing all connections. Reload sends a signal to re-read configuration files without interrupting active connections.
Can I Restart A Service Without Sudo?
No, by default only root can manage system services. You can configure sudoers to allow specific users to restart specific services without a password.
Why Does My Service Fail To Restart?
Common reasons include syntax errors in configuration files, port conflicts, missing dependencies, or permission issues. Check journalctl -u service_name for details.
How Do I Restart All Services At Once?
You can list multiple services in one command: sudo systemctl restart service1 service2 service3. Or use a script to loop through a list.
Is It Safe To Restart Services During Production Hours?
It depends on the service. Critical services like databases or web servers may cause downtime. Schedule restarts during maintenance windows or use reload when possible.
Conclusion
Mastering how to restart service in linux is essential for system administration. Whether you use systemctl, service, or custom scripts, the key is understanding your init system and service dependencies. Always verify the status after restarting and monitor logs for issues. With practice, these commands become second nature, keeping your Linux system stable and responsive.
Remember to test restarts in a staging environment first. Automate where possible, but always have a fallback plan. Your services will thank you.