Ravindra BagaleCourses & study guides

Chapter 4: Linux Advanced Commands

4.6 Services: service, systemctl and journalctl

In my sessions, students often ask: "Sir, I installed Nginx, why is the website not opening after reboot?" Usually they started the service but never enabled it. Bagha the difference below.

Modern Linux uses systemd to start and manage background services (daemons) such as nginx, sshd, mariadb. The real tool that talks to systemd is systemctl. In class I teach the simpler, older service command first, because it reads like plain English — service nginx start — and it works the same on Ubuntu, Amazon Linux and CentOS. Behind the scenes, on these systems service simply calls systemctl for you.

Day-to-day control with service

Command Purpose What runs underneath
sudo service nginx start Start now systemctl start nginx
sudo service nginx stop Stop now systemctl stop nginx
sudo service nginx restart Stop + start systemctl restart nginx
sudo service nginx reload Re-read config without dropping connections systemctl reload nginx
sudo service nginx status Is it running? Last log lines (press q to exit) systemctl status nginx
service --​status-​all List services and their state (most useful on Ubuntu) —

Things only systemctl can do

Command Purpose
sudo systemctl enable nginx Start automatically at every boot
sudo systemctl disable nginx Do not start at boot
systemctl is-​active nginx / systemctl is-​enabled nginx Quick yes/no checks (handy in scripts)
systemctl list-​units --​type=​service --​state=​running All running services
sudo systemctl daemon-​reload Reload systemd after creating or editing a .service unit file

Ravindra Bagale's Tip: why I still write systemctl enable

Mitranno, dhyan do — service can start, stop, restart, reload and show status, but it cannot make a service start automatically after a reboot. That job belongs to systemd itself. So throughout this book you will see this pair: sudo service nginx start (start it now) and sudo systemctl enable nginx (start it at every boot). Similarly, systemctl daemon-reload (after writing your own unit files) and journalctl (for logs) have no service equivalent, so we use them directly. Samjla ka?

service: command not found?

service is present by default on Ubuntu, Amazon Linux 2023 and CentOS Stream 9 AMIs. If a very minimal image lacks it, install it: sudo yum install -y initscripts on Amazon Linux 2023, or sudo yum install -y initscripts-service on CentOS Stream 9.

Logs with journalctl

systemd stores service logs in its journal; read them with journalctl (there is no service equivalent):

journalctl -u nginx                 # all logs of the nginx service
journalctl -u nginx -f              # follow live (Ctrl+C to stop)
journalctl -u nginx --since "1 hour ago"
journalctl -p err -b                # only errors since last boot
journalctl -u sshd -n 30 --no-pager # last 30 lines (service is 'ssh' on Ubuntu)
sudo journalctl --disk-usage

Logs on Amazon Linux 2023

Amazon Linux 2023 does not install rsyslog by default, so /var/log/messages and /var/log/secure may not exist. Use journalctl instead (or install rsyslog).