systemd
(all lowercase, not systemD
:) ) is an init system in Linux distros to bootstrap the user space and manage all processes subsequently.The alternatives are
- UNIX System V OS/ BSD OS init systems,
- upstart, 2006, an event-based replacement for the traditional init daemon upstart.
The recent version of ubuntu usessystemd
, as far as I remember previous versions of ubuntu use upstart. - runit, 2004
- launchd in MacOS world.
PS. There's lot of hate against
Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface.
Here's a thread which might be useful to dig.
systemd
, it being against Unix philosophy that goes as;Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface.
Here's a thread which might be useful to dig.
Fun time
I'm creating a streaming service in this example, which will simply write an event to a file continuously. And, if I restart my machine the service will start emitting events to a file right away.
Also I can
Also I can
start
or stop
service using systemctl start/stop serviceName
. I used to use nohup <command> &
and to kill it I had to use get the PID
and then kill it. I can get rid of all that crap with systemD
service.
STEP 1 - create streaming.service at
/etc/systemd/system/multi-user.target.wants/streaming.service
, also at /etc/systemd/system/streaming.service
.[Unit]
Description=Streaming pipeline
[Service]
Type=forking
ExecStart=/usr/local/bin/streaming.sh
TimeoutSec=infinity
Restart=always
[Install]
WantedBy=multi-user.target
STEP 2 - create a bash script at
/usr/local/bin/streaming.sh
and give current user permission to it. Don't forget #!/bin/bash
for the bash scripts.#!/bin/bash
while true
do
echo "Streaming an event at `date`" >> /var/log/streaming.log
done
chmod 777 /usr/local/bin/streaming.sh
STEP 3 - reload
systemd
daemon so that it loads streaming.service
and enable the servicesystemctl daemon-reload
systemctl enable streaming.service
STEP 4 - see the
streaming
job statussystemctl status streaming.service
I can use
journalctl _PID=??
to debug if the service is not working. eg.journalctl _PID=5871
-- Logs begin at Mon 2016-11-21 16:36:41 PST, end at Tue 2016-12-06 14:34:56 PST. --
Nov 26 04:17:52 y0319t10971 sshd[5871]: Connection closed by 10.16.132.191 [preauth]
Dec 06 14:33:15 y0319t10971 systemd[5871]: Failed at step EXEC spawning /usr/local/bin/streaming.sh: Exec format error
or also can use following command,
root@y0319t10971:~# journalctl -fu streaming.service
-- Logs begin at Mon 2016-11-21 16:36:41 PST. --
Dec 06 14:33:16 y0319t10971 systemd[1]: streaming.service: main process exited, code=exited, status=203/EXEC
Dec 06 14:33:16 y0319t10971 systemd[1]: Unit streaming.service entered failed state.
Dec 06 14:33:16 y0319t10971 systemd[1]: streaming.service failed.
Dec 06 14:34:28 y0319t10971 systemd[1]: Started Streaming pipeline.
Dec 06 14:34:28 y0319t10971 systemd[1]: Starting Streaming pipeline...
Dec 06 14:34:28 y0319t10971 systemd[1]: streaming.service: main process exited, code=exited, status=203/EXEC
Dec 06 14:34:28 y0319t10971 systemd[1]: Unit streaming.service entered failed state.
Dec 06 14:34:28 y0319t10971 systemd[1]: streaming.service failed.
Dec 06 14:44:58 y0319t10971 systemd[1]: Started Streaming pipeline.
Dec 06 14:44:58 y0319t10971 systemd[1]: Starting Streaming pipeline...
STEP 5 - see the events produced by the
streaming.service
tail -f /var/log/streaming.log
Streaming an event at Fri Dec 2 11:27:21 PST 2016
Streaming an event at Fri Dec 2 11:27:21 PST 2016
Streaming an event at Fri Dec 2 11:27:21 PST 2016
Streaming an event at Fri Dec 2 11:27:21 PST 2016
Streaming an event at Fri Dec 2 11:27:21 PST 2016
Streaming an event at Fri Dec 2 11:27:21 PST 2016
Streaming an event at Fri Dec 2 11:27:21 PST 2016
Streaming an event at Fri Dec 2 11:27:21 PST 2016
Streaming an event at Fri Dec 2 11:27:21 PST 2016
Streaming an event at Fri Dec 2 11:27:21 PST 2016
PS. Dont forget to stop the service, otherwise your Storage will be full in few days :)
ll /var/log/streaming.log --block-size=GB
-rw-r--r-- 1 root root 3GB Dec 6 13:51 /var/log/streaming.log
Also, the service would be running right after I reboot the machine.