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.
माझ्या क्लासमध्ये स्टुडंट्स नेहमी विचारतात: "सर, Nginx इन्स्टॉल केला, रीबूटनंतर वेबसाइट का उघडत नाही?" बहुतेक वेळा त्यांनी सर्व्हिस start केली, पण enable केली नाही. खाली फरक बघा.
मेरी क्लास में स्टूडेंट्स हमेशा पूछते हैं: "सर, Nginx इंस्टॉल किया, रीबूट के बाद वेबसाइट क्यों नहीं खुलती?" ज़्यादातर बार उन्होंने सर्विस start की, लेकिन enable नहीं की. नीचे फ़र्क देखो.
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?
Ravindra Bagale's Tip – मराठी
मित्रांनो, लक्ष द्या – service start, stop, restart, reload आणि status करू शकतो, पण reboot नंतर service आपोआप start करू शकत नाही. हे काम systemd चं आहे. म्हणून या book मध्ये तुम्हाला ही जोडी नेहमी दिसेल: sudo service nginx start (आत्ताच start) आणि sudo systemctl enable nginx (प्रत्येक boot ला start). तसेच systemctl daemon-reload (स्वतःच्या unit files नंतर) आणि journalctl (logs साठी) यांना service मध्ये पर्याय नाही, म्हणून ते आपण direct वापरतो. समजलं का?
Ravindra Bagale's Tip – हिंदी
दोस्तों, ध्यान दो – service start, stop, restart, reload और status कर सकता है, लेकिन reboot के बाद service को अपने आप start नहीं कर सकता. ये काम systemd का है. इसलिए इस book में आपको ये जोड़ी हमेशा दिखेगी: sudo service nginx start (अभी start) और sudo systemctl enable nginx (हर boot पर start). वैसे ही systemctl daemon-reload (अपनी unit files के बाद) और journalctl (logs के लिए) का service में कोई विकल्प नहीं, इसलिए उन्हें हम सीधे इस्तेमाल करते हैं. समझ आया?
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.
Ravindra Bagale's Tip – मराठी
बरेच students config बदललं की लगेच restart करतात, आणि config मध्ये चूक असेल तर website पूर्ण बंद पडते. आधी test, मग reload – && ने जोडलं तर test fail झाला की reload होतच नाही: sudo nginx -t && sudo service nginx reload.
Ravindra Bagale's Tip – हिंदी
बहुत से students config बदलते ही restart कर देते हैं, और config में गलती हो तो पूरी website बंद हो जाती है. पहले test, फिर reload – && से जोड़ा तो test fail होने पर reload होता ही नहीं: 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.