18.04 - how to create PID file in systemd unit?











up vote
0
down vote

favorite












I am trying to properly setup my minecraft server with systemd so that it correctly determines the main PID for the process. I have the following systemd service unit:



[Unit]
Description=Minecraft Server
After=network.target

[Service]
User=minecraft
Group=minecraft

WorkingDirectory=/srv/minecraft

ProtectSystem=full
ProtectHome=true

Type=forking

ExecStartPre=/srv/minecraft/mc_server.sh setup
ExecStart=/srv/minecraft/mc_server.sh start

PIDFile=/srv/minecraft/mc.pid

ExecStop=/srv/minecraft/mc_server.sh stop


[Install]
WantedBy=multi-user.target


When I attempt to start the service, I get this error message (the main issue being the "failed to parse PID from file" part):



minecraft.service - Minecraft Server
Loaded: loaded (/etc/systemd/system/minecraft.service; enabled; vendor preset: enabled)
Active: failed (Result: timeout) since Wed 2018-11-28 12:57:03 PST; 6min ago
Process: 9611 ExecStart=/srv/minecraft/mc_server.sh start (code=exited, status=0/SUCCESS)
Process: 9591 ExecStartPre=/srv/minecraft/mc_server.sh setup (code=exited, status=0/SUCCESS)
Main PID: 5213 (code=exited, status=0/SUCCESS)

Nov 28 12:55:32 xxx mc_server.sh[9591]: No Session found. Creating and configuring.
Nov 28 12:55:32 xxx systemd[1]: minecraft.service: Found left-over process 9607 (tmux: server) in control group while starting unit. Ignoring.
Nov 28 12:55:32 xxx systemd[1]: This usually indicates unclean termination of a previous run, or service implementation deficiencies.
Nov 28 12:55:32 xxx systemd[1]: minecraft.service: Found left-over process 9608 (bash) in control group while starting unit. Ignoring.
Nov 28 12:55:32 xxx systemd[1]: This usually indicates unclean termination of a previous run, or service implementation deficiencies.
Nov 28 12:55:32 xxx mc_server.sh[9611]: Minecraft server successfully started
Nov 28 12:55:32 xxx systemd[1]: minecraft.service: Failed to parse PID from file /srv/minecraft/mc.pid: Invalid argument
Nov 28 12:57:02 xxx systemd[1]: minecraft.service: Start operation timed out. Terminating.
Nov 28 12:57:03 xxx systemd[1]: minecraft.service: Failed with result 'timeout'.
Nov 28 12:57:03 xxx systemd[1]: Failed to start Minecraft Server.


I'm not sure what this error means or how to resolve it. If I remove the PIDfile= option then it works, but lists the tmux process as the main PID and therefore doesn't record output from the java process in the logs. I also just noticed that it's not actually writing the PID to mc.pid. When running the script directly it does write the PID correctly.



Here is the contents of "mc_server.sh start", which is how I'm starting the server and creating the PID file (note that the tmux session is created during "mc_server.sh setup"):



/usr/bin/tmux send-keys -t minecraft '/usr/bin/java -server -Xmx2048M -Xms1024M -jar /srv/minecraft/minecraft_server.jar nogui' C-m && 
/usr/bin/pgrep -u minecraft -f minecraft_server.jar > /srv/minecraft/mc.pid









share|improve this question




























    up vote
    0
    down vote

    favorite












    I am trying to properly setup my minecraft server with systemd so that it correctly determines the main PID for the process. I have the following systemd service unit:



    [Unit]
    Description=Minecraft Server
    After=network.target

    [Service]
    User=minecraft
    Group=minecraft

    WorkingDirectory=/srv/minecraft

    ProtectSystem=full
    ProtectHome=true

    Type=forking

    ExecStartPre=/srv/minecraft/mc_server.sh setup
    ExecStart=/srv/minecraft/mc_server.sh start

    PIDFile=/srv/minecraft/mc.pid

    ExecStop=/srv/minecraft/mc_server.sh stop


    [Install]
    WantedBy=multi-user.target


    When I attempt to start the service, I get this error message (the main issue being the "failed to parse PID from file" part):



    minecraft.service - Minecraft Server
    Loaded: loaded (/etc/systemd/system/minecraft.service; enabled; vendor preset: enabled)
    Active: failed (Result: timeout) since Wed 2018-11-28 12:57:03 PST; 6min ago
    Process: 9611 ExecStart=/srv/minecraft/mc_server.sh start (code=exited, status=0/SUCCESS)
    Process: 9591 ExecStartPre=/srv/minecraft/mc_server.sh setup (code=exited, status=0/SUCCESS)
    Main PID: 5213 (code=exited, status=0/SUCCESS)

    Nov 28 12:55:32 xxx mc_server.sh[9591]: No Session found. Creating and configuring.
    Nov 28 12:55:32 xxx systemd[1]: minecraft.service: Found left-over process 9607 (tmux: server) in control group while starting unit. Ignoring.
    Nov 28 12:55:32 xxx systemd[1]: This usually indicates unclean termination of a previous run, or service implementation deficiencies.
    Nov 28 12:55:32 xxx systemd[1]: minecraft.service: Found left-over process 9608 (bash) in control group while starting unit. Ignoring.
    Nov 28 12:55:32 xxx systemd[1]: This usually indicates unclean termination of a previous run, or service implementation deficiencies.
    Nov 28 12:55:32 xxx mc_server.sh[9611]: Minecraft server successfully started
    Nov 28 12:55:32 xxx systemd[1]: minecraft.service: Failed to parse PID from file /srv/minecraft/mc.pid: Invalid argument
    Nov 28 12:57:02 xxx systemd[1]: minecraft.service: Start operation timed out. Terminating.
    Nov 28 12:57:03 xxx systemd[1]: minecraft.service: Failed with result 'timeout'.
    Nov 28 12:57:03 xxx systemd[1]: Failed to start Minecraft Server.


    I'm not sure what this error means or how to resolve it. If I remove the PIDfile= option then it works, but lists the tmux process as the main PID and therefore doesn't record output from the java process in the logs. I also just noticed that it's not actually writing the PID to mc.pid. When running the script directly it does write the PID correctly.



    Here is the contents of "mc_server.sh start", which is how I'm starting the server and creating the PID file (note that the tmux session is created during "mc_server.sh setup"):



    /usr/bin/tmux send-keys -t minecraft '/usr/bin/java -server -Xmx2048M -Xms1024M -jar /srv/minecraft/minecraft_server.jar nogui' C-m && 
    /usr/bin/pgrep -u minecraft -f minecraft_server.jar > /srv/minecraft/mc.pid









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am trying to properly setup my minecraft server with systemd so that it correctly determines the main PID for the process. I have the following systemd service unit:



      [Unit]
      Description=Minecraft Server
      After=network.target

      [Service]
      User=minecraft
      Group=minecraft

      WorkingDirectory=/srv/minecraft

      ProtectSystem=full
      ProtectHome=true

      Type=forking

      ExecStartPre=/srv/minecraft/mc_server.sh setup
      ExecStart=/srv/minecraft/mc_server.sh start

      PIDFile=/srv/minecraft/mc.pid

      ExecStop=/srv/minecraft/mc_server.sh stop


      [Install]
      WantedBy=multi-user.target


      When I attempt to start the service, I get this error message (the main issue being the "failed to parse PID from file" part):



      minecraft.service - Minecraft Server
      Loaded: loaded (/etc/systemd/system/minecraft.service; enabled; vendor preset: enabled)
      Active: failed (Result: timeout) since Wed 2018-11-28 12:57:03 PST; 6min ago
      Process: 9611 ExecStart=/srv/minecraft/mc_server.sh start (code=exited, status=0/SUCCESS)
      Process: 9591 ExecStartPre=/srv/minecraft/mc_server.sh setup (code=exited, status=0/SUCCESS)
      Main PID: 5213 (code=exited, status=0/SUCCESS)

      Nov 28 12:55:32 xxx mc_server.sh[9591]: No Session found. Creating and configuring.
      Nov 28 12:55:32 xxx systemd[1]: minecraft.service: Found left-over process 9607 (tmux: server) in control group while starting unit. Ignoring.
      Nov 28 12:55:32 xxx systemd[1]: This usually indicates unclean termination of a previous run, or service implementation deficiencies.
      Nov 28 12:55:32 xxx systemd[1]: minecraft.service: Found left-over process 9608 (bash) in control group while starting unit. Ignoring.
      Nov 28 12:55:32 xxx systemd[1]: This usually indicates unclean termination of a previous run, or service implementation deficiencies.
      Nov 28 12:55:32 xxx mc_server.sh[9611]: Minecraft server successfully started
      Nov 28 12:55:32 xxx systemd[1]: minecraft.service: Failed to parse PID from file /srv/minecraft/mc.pid: Invalid argument
      Nov 28 12:57:02 xxx systemd[1]: minecraft.service: Start operation timed out. Terminating.
      Nov 28 12:57:03 xxx systemd[1]: minecraft.service: Failed with result 'timeout'.
      Nov 28 12:57:03 xxx systemd[1]: Failed to start Minecraft Server.


      I'm not sure what this error means or how to resolve it. If I remove the PIDfile= option then it works, but lists the tmux process as the main PID and therefore doesn't record output from the java process in the logs. I also just noticed that it's not actually writing the PID to mc.pid. When running the script directly it does write the PID correctly.



      Here is the contents of "mc_server.sh start", which is how I'm starting the server and creating the PID file (note that the tmux session is created during "mc_server.sh setup"):



      /usr/bin/tmux send-keys -t minecraft '/usr/bin/java -server -Xmx2048M -Xms1024M -jar /srv/minecraft/minecraft_server.jar nogui' C-m && 
      /usr/bin/pgrep -u minecraft -f minecraft_server.jar > /srv/minecraft/mc.pid









      share|improve this question















      I am trying to properly setup my minecraft server with systemd so that it correctly determines the main PID for the process. I have the following systemd service unit:



      [Unit]
      Description=Minecraft Server
      After=network.target

      [Service]
      User=minecraft
      Group=minecraft

      WorkingDirectory=/srv/minecraft

      ProtectSystem=full
      ProtectHome=true

      Type=forking

      ExecStartPre=/srv/minecraft/mc_server.sh setup
      ExecStart=/srv/minecraft/mc_server.sh start

      PIDFile=/srv/minecraft/mc.pid

      ExecStop=/srv/minecraft/mc_server.sh stop


      [Install]
      WantedBy=multi-user.target


      When I attempt to start the service, I get this error message (the main issue being the "failed to parse PID from file" part):



      minecraft.service - Minecraft Server
      Loaded: loaded (/etc/systemd/system/minecraft.service; enabled; vendor preset: enabled)
      Active: failed (Result: timeout) since Wed 2018-11-28 12:57:03 PST; 6min ago
      Process: 9611 ExecStart=/srv/minecraft/mc_server.sh start (code=exited, status=0/SUCCESS)
      Process: 9591 ExecStartPre=/srv/minecraft/mc_server.sh setup (code=exited, status=0/SUCCESS)
      Main PID: 5213 (code=exited, status=0/SUCCESS)

      Nov 28 12:55:32 xxx mc_server.sh[9591]: No Session found. Creating and configuring.
      Nov 28 12:55:32 xxx systemd[1]: minecraft.service: Found left-over process 9607 (tmux: server) in control group while starting unit. Ignoring.
      Nov 28 12:55:32 xxx systemd[1]: This usually indicates unclean termination of a previous run, or service implementation deficiencies.
      Nov 28 12:55:32 xxx systemd[1]: minecraft.service: Found left-over process 9608 (bash) in control group while starting unit. Ignoring.
      Nov 28 12:55:32 xxx systemd[1]: This usually indicates unclean termination of a previous run, or service implementation deficiencies.
      Nov 28 12:55:32 xxx mc_server.sh[9611]: Minecraft server successfully started
      Nov 28 12:55:32 xxx systemd[1]: minecraft.service: Failed to parse PID from file /srv/minecraft/mc.pid: Invalid argument
      Nov 28 12:57:02 xxx systemd[1]: minecraft.service: Start operation timed out. Terminating.
      Nov 28 12:57:03 xxx systemd[1]: minecraft.service: Failed with result 'timeout'.
      Nov 28 12:57:03 xxx systemd[1]: Failed to start Minecraft Server.


      I'm not sure what this error means or how to resolve it. If I remove the PIDfile= option then it works, but lists the tmux process as the main PID and therefore doesn't record output from the java process in the logs. I also just noticed that it's not actually writing the PID to mc.pid. When running the script directly it does write the PID correctly.



      Here is the contents of "mc_server.sh start", which is how I'm starting the server and creating the PID file (note that the tmux session is created during "mc_server.sh setup"):



      /usr/bin/tmux send-keys -t minecraft '/usr/bin/java -server -Xmx2048M -Xms1024M -jar /srv/minecraft/minecraft_server.jar nogui' C-m && 
      /usr/bin/pgrep -u minecraft -f minecraft_server.jar > /srv/minecraft/mc.pid






      server java systemd minecraft tmux






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 days ago

























      asked Nov 28 at 21:14









      MysticEagle

      184




      184



























          active

          oldest

          votes











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "89"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1096933%2f18-04-how-to-create-pid-file-in-systemd-unit%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Ask Ubuntu!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1096933%2f18-04-how-to-create-pid-file-in-systemd-unit%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          數位音樂下載

          When can things happen in Etherscan, such as the picture below?

          格利澤436b