Before a daemon process is to be executed by systemd, I need to create or change the destination of a symlink. The daemon process redirects its standard output to this symlink and therewith into a special file for each started daemon process. That is the idea. I set up the service unit file named "test_start.service":
[Unit]
Description=Test Server
After=network-online.target test_start.socket
Requires=network-online.target test_start.socket
[Service]
User=test1
Group=test1
Type=simple
ExecStartPre=/bin/bash /etc/testServer/Debug/makeOutfile.sh
ExecStart=/etc/testServer/Debug/testServer
StandardInput=socket
StandardOutput=file:/etc/testServer/Debug/test_outLink
Restart=on-failure
RestartSec=3
[Install]
WantedBy=multi-user.target
The bash script "/etc/testServer/Debug/makeOutfile.sh" looks like this:
#!/usr/bin/env bash
timeStamp=$(date +%y-%m-%d_%Hh%Mm%Ss)
myfolder="/etc/testServer/Debug"
# create new logfile
touch $myfolder/started_$timeStamp
# remove old symlink if exists (owned by test1)
if [ -h $myfolder/test_outLink ]; then
rm $myfolder/test_outLink
fi
# create new symlink
ln -s $myfolder/started_$timeStamp $myfolder/test_outLink
It works under certain circumstances. If a symlink exists and if the file it points to also exists, everything is just fine. The bash script "makeOutfile.sh" works via terminal for testing purposes and via a systemd service start.
But:
If the file pointed to (name: "started_atSomeDate") does not exist (because it got removed by someone meanwhile), a terminal call of the script works as expected, but starting the systemd service will add an additional file owned by root with group root with the original old file name: "started_atSomeDate".
Where does that come from?
If the old symlink "test_outLink" does not exist (because it got removed by someone meanwhile), a terminal call of the script works as expected, but starting the systemd service will add the newly created "test_outLink" as a regular file owned by root with group root and the service does not start.
Something goes terribly wrong here and although the systemd service unit User and Group is test1, root gets mixed in here. Can anybody explain? What am I doing wrong?