Resolve systemd alias (service name) - linux

Systemd supports aliases. For example "httpd.service"
ls -l /etc/systemd/system/httpd.service
/etc/systemd/system/httpd.service -> /usr/lib/systemd/system/apache2.service
Content of this file:
[Unit]
Description=The Apache Webserver
...
[Install]
WantedBy=multi-user.target
Alias=httpd.service apache.service
I would like to resolve the alias in a script.
Example:
If the input is "httpd.service", then the output should be "apache2.service"
I can use shell or python for this script.
How to do this the systemd-way?
Reading the symlink might work, but I guess there is a more official way to resolve the alias.

You should ask for the Id property of the aliased service
> systemctl show -p Id --value httpd.service
apache2.service
You can also query the Names property
> systemctl show -p Names --value httpd.service
httpd.service apache2.service

Related

ubuntu 18 custom service does not load

ubuntu 18
I have created a custom service at /etc/systemd/system/mycustomservice.service
and enable it : sudo systemctl enable /etc/systemd/system/mycustomservice.service
but the service does not load at start up, the content is:
[Unit]
After=mysql.service
[Service]
ExecStart=/home/myuser/runupdate.sh
[Install]
WantedBy=default.target
I try to execute the file /home/myuser/runupdate.sh without any issue
the permission of
/home/myuser/runupdate.sh is -rwxr--r--
/etc/systemd/system/mycustomservice.service is -rw-rw-r--
Please advise, thank you!
Systemd will need to know how to execute the script and what shell to use, hence there are two options Add:
#!/bin/bash
or
#!/bin/sh
to the top line of the script depending on the shell you are using. Alternatively, you can use:
ExecStart=/bin/bash -c /home/myuser/runupdate.sh

How to start Greeplum on boot - linux

Trying to start Greenplum on system startup. Please find systemd service file content below.
[Unit]
Description=Greenplum daemon
[Service]
EnvironmentFile=/etc/environment
EnvironmentFile=/etc/default/greenplum
User=gpadmin
Group=gpadmin
Type=simple
ExecStartPre=/bin/bash -c "source /opt/greenplum-db-6-6.11.2/greenplum_path.sh"
#ExecStartPre=/opt/greenplum-db-6-6.11.2/greenplum_path.sh
ExecStart=/opt/greenplum-db-6-6.11.2/bin/gpstart -a -l /home/gpadmin/gpAdminLogs -d /greenplum/master/gpseg-1
Restart=on-failure
RestartSec=5s
PrivateTmp=true
[Install]
WantedBy=multi-user.target
All required environment variables are loaded and required path is sourced still not able to start service. Getting "ImportError: No module named gppylib.mainUtils". After machine is started if I run start command(/opt/greenplum-db-6-6.11.2/bin/gpstart -a -l /home/gpadmin/gpAdminLogs -d /greenplum/master/gpseg-1) it is working fine. I am not able to understand what is going wrong. My current setup has two hosts(master and segment host). I have kept segment node up and trying on master. Any help is much appreciated.
Sourcing greenplum_path.sh in ExecStartPre won't work because the environment isn't preserved between ExecStartPre and ExecStart. I would try something like
ExecStart=/bin/bash -c "source /opt/greenplum-db-6-6.11.2/greenplum_path.sh; gpstart -a -l /home/gpadmin/gpAdminLogs -d /greenplum/master/gpseg-1.

Change domain "local" in avahi and and use a hostname with multiple labels

I need to publish multiple addresses for the same IP (eg: address1.local, address2.local)
Did it creating a service /etc/systemd/system/avahi-alias#.service with the following:
[Unit]
Description=Publish %I as alias for %H.local via mdns
[Service]
Type=simple
ExecStart=/bin/bash -c "/usr/bin/avahi-publish -a -R %I $(avahi-resolve -4 -n %H.local | cut -f 2)"
[Install]
WantedBy=multi-user.target
Then if I run:
sudo systemctl enable --now avahi-alias#address1.local.service
sudo systemctl enable --now avahi-alias#address2.local.service
It works perfect.
But now if I need to extend it to publish eg: address1.something.otherdomain I have error publishing.
Tried different ways, changing the avahi-daemon.conf for example and didn't work for me.
I will appreciate any help you can give me.
Kind regards
EDITED:
Using this solution: https://github.com/george-hawkins/avahi-aliases-notes
I can handle the use of multiple labels on the cname. But still without being able to change ".local" by something else.

Could not edit systemd service file

Need to edit following entries:
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
in /lib/systemd/system/docker.service file
$ sudo -E systemctl edit docker.service
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2376 --containerd=/run/containerd/containerd.sock
did not update the service file after restart(sudo systemctl restart docker.service)
Edit
On AWS EC2, below is the issue:
$ nano /etc/systemd/system/docker.service.d/override.conf
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
/lib/systemd/system/docker.service still shows unmodified
1) What is the recommended approach to edit service file(docker.service)?
2) Why /lib/systemd/system/docker.service cannot be edited directly?
You need to create a systemd drop-in file for docker.service.
Create /etc/systemd/system/docker.service.d/override.conf file with contents
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2376 --containerd=/run/containerd/containerd.sock
Reload systemd unit files.
systemctl daemon-reload
NOTE: Reloading systemctl daemon is required after editing any systemd unit file that's as per systemd design. For more info check this.
Restart docker daemon.
systemctl restart docker
You need to restart docker daemon to pick up the latest updated systemd unit file.
For more info check this.

systemd service file and CapabilitiesBoundingSet

I am trying to reduce the root user capabilities by using the CapabilityBoundingSet option in my service file. Anyway, it seems I cannot prevent root from writing a file.
For example, with this service file:
$ cat test.service
[Unit]
Description=Test
After=basic.target
[Service]
ExecStart=/bin/sh -c "echo 172 > /target"
CapabilityBoundingSet=CAP_DAC_READ_SEARCH
so, if I have this original file:
$ cat /target
I am the original file
$ systemctl start test.service
$ cat /target
172
$ whoami
root
My kernel version is 3.1.10.
I have also tried with an empty set, or other capabilities, but is not working.. what could be wrong?
My problem was simple: the file I was trying to modify is owned by root, and this is why I am able to perform the change. If I change the owner, then I am no more allowed to modify it.

Resources