0
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 sales@mydomain.co.uk
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
cod4
relies onPATH
and./cod4x18_dedrun
is relative. What if you make them absolute paths? I don't know ifserver.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. – Kamil Maciorowski – 2019-11-11T19:13:54.217Thanx 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. – Alec Barnes – 2019-11-11T19:51:13.490
"if I cd to the server path" – Doesn't your service miss this step? – Kamil Maciorowski – 2019-11-11T19:57:00.770
No, the command is here ExecStart=/cod4-linux-server/cod4.sh – Alec Barnes – 2019-11-11T20:42:42.007
So where does your service change the directory? There is no
WorkingDirectory=
in the unit norcd
in the script… – Kamil Maciorowski – 2019-11-11T21:12:31.673I don't know if the service needs to specify a WorkingDirectory= as when the cod4.sh starts it just uses the /home/cod4 It does not need a cd because the path is in the ExecStart=/cod4-linux-server/cod4.sh – Alec Barnes – 2019-11-11T21:27:37.860
You seem to know better. Well, good luck then. – Kamil Maciorowski – 2019-11-11T21:49:59.623
@KamilMaciorowski Thank-you for your input – Alec Barnes – 2019-11-11T22:37:21.423