I've created the following systemd unit in the cloud-init file:
- path: /etc/systemd/system/multi-user.target.wants/docker-compose.service
owner: root:root
permissions: '0755'
content: |
[Unit]
Description=Docker Compose Boot Up
Requires=docker.service
After=docker.service
[Service]
Type=simple
ExecStart=/usr/local/bin/docker-compose -f /opt/data/docker-compose.yml up -d
Restart=always
RestartSec=30
[Install]
WantedBy=multi-user.target
When I try to run
sudo systemctl enable docker-compose.service
to create the symlink I get this:
Failed to execute operation: No such file or directory
However I'm sure that the file is under /etc/systemd/system/multi-user.target.wants
I had the same need, but I was working from a recipe that said to create /etc/systemd/system/unit.service and then do systemctl enable --now unit.
So I created the unit file with write_files and did the reload and enable in a text/x-shellscript part and that worked fine. (User scripts run last and in order, while I don't think there are guarantees about when the write_files key in the user-data is processed. I found out the hard way that it's before the user key so you can't set ownership to users that cloud-init creates).
I think runcmd entries are converted to user scripts and run in list order (either before or after the other user scripts), so if you don't like x-shellscript parts you can do the reload and enable that way. /var/log/cloud-init.log is where I check the order, there is probably a config file too.
Full disclosure: I forgot the systemctl daemon-reload command but it still worked. Actually there is a caveat against systemd manipulations from cloud-init because it's running under systemd itself and some systemd commands may wait for cloud-init to finish -- deadlock!
After unit file creation but before any manipulations with it systemd should be notified about the changes:
systemctl daemon-reload
So cloud-init YAML block creating docker-compose.service file should be followed by:
runcmd:
- systemctl daemon-reload
Check that every file involved is present and valid:
ls -l /etc/systemd/system/multi-user.target.wants/docker-compose.service
ls -l /usr/local/bin/docker-compose
ls -l /opt/data/docker-compose.yml
systemd-analyze verify /etc/systemd/system/multi-user.target.wants/docker-compose.service
Also consider the timing. Even if the files exist once they are fully booted, would /etc/systemd/system/multi-user.target.wants/ exist when cloud-init runs?
Related
On a Virtual Maching running on Centos7, using root privileges, I want to create a service to launch a Python (Python 3.9) webservice and restart it every hour.
Here's what i did:
creating a directory /etc/systemd/system/dlweb_doc_webservice.service.d
creating a file dlweb_db_generate_report.service like this
[Unit]
Description=dlweb_db_generate_report_webservice
After=network-online.target
Wants=network-online.target systemd-networkd-wait-online.service
[Service]
Type = simple
ExecStart=/usr/local/bin/python3.9 /opt/scripts/dlweb_db_generate_report_webservice/dlweb_db_generate_report.py
Restart=on-failure
RestartSec=5s
StartLimitIntervalSec=3600
StartLimitBurst=5
[Install]
WantedBy=multi-user.target
running systemctl daemon-reload
running systemctl --type=service --all doesn't display my service
and
running systemctl enable dlweb_db_generate_report_webservice returned a Failed to execute operation: No such file or directory
What did i miss or do wrong?
I have a cpp application which broadcasts an object and its methods on the dbus. I try to run this program at startup with the following service file:
[Unit]
Description=Running dbus program
After=network.target
[Service]
Type=simple
ExecStart=/home/my_name/Documents/dbus/build/my_app
StandardOutput=console+journal
StandardError=console+journal
[Install]
WantedBy=multi-user.target
After reloading:
systemctl daemon-reload
and running it:
sudo systemctl start my_service.service
I got no error in the journal, but I cant see anything on the dbus (running d-feet, and browsing for my object, I cant find anything)
Running the exact same ExecStart:
/home/my_name/Documents/dbus/build/my_app
in the console works fine.
What am I missing? Thanks!
As you want your service to run on the session bus you will need to use:
sudo systemctl --user start my_service.service
Putting the file into /etc/systemd/user/ location will make it available to all users still.
I wrote a program (node js, on Raspberry Pi) that I can start manually, but not as a systemd service:
pi#blueberry ~ $ systemd --version
systemd 215
+PAM +AUDIT +SELINUX +IMA +SYSVINIT +LIBCRYPTSETUP +GCRYPT +ACL +XZ -SECCOMP -APPARMOR
pi#blueberry ~ $ sudo systemctl daemon-reload
pi#blueberry ~ $ sudo systemctl start /etc/systemd/system/rfxtrx.service
Failed to start etc-systemd-system-rfxtrx.service.mount: Unit etc-systemd-system-rfxtrx.service.mount failed to load: No such file or directory.
pi#blueberry ~ $
The error message complains that there is no rfxtrx.service.mount unit. Correct. Should there be such unit? The most common suggestion is to daemon-reload; this doid not help. Or as per https://github.com/systemd/systemd/issues/5375 this could be a bug in systemd that was fixed but only in a later systemd version than the one in raspbian (raspbian has version 215).
Is there any other solution than trying to update to a version not supported by the raspbian maintainers?
Well the first problem here is that you're running to start the service name rfxtrx.service however systemd is expecting etc-systemd-system-rfxtrx.service.mount.
If you are trying to have a systemd mount configuration then your file name should follow the following rule:
Mount units must be named after the mount point directories they
control. Example: the mount point /home/lennart must be configured in
a unit file home-lennart.mount.
So if you were wanting to create a mount point at say /dir/to/rfxtrx then the systemd mount file needs to be named dir-to-rfxtrx.mount, and it's recommended that it sits in either /usr/lib/systemd/system/ or /etc/systemd/system/ with the latter directory taking precedence.
If you just wanted to have a service file then enable the unit systemctl enable rfxtrx.service. systemctl daemon-reload is used when the unit has already been registered with systemd and requires a reload.
You can check if the service exists with systemd by using the command systemctl list-units or systemctl status rfxtrx.service.
The error you have is that you're doing sudo systemctl start /etc/systemd/system/rfxtrx.service instead of sudo systemctl start rfxtrx.service.
I need to copy a bunch of files into some folder every time the httpd service restarts
e.g.:
yes | cp ./dynamic/*.file /folder/inside/my/webapp
is there a way to run some additional commands when the httpd is restarted?
According to this: https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units
You can edit the httpd service file with:
sudo systemctl edit --full httpd.service
You should be able to add more directives to the service unit, such as running your additional commands.
I have a DO droplet running Ubuntu 16.04.1x64 and I'm trying to run IPFS as a systemd service. I've gone ahead and created a user "connor" and installed IPFS following the instructions here. I'm storing the service as "ipfs.service" in ~/.config/systemd/user/ipfs.service which looks like this:
[Unit]
Description=IPFS Daemon
[Service]
Type=simple
ExecStart=/usr/local/bin/ipfs daemon
ExecStop=/usr/bin/pkill ipfs
Restart=always
User=Connor
[Install]
WantedBy=default.target
What's odd is that if I run systemctl --user start ipfs it starts up just fine. However, running systemctl --user daemon-reload and then
systemctl --user enable ipfs I get the error:
Failed to execute operation: No such file or directory
However, if I run systemctl enable /home/connor/.config/systemd/user/ipfs.service -f it runs just fine. I can reboot and run IPFS commands just fine. I'd like to run it as a user though, and would also like to understand what I'm doing wrong.
Please, check that you are executing the commands with connor user, you may run whoami to see the user executing the command. (running the command with sudo changes the user to root)
In addition, I see that the user in the service file is capitalized (Connor instead of connor), this could bring other problems, and it is not needed, as a simple configuration like the one proposed by Arch Linux wiki works for user daemons.
Please find bellow the configuration I used for my ipfs daemon, (without User= and with a different Restart=, since Restart=always gave me problems while starting the daemon):
[Unit]
Description=IPFS daemon
After=network.target
[Service]
ExecStart=/usr/local/bin/ipfs daemon
Restart=on-failure
[Install]
WantedBy=default.target