0

I'm trying to use monit to check my game server and restart it if it fails. The monit part is fairly simple to check port 28960 but I'm struggling to run the start script (cod4.sh) if monit detects a fail.

so far I have: monitrc:

    set daemon  60
# set log /var/log/monit.log
set mailserver 127.0.0.1
# set mail-format { from: monit@mydomain }
set alert [email protected]
set httpd port 2812
   allow alec:password

check host dels with address 127.0.0.1
start program = "/bin/systemctl start cod4"
stop program = "/bin/systemctl stop cod4"

if failed port 28960 then restart`

This works and I get en e-mail if I kill the server.

I created a service file cod4.service

[Unit]
Description=Cod4 service

[Service]
User=cod4
Type=forking
ExecStart=/cod4-linux-server/cod4.sh


[Install]
WantedBy=multi-user.target

cod4.sh consists of

#!/bin/bash
screen -dmS cod4 ./cod4x18_dedrun +set r_xassetnum "xmodel=1200" +set sv_authorizemode "0" + set sv_maxclients "64" +set fs_game "mods/rotu2beta" +exec server.cfg +map_rotate

When I try to start the server with service cod4 start nothing happens, I'm probably going about this in the wrong way or over complicating it as usual by piecing together code from different sites.

Basically monit checks port 28960 and needs to run cod4.sh if there is a failure.

Any ideas please.

A little progress I think,

systemctl daemon-reload
systemctl enable cod4.service

I now get

systemctl status cod4
● cod4.service - Cod4 service
   Loaded: loaded (/lib/systemd/system/cod4.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Mon 2019-11-11 16:01:22 EST; 52s ago
  Process: 17788 ExecStart=/cod4-linux-server/cod4.sh (code=exited, status=0/SUCCESS)
 Main PID: 17790 (code=exited, status=0/SUCCESS)

This would suggest that it did indeed execute the cod4.sh then exit as it should, however lsof -i as user cod4 tells me the server is not running.

So whats the difference between su cod4 cd/cod4-linux-server ./cod4.sh which loads the server correctly and

[Service]
User=cod4
Type=forking
ExecStart=/cod4-linux-server/cod4.sh


[Install]
WantedBy=multi-user.target
8
  • cod4 relies on PATH and ./cod4x18_dedrun is relative. What if you make them absolute paths? I don't know if server.cfg is a path. Also note you have + set with a space; other similar snippets are +set without spaces. I'm not sure how it should look like but it's suspicious. Commented Nov 11, 2019 at 19:13
  • Thanx Kamil, I forgot to mention the server runs fine if I cd to the server path and enter ./cod4.sh the entries in cod4.sh with +set are just options for the mod it runs. It could be shortened to screen -dmS cod4 ./cod4x18_dedrun +exec server.cfg +map_rotate. Commented Nov 11, 2019 at 19:51
  • "if I cd to the server path" – Doesn't your service miss this step? Commented Nov 11, 2019 at 19:57
  • No, the command is here ExecStart=/cod4-linux-server/cod4.sh Commented Nov 11, 2019 at 20:42
  • So where does your service change the directory? There is no WorkingDirectory= in the unit nor cd in the script… Commented Nov 11, 2019 at 21:12

1 Answer 1

0

Thank-you Kamil you nailed it with your first comment and made me aware of yet another strange thing about Ubuntu, let me explain what was going on here.

Monit was working as it should, the cod4.service file is correct and it did indeed run when asked.

When the service runs it looks for the cod4.sh script in /cod4-linux-server/ and it finds it OK BUT inside the script was ./cod4x18_dedrun and for some reason it couldn't find it. Changing it to /cod4-linux-server/cod4x18_dedrun did the job.

So if you run a script from a systemd and the script is in the same folder as the binary it launches you still need to be absolute with the binary path!

Cheers

1
  • Kamil's suggestions about the working directory in the comments were spot on. ./cod4x18_dedrun is a relative path, it's resolved relative to working directory. You didn't cd to the server's directory nor tell systemd to change working directory, so the path got resolved relatively to who knows what.
    – gronostaj
    Commented Nov 12, 2019 at 10:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .