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
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.WorkingDirectory=
in the unit norcd
in the script…