2

I have a custom service running in the background for a custom gmod game service. I wrote my own gmod.service file so now I can start it with service gmod start. You can see it below. Is it possible to input a command into that service from a terminal or a bash script so I can change the map for example? Thanks in advance

[Unit] 
Description=Gmod server 
After=network.target 
[Service] 
Type=simple 
ExecStart=/bin/bash /home/server/Documents/Servers/Gmod/run_gmod.sh 
[Install] 
WantedBy=multi-user.target
  • Does the script accept commands if you run it directly from the terminal? – muru May 17 '18 at 11:58
  • @muru Yeah it does – Kevin Snijder May 18 '18 at 6:47
  • Then I'd suggest that you run the script in tmux or screen, and then attach or detach to the tmux/screen session as needed. – muru May 18 '18 at 6:51
  • That might work! I'll try it later. Thanks a bunch :) – Kevin Snijder May 18 '18 at 7:09
3

Use systemd unit template files

This is a good example for when systemd unit templates can be used. It is possible to have a systemd service to be called with an argument and use that argument in the service definition to e. g. hand it to the program that is run. Here is an example for your case:

Install your unit as

/etc/systemd/system/gmod@.service

Change the file to look like this:

[Unit] 
Description=Gmod server (map: %I)
After=network.target 
[Service] 
Type=simple 
ExecStart=/bin/bash /home/server/Documents/Servers/Gmod/run_gmod.sh %i
[Install] 
WantedBy=multi-user.target
DefaultInstance=myDefaultMap

(You might need to run systemctl daemon-reload at this point to make systemd read the changed file). After this you will be able to start gmod with an argument like this:

systemctl start gmod@myMapName.service

Systemd will use the template file and replace I% with the argument, and %i with the shell quoted argument, then start it as a service instance named gmod@myMapName.service. You can verify this by running

systemctl status gmod@myMapName

You can also have it automatically starting a default instance at boot time like this:

systemctl enable gmod@

This way you could even start multiple instances of gmod, if the program allows that. For further reading on the topic check the systemd.unit(5) manual page.

| improve this answer | |

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.