Ravindra BagaleCourses & study guides मराठी Track your progress

Chapter 4: Linux Advanced Commands

4.6 Services: service, systemctl and journalctl

Students in my class always ask: "Sir, I installed Nginx – why doesn't the website open after a reboot?" Most of the time they started the service but did not enable it. See 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

Friends, pay attention – service can start, stop, restart, reload and show status, but it cannot make a service start automatically after a reboot. That is systemd's job. That is why in this book you will always see this pair: sudo service nginx start (start now) and sudo systemctl enable nginx (start at every boot). Likewise systemctl daemon-reload (after your own unit files) and journalctl (for logs) have no alternative in service, so we use them directly. Got it?

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).

Ravindra Bagale's Tip

Many students restart immediately after changing a config, and if the config has a mistake the whole website goes down. Test first, then reload – join them with && and if the test fails the reload never happens: sudo nginx -t && sudo service nginx reload.

Lab

Chala, Nginx nasel tar sudo yum install -y nginx kara, mag sudo service nginx start aani sudo systemctl enable nginx kara aani systemctl is-enabled nginx ne enabled aala ka bagha. Mag sudo reboot karun parat login kara aani sudo service nginx status madhe active (running) aahe ka te check kara. Shevti journalctl -u nginx -n 20 --no-pager bagha.