Writing service files in systemd

image source
image source

Systemd may not be the most popular init system out there, and it still manages to annoy me every now and then, but I had a chance to play with it at work recently. A deeper dive into it has left me a little happier with how it handles things.

My requirement was to daemonize a service and have it running at startup. The service has a few command line arguments, and also needs some environment variables set. The command line args could change but should not expect someone to reconfigure the init script. Systemd supported all these requirements out of the box.

Here is what the service file looked like:

[Unit]
 Description=myservice
[Service]
 Environment="LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH"
 EnvironmentFile=/etc/<somefile.cfg>
 ExecStart=/usr/bin/myservice ${arg1} ${arg2} ${arg3}
[Install]
 WantedBy=multi-user.target

The EnvironmentFile has name value pairs of the command line arguments:

arg1=foo
arg2=bar
arg3=baz

There are numerous others configuration parameters for the service. For instance, set Type=oneshot if the application is not a daemon and is going to exit immediately.

Use ExecStartPost for running additional scripts after the first one starts (e.g. maybe you want to write to a pidfile). ExecStartPre, ExecStopPost and ExecStopPre also exist.

The unti file must be present in /etc/systemd/system. To make it run on subsequent boots:

systemctl enable myservice

To start the service:

systemctl start myservice

To check the service’s logs:

journalctl -u myservice

Further reading:

Bash: Move between directories with ease

Bash has tons of stuff that can really make one productive on the terminal. If you just moved into a folder and want to quickly switch back to the one you came from, cd - does the job.

Here’s an example:

thaum:/etc/apache2# cd /var/log/apache2/
thaum:/var/log/apache2#
thaum:/var/log/apache2# # do something
thaum:/var/log/apache2# cd – /etc/apache2
thaum:/etc/apache2#

That works out for simple tasks, but there’s a lot more. Say hello to pushd. Bash has a directory stack that holds your current working directory. Call pushd with another path and it gets added to the stack. Repeat as many times as needed and you get a pile of directories that you can switch to immediately. popd removes the current entry from the stack.

This is how it works:


thaum:/etc/apache2# ### I want to switch between my logs and my etc now, so I start with this
thaum:/etc/apache2# pushd /var/log
/var/log /etc/apache2
thaum:/var/log# cd apache2
thaum:/var/log/apache2# ### I can go to other folders now and the stack is updated accordingly
thaum:/var/log/apache2# # do something
thaum:/var/log/apache2#
thaum:/var/log/apache2# pushd /var/www ### Add my apache root folder to the stack as well
/var/www /var/log/apache2 /etc/apache2 ### Three entries are now available!
thaum:/var/www#
thaum:/var/www# # do something else
thaum:/var/www#
thaum:/var/www# pushd +2 ### I want to go back to my /etc folder, so I call pushd with the +2 arg
/etc/apache2 /var/www /var/log/apache2
thaum:/etc/apache2#
thaum:/etc/apache2# pushd ### Without any args, it goes to the most recent directory in the stack
/var/www /etc/apache2 /var/log/apache2
thaum:/var/www#

At work I organize something like this for a set of commonly used folders in a screen session, and it really helps. It’s a bit of a bother typing pushd +2 or pushd +3 each time, so I also have the following aliases to really speed things up.


function pd { pushd $@; }
function pd2 { pushd +2 $@; }
function pd3 { pushd +3 $@; }

Now I just do a pd /path/to/file and pd2, pd3 etc to swap between them.