本文を読み飛ばす

systemd: enabling a user-unit service

In my experience, setting up a systemd service as a user unit can be trickier than setting it up as a system unit.

You need to pay attention to a few key points to get it working correctly.

TL;DR

At first, here is my service file.

[Unit]
Description=voice from pushbullet
#Wants=default.target
After=network.target

[Service]
Type=simple
WorkingDirectory=%h/src/pb-va
ExecStart=%h/bin/pb_va some options...
ExecStop=pkill pb-va
Restart=always

[Install]
WantedBy=default.target

Copy this file to the systemd directory and enable it::

$ mkdir -p ~/.config/systemd/user
$ cp pb-va.service ~/.config/systemd/user
$ systemctl --user daemon-reload
$ systemctl --user enable pb-va.service
$ systemctl --user start pb-va.service
$ loginctl enable-linger $USER

Do not specify the User= and Group= options

In the [Service] section, system units can use the User= and Group= options.

However, user units run under your own user account by default, so adding these options will cause the service to fail on startup.

Use User-Specific Targets like default.target

You might be used to seeing targets like multi-user.target in system-wide services.

Those targets are for system units and won't work for user units. For user services, you should almost always use WantedBy=default.target.

This is the user's equivalent and ensures your service starts after you log in.

Remember to re-enable the service and reload the daemon after changing the serfice file to apply your changes:

$ systemctl --user daemon-reload
$ systemctl --user disable pb-va.service
$ systemctl --user enable pb-va.service

Run Services Even When Logged Out (Enable Linger)

By default, user services stop when you log out.

If you want your service to start at boot and keep running, you must enable linger for your user.

$ loginctl enable-linger your-username

# then confirm the linger status.

$ loginctl list-users
 UID USER   LINGER
1001 your-username yes

続きを読む…

The ssh error: failed remote commands by stty in login shell

I used the stty command in .bashrc, it cause ssh errors with no-console.

# nothing for the output

$ ssh host echo 1234

# you can check the warning with `-v` option

$ ssh -vv host echo 1234

Pseudo-terminal will not be allocated because stdin is not a terminal.

The stty command is used to change terminal settings, so it requires a TTY to function.

When it runs in a non-interactive shell without a TTY, it fails and can cause the entire script or SSH session to exit unexpectedly.

To fix this, I commented out the stty commands in my bash::

if false; then
    stty ...
fi

Please check your login script files:

  • .profile
  • .bash_login
  • .bashrc or etc

Finally, I add the follow line before stty command to avoid the this problem.

[[ $- != *i* ]] && return
stty ...

According to man bash:

PS1 is set and $- includes i if bash is interactive,
allowing a shell script or a startup file to
test this state.

続きを読む…

systemd.mount with disk label

here are some points

  1. e2label will not work with the mounted partitions.
  2. the service file name is correspond with the Where option.
  3. check the systemctl status results for the unit file.

here is my procedure and the unit file example.

# label the partition
e2label /dev/sd?? hdd-label1

# create the unit file.
cat > /etc/systemd/system/mnt-aaa.mount <<EOF
[Unit]
Description = mount WesternDigital 1T to /mnt/aaa
After = local-fs.target

[Mount]
#What = /dev/disk/by-label/ts1t-deg
What = LABEL=wd1t-deg
Where = /mnt/aaa
Type = ext4
Options = nofail

[Install]
WantedBy = default.target
EOF

# reload and mount...
systemctl daemon-reload
systemctl status mnt-aaa.mount  # check the errors
systemctl start mnt-aaa.mount

続きを読む…

systemd: Reducing SD Card Wear with OverlayFS and Systemd

SD cards have a limited number of write cycles, making them a common failure point for SBCs like the Raspberry Pi. One of the most effective ways to prolong the life of your SD card is to use an OverlayFS to redirect write-heavy operations to a different storage medium (or RAM).

In this article, I will demonstrate how to set up persistent OverlayFS mounts using systemd and Makefile.

続きを読む…

ARM SBC: podman install

My experience in the debian armhf. To install podman, according to https://wiki.debian.org/Podman.

it is need to enable the registory at search::

sudo apt install podman
echo 'unqualified-search-registries=["quay.io"]' > /tmp/registries.conf
CONTAINERS_REGISTRIES_CONF=/tmp/registries.conf podman search podman

move cache directory to an external drive,

sudo mkdir -o /nmt/external/containers.$USER
sudo chown $USER:$USER /nmt/external/containers.$USER
sudo mount -o bind /mnt/external/containers.$USER ~/.local/share/containers

thanks podman github .

I want to build the application from a debian image.

podman pull debian:12.11-slim

build the application.

mkdir app; cd app
echo "FROM debian:12.11-slim" > Containerfile
podman build

install the network program:

sudo apt install uidmap
sudo apt install slirp4netns

thanks github issue .

then run the container:

podman run

続きを読む…

宣伝: