1

I've created scripts to control starting, stopping, etc. of my git-daemon on Fedora28. I'm now attempting to link these scripts to a systemd service so git-daemon will be available after reboot.

The primary script (gitT) is...

#!/bin/bash
case "$1" in
  'start')
    echo "Starting git-daemon"
    /home/git/scripts/start.sh >> /home/git/gitT.log
    ;;
  'stop')
    echo "Stopping git-daemon"
    /home/git/scripts/stop.sh >> /home/git/gitT.log
    ;;
  'restart')
    echo "Bouncing git-daemon"
    /home/git/scripts/bounce.sh >> /home/git/gitT.log
    ;;
  'status')
    echo "Status of git-daemon"
    /home/git/scripts/status.sh
    ;;
  *)
    echo "`basename $0`: usage: `basename $0` { stop | start | restart | status }"
    ;;
esac

Secondary scripts are...

start.sh

#!/bin/bash
# --------------------------
echo "---------------------"
/usr/bin/git daemon --export-all --enable=receive-pack --verbose --pid-file=/home/git/git-daemon.pid --base-path=/home/git/repos >> /home/git/git-daemon.out 2>> /home/git/git-daemon.err &
echo "---------------------"
echo "STARTED at `date`"

stop.sh

#!/bin/bash
# --------------------------
echo "---------------------"
pkill -F /home/git/git-daemon.pid
echo "---------------------"
echo "STOPPED at `date`"

bounce.sh

#!/bin/bash
# --------------------------
echo "====================="
/home/git/scripts/stop.sh
echo "====================="
sleep 5
echo "====================="
/home/git/scripts/start.sh
echo "====================="
echo "BOUNCED"

and status.sh

#!/bin/bash
# --------------------------
echo "====================="
ps -x --forest
echo "====================="

Finally I created a service file (git-daemon.service)...

[Unit]
Description=Git Daemon
Documentation=man:git-daemon(1)
ConditionPathExists=/home/git/repos

[Service]
Type=oneshot
ExecStart=/bin/bash /home/git/gitT start
ExecStop=/bin/bash /home/git/gitT stop
RemainAfterExit=yes
User=git
Group=git

[Install]
WantedBy=multi-user.target

Then I set it up with these commands...

cp /home/git/git-daemon.service /etc/systemd/system
systemctl enable git-daemon.service

Now if I run gitT start as the git user, everything starts fine. But it I run systemctl start git-daemon as root, this is the error...

fatal: base-path '/home/git/repos' does not exist or is not a directory
  • Does /home/git/repos exist at the time unit is to be started? – Mark Stosberg May 21 '18 at 17:43
  • Yes, it exists. I confirm this by writing a test into the start script prior to executing the git-daemon, and it was positive. – dacracot May 21 '18 at 17:48
  • The error appears to come from git-daemon not from systemd. But is there is a change if ConditionPathExists=/home/git/repos is removed? Also, what is the output of ls -ld /home/git/repos? Maybe it's a permissions issue and the error message is not accurate. – Mark Stosberg May 21 '18 at 20:04
  • I’ve tried it with and without the condition with no change. Permission shouldn’t be an issue since the directory is owned by the git user and set 775. – dacracot May 21 '18 at 20:10
1

You have created a system that is significantly more complicated to debug then needs to by duplicating a number of pieces of systemd functionality in unnecessary shell scripts. systemd has built-in facilities to start, stop and restart services, provide their status and handle their output.

By removing this layer of complexity, you will have eliminated a significant surface area where bugs can exist and will be that much closer to solving your problem.

I recommend using one of existing templates already posted online for managing git-daemon with systemd if your system doesn't already ship one.

References

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.