Simple services
Here is the simplest possible service file that starts MyRubyScript with options that work for me on my computer. Save it as /etc/systemd/system/my_ruby_script.service :
[Unit]
Description=Virtual Distributed Ethernet
[Service]
ExecStart=/home/anil/.rvm/rubies/ruby-2.2.0/bin/ruby my_ruby_script.rb
[Install]
WantedBy=multi-user.target
Automatic restarts
Let's also add the proper dependency on the system logger and tell systemd to restart my_ruby_script_switch if it crashes due to an uncaught signal (although it never happened to me):
[Unit]
Description=My Ruby Script Demo
After=syslog.target
[Service]
WorkingDirectory=/home/anil/new_workers
ExecStart=/home/anil/.rvm/rubies/ruby-2.2.0/bin/ruby my_ruby_script.rb
Restart=on-abort
[Install]
WantedBy=multi-user.target
Systemd consists of two main concepts: a unit and a target. A unit is a configuration file that describes the properties of the process that you'd like to run. This is normally a
docker run
command or something similar. A target is a grouping mechanism that allows systemd to start up groups of processes at the same time. This happens at every boot as processes are started at different run levels.systemd is the first process started on CoreOS and it reads different targets and starts the processes specified which allows the operating system to start. The target that you'll interact with is the
multi-user.target
which holds all of the general use unit files for our containers.Each target is actually a collection of symlinks to our unit files. This is specified in the unit file by
WantedBy=multi-user.target
. Running systemctl enable foo.service
creates symlinks to the unit inside multi-user.target.wants
Click - for more detail