Home > OS >  "cassandra.service" fails when I try to create a "systemd" service for it
"cassandra.service" fails when I try to create a "systemd" service for it

Time:12-23

I am trying to create a systemd cassandra.service. I have downloaded binary codes and installed it on Ubuntu 22.04 within home/a/dons/cassandra directory. Then tried to create the following file /etc/systemd/system/cassandra.service that I founded on the internet :

[Unit]
Description=Cassandra Cluster Node Daemon

[Service]
Type=forking
User=cassandra
ExecStartPre=/usr/bin/echo "Starting Cassandra Daemon"
ExecStart=/home/a/dons/cassandra/bin/cassandra
ExecStartPost=/usr/bin/echo "Cassandra Daemon Running"

ExecStopPost=/usr/bin/rm -rf /data/cassandra/saved_caches;/usr/bin/echo "Cassandra Daemon Stopped"

[Install]
WantedBy=default.target

Then I tried sudo systemctl daemon-reload and after that I tried sudo systemctl status cassandra but the result is:

× cassandra.service - Cassandra Cluster Node Daemon
     Loaded: loaded (/etc/systemd/system/cassandra.service; disabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Tue 2022-12-20 18:55:41 PST; 1min 17s ago
    Process: 477388 ExecStartPre=/usr/bin/echo Starting Cassandra Daemon (code=exited, status=217/USER)
    Process: 477389 ExecStopPost=/usr/bin/rm -rf /data/cassandra/saved_caches;/usr/bin/echo Cassandra Daemon Stopped (code=exited,>
        CPU: 4ms

Dec 20 18:55:41 a systemd[1]: Starting Cassandra Cluster Node Daemon...
Dec 20 18:55:41 a systemd[477388]: cassandra.service: Failed to determine user credentials: No such process
Dec 20 18:55:41 a systemd[477388]: cassandra.service: Failed at step USER spawning /usr/bin/echo: No such process
Dec 20 18:55:41 a systemd[1]: cassandra.service: Control process exited, code=exited, status=217/USER
Dec 20 18:55:41 a systemd[1]: cassandra.service: Control process exited, code=exited, status=217/USER
Dec 20 18:55:41 a systemd[1]: cassandra.service: Failed with result 'exit-code'.
Dec 20 18:55:41 a systemd[1]: Failed to start Cassandra Cluster Node Daemon.

CodePudding user response:

The problem is user cassandra probably doesn't exist on your system.


Here's a simple example showing the same result on my machine (where user noexist doesn't exist):

$ systemctl cat simple.service
# /etc/systemd/system/simple.service
[Service]
User=noexist
ExecStart=/bin/true

$ sudo systemctl start simple.service
$ sudo systemctl status simple.service
× simple.service - A simple service
     Loaded: loaded (/etc/systemd/system/simple.service; static)
     Active: failed (Result: exit-code) since Wed 2022-12-21 09:41:08 CET; 5s ago
   Duration: 413us
    Process: 3314 ExecStart=/bin/true (code=exited, status=217/USER)
   Main PID: 3314 (code=exited, status=217/USER)
        CPU: 0

Dec 21 09:41:08 systemd[1]: Started simple.service.
Dec 21 09:41:08 systemd[1]: simple.service: Main process exited, code=exited, status=217/USER
Dec 21 09:41:08 systemd[1]: simple.service: Failed with result 'exit-code'.

The important part here is the status=217/USER. man systemd.exec says:

Exit Code Symbolic Name Description
217 EXIT_USER Failed to determine or change user credentials, or to set up user namespacing. See User=/PrivateUsers= above.

I think the issue is related to your installation. Usually you don't install packages to /home. Who owns /home/a/dons/cassandra? This is probably the user that you should run this as. I would often guess it is user a, but on shared systemd with lots of mounts, it might be user dons.

I would normally install packages via a package manager (i.e. apt install). This contains scripts which will set up any required users for you, and set up your services.

I did a quick search and found that a .deb package does exist for cassandra:

https://cassandra.apache.org/doc/latest/cassandra/getting_started/installing.html#installing-the-debian-packages


If no package was available, I would often try to make a package out of it. The advantage of a package is that it makes it easier to uninstall or upgrade your software. This is pretty intense and is beyond the scope of this answer.


If you don't want to make a package, you could

  1. build from source and sudo make install DESTDIR=/usr/local. This will link to dependencies on your machine already. It sounds like you want to use pre-compiled binaries (those probably won't work against the exact versions of the dependencies you have installed).
  2. Install pre-compiled binaries to /opt/cassandra. Opt is appropriate for add-on, relocatable applications that can't be merged with your main package.

You sound like you want to go with (2) and just install binaries without compiling for your system.

Once the binaries/config files are in place, they should be owned by root and world readable/executable. This means any user can run the software, but only root can change it. This prevents cassandra from replacing itself with something malicious.

However cassandra may need to write somewhere. You'll have to figure out what directories it needs. It might be relative to the binary, or could be relative to a working directory, or could be an absolute path. Given the opportunity, I would probably point this to /var/lib/cassandra, but /opt/cassandra/var is reasonable too. Make sure that directory exists.

Next, create system user cassandra so you don't need to run this as your primary user, or as root.

sudo adduser --system cassandra

Now make sure cassandra has write access to the data directory:

sudo chown -R cassandra /opt/cassandra/var

If you're having troubles defining where any var directories should exist, then I strongly recommend installing the *.deb package. All of that is already done for you in the deb package.

  • Related