You are not logged in.

#1 2014-03-14 12:29:39

HyperLink
Member
From: In front of my Linux box...?!
Registered: 2014-03-14
Posts: 44

[Systemctl] Own .service file doesn't work/don't know how to do it

Hi guys,

As the title says, I want to create my own systemd .service file. It should start/stop my minecraft server automatically on boot/shutdown. I have had the bash script (http://pastebin.com/ntJJh3Ru) before, it uses some other scripts, but it works. If you exec ./mcserv start, it starts the minecraft server, if you do ./mcserv stop, it stops it. Then I wanted to make it automatically start on bootup. Here's my .service file: http://pastebin.com/Ty5RSRWv, but here's the output of sudo systemctl start minecraftserver-mcserv.service and then sudo systemctl status minecraftserver-mcserv.service:

[mr@server ~]$ sudo systemctl start minecraftserver-mcserv
[mr@server ~]$ sudo systemctl status minecraftserver-mcserv
minecraftserver-mcserv.service - Minecraft Server
   Loaded: loaded (/etc/systemd/system/minecraftserver-mcserv.service; disabled)
   Active: failed (Result: exit-code) since Fr 2014-03-14 13:12:25 CET; 1s ago
  Process: 599 ExecStop=/home/mr/minecraft-server/mcserv stop (code=exited, status=2)
  Process: 570 ExecStart=/home/mr/minecraft-server/mcserv start (code=exited, status=2)
 Main PID: 570 (code=exited, status=2)

Mär 14 13:12:25 server mcserv[599]: tput: No value for $TERM and no -T specified
Mär 14 13:12:25 server mcserv[599]: ] Licensed under the GNU GPL.
Mär 14 13:12:25 server mcserv[599]: /home/mr/minecraft-server/mcserv: Zeile 85: screenonline: Kommand...nden.
Mär 14 13:12:25 server mcserv[599]: tput: No value for $TERM and no -T specified
Mär 14 13:12:25 server mcserv[599]: tput: No value for $TERM and no -T specified
Mär 14 13:12:25 server mcserv[599]: [tput: No value for $TERM and no -T specified
Mär 14 13:12:25 server mcserv[599]: FAILEDtput: No value for $TERM and no -T specified
Mär 14 13:12:25 server mcserv[599]: tput: No value for $TERM and no -T specified
Mär 14 13:12:25 server mcserv[599]: ] Server not running, aborting...
Mär 14 13:12:25 server mcserv[599]: tput: No value for $TERM and no -T specified
Mär 14 13:12:25 server systemd[1]: minecraftserver-mcserv.service: control process exited, code=exite...tus=2
Mär 14 13:12:25 server systemd[1]: Unit minecraftserver-mcserv.service entered failed state.
Hint: Some lines were ellipsized, use -l to show in full.

If you want to test it you need:
GNU screen
My script REQUIRE (http://www.marco-rebhan.de/mirror/linux-script/REQUIRE), it must be in ~/bin folder and that folder must be in your PATH.
Minecraft test server (https://s3.amazonaws.com/Minecraft.Down … .1.7.5.jar), must be in the same folder as mcserv script and named minecraft-server.jar

Then run:

./mcserv start

to start it and

./mcserv stop

to stop, you can also run

./mcserv

to get all the arguments etc etc.

Why doesn't my .service work when mcserv works perfect?

Please help! I want to make this fully automated smile

Last edited by HyperLink (2014-03-14 12:36:56)

Offline

#2 2014-03-14 13:11:56

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: [Systemctl] Own .service file doesn't work/don't know how to do it

Look at the errors in the journal. There's a lot of noise because your scripts assume they're running in a terminal and use tput everywhere.

There's also a message about screenonline. It's incomplete (see the hint at the bottom of the systemctl status output; you may also want to use LANG=C temporarily to avoid posting German messages here), but I think it says "Kommando nicht gefunden" and that makes sense, because while you say that ~/bin has to be in your PATH to run the script, you're not setting the PATH in the service file and ~/bin shouldn't be in the default PATH. Also, unless I'm mistaken, the shell that executes the script is neither a login shell nor an interactive shell, so any PATH exports in .bash_profile/.bashrc won't affect it.

Offline

#3 2014-03-14 13:29:44

ukhippo
Member
From: Non-paged pool
Registered: 2014-02-21
Posts: 366

Re: [Systemctl] Own .service file doesn't work/don't know how to do it

With a 'Type=simple' .service, systemd is expecting the 'ExecStart' command to not exit whilst the service is active.
The other Exec* commands are used to manipulate the active service.

Your 'mcserv start' doesn't do that, as it spawns and then exits. This is an unexpected exit, so systemd calls the ExecStop (if defined, which it is in your case) to cleanup the unexpected exit.

So your service is doing a start immediately followed by a stop.

In this scenario, the solution is to use the 'RemainAfterExit' option. See systemd.service man page. This will tell systemd to consider your service still active, even though the process has exited.

Edit: looks like I took too long to write my reply.

Last edited by ukhippo (2014-03-14 13:32:46)

Offline

#4 2014-03-16 07:25:22

HyperLink
Member
From: In front of my Linux box...?!
Registered: 2014-03-14
Posts: 44

Re: [Systemctl] Own .service file doesn't work/don't know how to do it

Hm, okay. I'll have a look at the man page for systemd.service, maybe I can find out more...

You say the PATH must include ~/bin...
So the lines are like this?

    [Unit]
    Description=Minecraft Server
     
    [Service]
    RemainAfterExit=yes
    User=mr
    ExecStart=PATH=/home/mr/bin:$PATH /home/mr/minecraft-server/mcserv start
    ExecReload=/home/mr/bin:$PATH /home/mr/minecraft-server/mcserv restart
    ExecStop=/home/mr/bin:$PATH /home/mr/minecraft-server/mcserv stop
     
    [Install]
    WantedBy=multi-user.target

I will test this, if it works, Thank you!

EDIT: Nope, it doesn't like PATH=...

Continue testing xD

Last edited by HyperLink (2014-03-16 07:29:03)

Offline

#5 2014-03-16 07:41:06

HyperLink
Member
From: In front of my Linux box...?!
Registered: 2014-03-14
Posts: 44

Re: [Systemctl] Own .service file doesn't work/don't know how to do it

Okay. The output is now this:

[mr@server ~]$ sudo systemctl start minecraftserver-mcserv 
[mr@server ~]$ sudo systemctl status minecraftserver-mcserv
minecraftserver-mcserv.service - Minecraft Server
   Loaded: loaded (/etc/systemd/system/minecraftserver-mcserv.service; disabled)
   Active: active (exited) (Result: exit-code) since So 2014-03-16 08:34:28 CET; 6s ago
  Process: 1073 ExecStart=/home/mr/setpath; /home/mr/minecraft-server/mcserv start (code=exited, status=203/EXEC)
 Main PID: 1073 (code=exited, status=203/EXEC)

Mär 16 08:34:28 server systemd[1]: Started Minecraft Server.
Mär 16 08:34:28 server systemd[1]: minecraftserver-mcserv.service: main process exited, code=exited, status=203/EXEC
[mr@server ~]$ sudo systemctl stop minecraftserver-mcserv  
[mr@server ~]$ sudo systemctl status minecraftserver-mcserv
minecraftserver-mcserv.service - Minecraft Server
   Loaded: loaded (/etc/systemd/system/minecraftserver-mcserv.service; disabled)
   Active: failed (Result: exit-code) since So 2014-03-16 08:34:53 CET; 1s ago
  Process: 1080 ExecStop=/home/mr/setpath; /home/mr/minecraft-server/mcserv stop (code=exited, status=203/EXEC)
  Process: 1073 ExecStart=/home/mr/setpath; /home/mr/minecraft-server/mcserv start (code=exited, status=203/EXEC)
 Main PID: 1073 (code=exited, status=203/EXEC)

Mär 16 08:34:28 server systemd[1]: Started Minecraft Server.
Mär 16 08:34:28 server systemd[1]: minecraftserver-mcserv.service: main process exited, code=exited, status=203/EXEC
Mär 16 08:34:53 server systemd[1]: Stopping Minecraft Server...
Mär 16 08:34:53 server systemd[1]: minecraftserver-mcserv.service: control process exited, code=exited status=203
Mär 16 08:34:53 server systemd[1]: Stopped Minecraft Server.
Mär 16 08:34:53 server systemd[1]: Unit minecraftserver-mcserv.service entered failed state.

and the file contains this:

[Unit]
Description=Minecraft Server

[Service]
RemainAfterExit=yes
User=mr
ExecStart=/home/mr/setpath; /home/mr/minecraft-server/mcserv start
ExecReload=/home/mr/setpath; /home/mr/minecraft-server/mcserv restart
ExecStop=/home/mr/setpath; /home/mr/minecraft-server/mcserv stop

[Install]
WantedBy=multi-user.target

What am I doing wrong?
What's wierd is, that it doesnt show those tput messages anymore, and when I execute

systemctl start minecraftserver-mcserv

,

~/minecraft-server/mcserv status

still says the server is offline...

Offline

#6 2014-03-16 11:12:14

ukhippo
Member
From: Non-paged pool
Registered: 2014-02-21
Posts: 366

Re: [Systemctl] Own .service file doesn't work/don't know how to do it

The main issue is that for running under systemd your mcserv script is not fit for purpose.

Start afresh. Create the following sd-mcserv script:

#!/bin/bash
. ~/.bashrc
nohup java -jar minecraft-server.jar &
echo $! > /run/sd-mcserv.pid

And the service file:

[Unit]
Description=Minecraft Server

[Service]
Type=forking
User=mr
PIDFile=/run/sd-mcserv.pid
ExecStart=/path/to/sd-mcserv

[Install]
WantedBy=multi-user.target

Get that working first, then build on that.

Offline

#7 2014-03-16 12:43:55

HyperLink
Member
From: In front of my Linux box...?!
Registered: 2014-03-14
Posts: 44

Re: [Systemctl] Own .service file doesn't work/don't know how to do it

Hm, I'd really like to have that minecraft server as a GNU screen... Otherwise it's not really controllable
The problem is, I have to first attach the screen session once before I can do "screen -X stuff ...."

Last edited by HyperLink (2014-03-16 12:49:12)

Offline

#8 2014-03-16 17:51:35

ukhippo
Member
From: Non-paged pool
Registered: 2014-02-21
Posts: 366

Re: [Systemctl] Own .service file doesn't work/don't know how to do it

In that case make the script use screen, and go back to a Type=simple service. Again, this is just the bare-bones so it should be easier to see what errors occur. You can implement a stop behaviour if kill is too drastic, once you know the service will startup OK.

Updated sd-mcserv script:

#!/bin/bash
. ~/.bashrc
screen -d -m -S mcserv java -jar minecraft-server.jar

The updated service file:

[Unit]
Description=Minecraft Server

[Service]
User=mr
RemainAfterExit=true
ExecStart=/path/to/sd-mcserv

[Install]
WantedBy=multi-user.target

Offline

#9 2014-03-17 12:25:07

HyperLink
Member
From: In front of my Linux box...?!
Registered: 2014-03-14
Posts: 44

Re: [Systemctl] Own .service file doesn't work/don't know how to do it

Ok, that works, but how to stop it? kill always locks up my whole command blocks, and for screen -X stuff the screen must be visiited atleast once... sad
systemctl stop mcserv just sets the status to inactive(dead)...

Offline

#10 2014-03-17 14:25:56

ukhippo
Member
From: Non-paged pool
Registered: 2014-02-21
Posts: 366

Re: [Systemctl] Own .service file doesn't work/don't know how to do it

Can you not use -X quit to kill it? If so you could use that as the ExecStop command in the service file.

However, if that doesn't cut it extend the sd-mcserv script to support $1 being start/stop and implement stop. Perhaps using a cut-down version of the stop from your original mcserv script.

Offline

#11 2014-03-18 12:06:41

HyperLink
Member
From: In front of my Linux box...?!
Registered: 2014-03-14
Posts: 44

Re: [Systemctl] Own .service file doesn't work/don't know how to do it

Yes, but as I said, kill or anything like that locks up my command block redstone circiut and -X stuff requires the screen to tlease be opened once sad
Is there any way to open the screen quickly and detach it then again? That would solve my problem with -X stuff.

Offline

#12 2014-03-18 13:47:16

ukhippo
Member
From: Non-paged pool
Registered: 2014-02-21
Posts: 366

Re: [Systemctl] Own .service file doesn't work/don't know how to do it

Have you tried using tmux instead of screen (just for minecraft server)?

Offline

#13 2014-03-18 16:42:35

HyperLink
Member
From: In front of my Linux box...?!
Registered: 2014-03-14
Posts: 44

Re: [Systemctl] Own .service file doesn't work/don't know how to do it

Hm, never used tmux too much hmm I'll read the manpage for that first smile

Offline

#14 2014-03-18 18:06:38

ukhippo
Member
From: Non-paged pool
Registered: 2014-02-21
Posts: 366

Re: [Systemctl] Own .service file doesn't work/don't know how to do it

The wiki page is quite helpful; it has an example .service for auto-starting a tmux session.
You could tweak the ExecStart line (and ExecStop if you change the session name) to run the minecraft server. You may also want to add "-c $HOME" to the ExecStart line so that it starts up in your home directory instead of '/'.
To replace stuff, look at send-keys.

Offline

Board footer

Powered by FluxBB