Thursday 15 November 2012

Life with systemd Part 2

systemd rocks!  I really did not think I had the patience to learn it, but now I understand it and have played about with it, I think it is brilliant if not for one reason alone - creating service files are trivial, which is how it should be.  In SysVInit I rarely bothered.

Here's one I knocked up for icecast.  There was already a rc script for it running under under systemd but I decided to create my own systemd service file as a test.  To install was was simply a matter of:-
systemctl stop icecast.service 
systemctl disable icecast.service
cp icecast.service /lib/systemd/system/icecast.service,  
and then activating it again with systemctl enable icecast.service 

systemd realised it had a service file for it and uses that rather that the installed /etc/rc.d/icecast file which I could leave installed.  I'm impressed.

Here's the icecast.service file I created.  I had to uncomment the pid file option in icecast.xml.  I also changed the path to /var/run


[Unit]
Description=Icecast 2.x Server

[Service]
ExecStart=/usr/bin/icecast -b -c /etc/icecast.xml
Type=forking
PIDFile=/var/run/icecast.pid

[Install]
WantedBy=multi-user.target

Life With systemd Part1

I rolled out a new server recently and very nearly did not put opensuse on it based and the thought of having to learn systemd.  I can see why SysVInit could have done with being replaced but I didn't really see it as pressing.  Anyway, here's my braindump of learning it so far!

First thing I dislike is that it pages to $PAGER by default.  Let's turn that off

export SYSTEMD_PAGER='' (I've added this to my ~/.zshrc)

ls -l /etc/systemd/system/default.target - What is the default target? (usually a symlink to another target)

systemctl show -p Wants default.target - What does this target load (want and can fail)

systemctl show -p Requires default.target - What does this target load (want and can't fail)

systemctl show -p MainPID icecast.service - Show the main pid of the process (0 if not running)

systemctl show -p icecast.service - Show all systemd values for icecast.service

systemctl {start,stop,restart} something.service - Like service something {start,stop.restart}

systemd-analyze blame - How long did each unit take to start

systemctl list-unit-files  - list all available systemd unit files

systemctl list-unit - list all loaded units

filter the above 2 outputs e.g.

systemctl list-units --type=service or  

systemctl --type=service - show running services

systemctl --type=target - show running targets

Wednesday 14 November 2012

Fun with lsof

lsof (list open files) is a tool I have used recently for discovering which service is using a certain port.  We also had a issue recently where our /var partition was showing 0 bytes free, but du -csh /var was showing over 1.5GB free.  Using lsof | grep deleted we were able to determine that processes were still holding files open that had since been (erroneously) deleted.  Here are some more examples of lsof that I like.

sudo lsof -i:22 - List all open internet sockets using port 22 (who is using port 22)

sudo lsof -i4 -sTCP:LISTEN - List all IPv4 internet sockets (-i4) in state Listen (-sTCP:LISTEN) This is should list all listening services (not working on centos 5)

sudo lsof -i4 -sTCP:LISTEN -P - As above but don't lookup service names

sudo lsof -u icecast - List all open files used by user icecast (-u icecast)

sudo lsof -u icecast -a -i List all open files used by user icecast and (-a) only internet sockets (-i)

sudo lsof -p 3893 - List all open files of pid 3893

sudo lsof -P -c ushare -a -i - List open files for processes executing a command beginning with "ushare" (-c) and (-a) internet sockets.  Don't lookup the service in /etc/services(-P)

sudo lsof -P -c ushare -a -i -sTCP:LISTEN As above but only the the listening ports for TCP

sudo lsof -i4 -sTCP:LISTEN -a -p 3792 -P - Show listening IP4 TCP ports for process 3792