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

Wednesday 20 June 2012

Vim formatting goodness

So I wanted to paste this licence into my piece of code http://www.opensource.org/licenses/bsd-license.php

But I wanted it be wrapped to no more than  79 characters a line and before each sentence I wanted to insert a "# "

This is how

:set paste (paste text from web browser)
highlight area with shift V

! fmt -77 (keep 2 characters for "# ")
s!^!# !

Love it!

Wednesday 13 June 2012

Listing all available package versions with yum

Sometime I needs to install an older flavour of a package.  You can list these with yum and install a specific version, e.g.

yum --showduplicates list mod_ssl
yum install mod_ssl-2.2.3-53.el5

Thursday 31 May 2012

Tracing Redirects with curl

I quite often need to debug rewrites and redirects and with curl this is quite easy.

curl -I -L "http://somewhere.com"

also you can add the -k option to ignore invalid certificate errors

Wednesday 25 January 2012

SSH Piping between remote servers

SSH is without doubt my favourite command line tool. Today I needed to transfer a file between two hosts that I could SSH to, but could not SSH to each other. This is really simple with a bit of standard UNIX piping.

ssh hostA cat somescript.sh | ssh hostB "cat > somescript.sh"

Job done.

Thursday 19 January 2012

iproute2 cheat sheet

Ok so apparently ifconfig is either deprecated or old fashioned and your supposed to use the 'ip' command instead. Turns out it's really nice and I prefer it to ifconfig,route,already.

Here's some examples:

ip addr - equivalent to ifconfig
ip addr show eth0 - same as above just show eth0

ip ro - show routing table
ip route add default via 192.168.0.254 - set the default gateway
ip addr add 192.168.0.5/24 brd + dev eth0 label eth0:0 - set an ip alias on eth0 and call it eth0:0

ip neigh - show the arp table



Monday 9 January 2012

Disable a yum repository while running yum update

I wanted to run all updates from the RHEL repositories, but not the rpmforge and WAN Disco ones. Easy.

sudo yum check-update --disablerepo=rpmforge --disablerepo=WANdisco-dev