Systemd
vim /lib/systemd/system/foo.service
chmod +x /lib/systemd/system/myservice.service
[Unit]
Description=foo
[Service]
ExecStart=/bin/bash echo "Hello World!"
[Install]
WantedBy=multi-user.target
systemctl start foo
You can also use systemctl cat nginx.service to simply view how the init script starts the service
To enable and start a service in the same line you can do
systemctl enable --now foo.service
To check if service is enabled
systemctl is-enabled foo.service; echo $?
To check the services that start with the OS in order you can do
systemctl list-units --type=target
Journalctl
List failed services
systemctl --failed
journalctl -p 3 -xb
To filter only 1 service you will need to use the flag -u
journalctl -u nginx.service
To have live logs on a service you can do
journalctl -f _SYSTEMD_UNIT=nginx.service
To have live-tail logs for 2 services example nginx and ssh
journalctl -f _SYSTEMD_UNIT=nginx.service + _SYSTEMD_UNIT=sshd.service
To check logs since the latest boot:
journalctl -b
To get the data from yesterday
journalctl --since yesterday
#or
journalctl -u nginx.service --since yesterday
To view kernel messages
journalctl -k