1

I'm using Ubuntu with the latest updates. I made a systemd service to run a script (Server.exe) (Mono, C#) when my server starts. Here's its configuration:

    [Unit]
    Description=My
    After=network.target

    [Service]
    PIDFile=/home/my/server/bshserver.pid
    WorkingDirectory=/home/my/server
    ExecStart=/home/my/server/start
    User=my
    Group=my
    SyslogIdentifier=My-Server
    StandardOutput=syslog
    Restart=on-failure
    KillSignal=SIGINT

    [Install]
    WantedBy=multi-user.target

"start" file:

    #!/bin/sh
    echo Starting MyServer in /home/my/server
    /home/my/server/Server.exe
    exit $?

I was to try another Ubuntu, fail too

When I run the script /home/my/server/start normally (simply /home/my/server/start on my terminal), it is working as expected. Top reports it's using between 2 and, say, 5% of my CPU, which is normal. But when I start it with service start, htop says it's always using at least 100% of my CPU (one core)

What could explain such a big difference in CPU usage?

Serjaru
  • 89
  • 1
  • 9

1 Answers1

0

There are a couple issues I'm seeing:

  • In order to start a mono application it must be run with the command mono. Use which mono to find the full path of mono installed on your server. As it is your script will report a failure every time its run.

  • Your systemd configuration has OnRestart=on-failure set, which is working more effectively than intended. Since your script will always fail systemd will continuously try to restart the service.

Update your script to include calling mono:

#!/bin/sh
echo Starting MyServer in /home/my/server
/user/bin/mono /home/my/server/Server.exe
exit $?

If your service uses ServiceBase then use:

#!/bin/sh
echo Starting MyServer in /home/my/server
/user/bin/mono-service /home/my/server/Server.exe
exit $?
  • Your script may imply that Server.exe will fork. Add Type=forking to your configuration.
    [Unit]
    Description=My
    After=network.target

    [Service]
    Type=forking
    PIDFile=/home/my/server/bshserver.pid
    WorkingDirectory=/home/my/server
    ExecStart=/home/my/server/start
    User=my
    Group=my
    SyslogIdentifier=My-Server
    StandardOutput=syslog
    Restart=on-failure
    KillSignal=SIGINT

    [Install]
    WantedBy=multi-user.target
Dan S
  • 9,139
  • 3
  • 37
  • 48