Can we write pid into a file which store in systemd?












0














I have a script to start java application on boot time.



[Unit]
Description=test Background

[Service]
ExecStart=/bin/bash -c "/usr/bin/java -jar /var/www/tset.com/*.jar"
Type=simple
WorkingDirectory=/var/www/test.com

[Install]
WantedBy=multi-user.target


Can we edit this to write pid into a file in specific location?



As an example,



ExecStart=/bin/bash -c "/usr/bin/java -jar /var/www/test.com/*.jar | echo $! > ${RUNNING_PID}"









share|improve this question



























    0














    I have a script to start java application on boot time.



    [Unit]
    Description=test Background

    [Service]
    ExecStart=/bin/bash -c "/usr/bin/java -jar /var/www/tset.com/*.jar"
    Type=simple
    WorkingDirectory=/var/www/test.com

    [Install]
    WantedBy=multi-user.target


    Can we edit this to write pid into a file in specific location?



    As an example,



    ExecStart=/bin/bash -c "/usr/bin/java -jar /var/www/test.com/*.jar | echo $! > ${RUNNING_PID}"









    share|improve this question

























      0












      0








      0







      I have a script to start java application on boot time.



      [Unit]
      Description=test Background

      [Service]
      ExecStart=/bin/bash -c "/usr/bin/java -jar /var/www/tset.com/*.jar"
      Type=simple
      WorkingDirectory=/var/www/test.com

      [Install]
      WantedBy=multi-user.target


      Can we edit this to write pid into a file in specific location?



      As an example,



      ExecStart=/bin/bash -c "/usr/bin/java -jar /var/www/test.com/*.jar | echo $! > ${RUNNING_PID}"









      share|improve this question













      I have a script to start java application on boot time.



      [Unit]
      Description=test Background

      [Service]
      ExecStart=/bin/bash -c "/usr/bin/java -jar /var/www/tset.com/*.jar"
      Type=simple
      WorkingDirectory=/var/www/test.com

      [Install]
      WantedBy=multi-user.target


      Can we edit this to write pid into a file in specific location?



      As an example,



      ExecStart=/bin/bash -c "/usr/bin/java -jar /var/www/test.com/*.jar | echo $! > ${RUNNING_PID}"






      startup systemd






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 17 '18 at 3:21









      Janith

      697




      697






















          2 Answers
          2






          active

          oldest

          votes


















          1














          The way I'd approach this problem is this way:



          ExecStart=/bin/bash -c 'echo $$ > /var/run/tset.pid; exec /usr/bin/java -jar /var/www/tset.com/*.jar'


          There's a couple things that require attention:




          • what you're trying to do is to write .pid file which contains the PID of the process, and on Linux the "de facto" location is to put it into /var/run


          • The > operator is for redirecting stdout of echo, which will be the contents of $$ or the shell PID.


          • Why shell PID ? Well, that's because next thing we're using is exec command which will replace shell process with your java application. What was PID 1234 representing shell will now be 1234 representing java application.



          Such approach, while seems convoluted, is actually more appropriate:




          • we know the PID instead of using ps or pgrep to find it, we don't have to apply complex parsing, nor we have a problem if there's multiple instances of the application running already ( which shouldn't be the case if you're using a systemd service to start it, but there's no harm in accounting for a possibility of that as well )

          • and with exec resources won't be wasted for starting multiple processes. When you have something like bash -c '/usr/bin/java -jar myapp.jar & pgrep -f myapp.jar > /var/run/myapp.pid ' that's 3 processes. In the above command suggested - we have one process replacing another.


          Of course, this is just an example. Adjust how necessary to your case, and /var/run/tset.pid is chosen here just as an example - naming is up to you as well as location of the file, though I'd recommend using /var/run for consistency with other applications.



          Side note:



          The | echo $! > ${RUNNING_PID}" part wouldn't be appropriate for at least one reason: ${RUNNING_PID} is a variable, however it is not declared anywhere, so it would be a replaced by plank in shell. | is pointless - stdout of java application is connected to stdin of echo which in case where you want output of application sent to another application is appropriate, but echo does not read stdin - i.e. here it is pointless.



          Of course, if the application itself ( i.e. the java application ) forks - i.e creates - another process the $$ won't help much, though to be perfectly fair if the application does indeed create multiple forks then it probably should be the job of the application itself to create and manage the .pid file and report main process pid.






          share|improve this answer























          • Can we use another method insted of this? As a example java -jar script is somewhere else and start that script using systemd ?
            – Janith
            Dec 17 '18 at 3:56












          • @Janith That depends on the details, though I'd say exec way is better. You say you have a script file somewhere. Well, is that a shell script ? If that's so, it could be edited to perform pretty much same function as show in the answer. Again, everything depends on the details - I can't give any concrete suggestions without knowing them.
            – Sergiy Kolodyazhnyy
            Dec 17 '18 at 4:04










          • Yes it is a shell script. I have a shell script to start and write pid into a file then kill pid. Can I run using systemd service.
            – Janith
            Dec 17 '18 at 4:09






          • 1




            @Janith Sure, starting things via systemd is perfectly fine. However, have you considered using systemd stop servicename instead of pid file ? This may be a more appropriate mechanism for manually stopping a service. Typically .pid files are intended for ensuring there exists only one instance of a process/application. I'd recommend either editing your question with more details, or asking a new question which describes the situation exactly of what you want to do with the script. Again, personally I'd just the method I described in this answer. It's appropriate for scripts,too
            – Sergiy Kolodyazhnyy
            Dec 17 '18 at 4:22










          • I have asked once. But did not get the appropriate answer. askubuntu.com/questions/1100256/run-a-script-on-boot/…. I tried to do that but not worked.
            – Janith
            Dec 17 '18 at 4:28





















          1














          You should be able to run java directly rather than running a bash shell that runs java. If you do that you can run the systemctl show command to get the PID



          systemctl show -p MainPID <your service name>


          assuming of course that your java application doesn't create more processes.



          If your service name file ends in .service then you can omit the .service suffix from <your service name>



          If you run via a bash script then you'll get the pid of the bash shell that's running your java process.






          share|improve this answer





















            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',
            autoActivateHeartbeat: false,
            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%2f1102458%2fcan-we-write-pid-into-a-file-which-store-in-systemd%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            The way I'd approach this problem is this way:



            ExecStart=/bin/bash -c 'echo $$ > /var/run/tset.pid; exec /usr/bin/java -jar /var/www/tset.com/*.jar'


            There's a couple things that require attention:




            • what you're trying to do is to write .pid file which contains the PID of the process, and on Linux the "de facto" location is to put it into /var/run


            • The > operator is for redirecting stdout of echo, which will be the contents of $$ or the shell PID.


            • Why shell PID ? Well, that's because next thing we're using is exec command which will replace shell process with your java application. What was PID 1234 representing shell will now be 1234 representing java application.



            Such approach, while seems convoluted, is actually more appropriate:




            • we know the PID instead of using ps or pgrep to find it, we don't have to apply complex parsing, nor we have a problem if there's multiple instances of the application running already ( which shouldn't be the case if you're using a systemd service to start it, but there's no harm in accounting for a possibility of that as well )

            • and with exec resources won't be wasted for starting multiple processes. When you have something like bash -c '/usr/bin/java -jar myapp.jar & pgrep -f myapp.jar > /var/run/myapp.pid ' that's 3 processes. In the above command suggested - we have one process replacing another.


            Of course, this is just an example. Adjust how necessary to your case, and /var/run/tset.pid is chosen here just as an example - naming is up to you as well as location of the file, though I'd recommend using /var/run for consistency with other applications.



            Side note:



            The | echo $! > ${RUNNING_PID}" part wouldn't be appropriate for at least one reason: ${RUNNING_PID} is a variable, however it is not declared anywhere, so it would be a replaced by plank in shell. | is pointless - stdout of java application is connected to stdin of echo which in case where you want output of application sent to another application is appropriate, but echo does not read stdin - i.e. here it is pointless.



            Of course, if the application itself ( i.e. the java application ) forks - i.e creates - another process the $$ won't help much, though to be perfectly fair if the application does indeed create multiple forks then it probably should be the job of the application itself to create and manage the .pid file and report main process pid.






            share|improve this answer























            • Can we use another method insted of this? As a example java -jar script is somewhere else and start that script using systemd ?
              – Janith
              Dec 17 '18 at 3:56












            • @Janith That depends on the details, though I'd say exec way is better. You say you have a script file somewhere. Well, is that a shell script ? If that's so, it could be edited to perform pretty much same function as show in the answer. Again, everything depends on the details - I can't give any concrete suggestions without knowing them.
              – Sergiy Kolodyazhnyy
              Dec 17 '18 at 4:04










            • Yes it is a shell script. I have a shell script to start and write pid into a file then kill pid. Can I run using systemd service.
              – Janith
              Dec 17 '18 at 4:09






            • 1




              @Janith Sure, starting things via systemd is perfectly fine. However, have you considered using systemd stop servicename instead of pid file ? This may be a more appropriate mechanism for manually stopping a service. Typically .pid files are intended for ensuring there exists only one instance of a process/application. I'd recommend either editing your question with more details, or asking a new question which describes the situation exactly of what you want to do with the script. Again, personally I'd just the method I described in this answer. It's appropriate for scripts,too
              – Sergiy Kolodyazhnyy
              Dec 17 '18 at 4:22










            • I have asked once. But did not get the appropriate answer. askubuntu.com/questions/1100256/run-a-script-on-boot/…. I tried to do that but not worked.
              – Janith
              Dec 17 '18 at 4:28


















            1














            The way I'd approach this problem is this way:



            ExecStart=/bin/bash -c 'echo $$ > /var/run/tset.pid; exec /usr/bin/java -jar /var/www/tset.com/*.jar'


            There's a couple things that require attention:




            • what you're trying to do is to write .pid file which contains the PID of the process, and on Linux the "de facto" location is to put it into /var/run


            • The > operator is for redirecting stdout of echo, which will be the contents of $$ or the shell PID.


            • Why shell PID ? Well, that's because next thing we're using is exec command which will replace shell process with your java application. What was PID 1234 representing shell will now be 1234 representing java application.



            Such approach, while seems convoluted, is actually more appropriate:




            • we know the PID instead of using ps or pgrep to find it, we don't have to apply complex parsing, nor we have a problem if there's multiple instances of the application running already ( which shouldn't be the case if you're using a systemd service to start it, but there's no harm in accounting for a possibility of that as well )

            • and with exec resources won't be wasted for starting multiple processes. When you have something like bash -c '/usr/bin/java -jar myapp.jar & pgrep -f myapp.jar > /var/run/myapp.pid ' that's 3 processes. In the above command suggested - we have one process replacing another.


            Of course, this is just an example. Adjust how necessary to your case, and /var/run/tset.pid is chosen here just as an example - naming is up to you as well as location of the file, though I'd recommend using /var/run for consistency with other applications.



            Side note:



            The | echo $! > ${RUNNING_PID}" part wouldn't be appropriate for at least one reason: ${RUNNING_PID} is a variable, however it is not declared anywhere, so it would be a replaced by plank in shell. | is pointless - stdout of java application is connected to stdin of echo which in case where you want output of application sent to another application is appropriate, but echo does not read stdin - i.e. here it is pointless.



            Of course, if the application itself ( i.e. the java application ) forks - i.e creates - another process the $$ won't help much, though to be perfectly fair if the application does indeed create multiple forks then it probably should be the job of the application itself to create and manage the .pid file and report main process pid.






            share|improve this answer























            • Can we use another method insted of this? As a example java -jar script is somewhere else and start that script using systemd ?
              – Janith
              Dec 17 '18 at 3:56












            • @Janith That depends on the details, though I'd say exec way is better. You say you have a script file somewhere. Well, is that a shell script ? If that's so, it could be edited to perform pretty much same function as show in the answer. Again, everything depends on the details - I can't give any concrete suggestions without knowing them.
              – Sergiy Kolodyazhnyy
              Dec 17 '18 at 4:04










            • Yes it is a shell script. I have a shell script to start and write pid into a file then kill pid. Can I run using systemd service.
              – Janith
              Dec 17 '18 at 4:09






            • 1




              @Janith Sure, starting things via systemd is perfectly fine. However, have you considered using systemd stop servicename instead of pid file ? This may be a more appropriate mechanism for manually stopping a service. Typically .pid files are intended for ensuring there exists only one instance of a process/application. I'd recommend either editing your question with more details, or asking a new question which describes the situation exactly of what you want to do with the script. Again, personally I'd just the method I described in this answer. It's appropriate for scripts,too
              – Sergiy Kolodyazhnyy
              Dec 17 '18 at 4:22










            • I have asked once. But did not get the appropriate answer. askubuntu.com/questions/1100256/run-a-script-on-boot/…. I tried to do that but not worked.
              – Janith
              Dec 17 '18 at 4:28
















            1












            1








            1






            The way I'd approach this problem is this way:



            ExecStart=/bin/bash -c 'echo $$ > /var/run/tset.pid; exec /usr/bin/java -jar /var/www/tset.com/*.jar'


            There's a couple things that require attention:




            • what you're trying to do is to write .pid file which contains the PID of the process, and on Linux the "de facto" location is to put it into /var/run


            • The > operator is for redirecting stdout of echo, which will be the contents of $$ or the shell PID.


            • Why shell PID ? Well, that's because next thing we're using is exec command which will replace shell process with your java application. What was PID 1234 representing shell will now be 1234 representing java application.



            Such approach, while seems convoluted, is actually more appropriate:




            • we know the PID instead of using ps or pgrep to find it, we don't have to apply complex parsing, nor we have a problem if there's multiple instances of the application running already ( which shouldn't be the case if you're using a systemd service to start it, but there's no harm in accounting for a possibility of that as well )

            • and with exec resources won't be wasted for starting multiple processes. When you have something like bash -c '/usr/bin/java -jar myapp.jar & pgrep -f myapp.jar > /var/run/myapp.pid ' that's 3 processes. In the above command suggested - we have one process replacing another.


            Of course, this is just an example. Adjust how necessary to your case, and /var/run/tset.pid is chosen here just as an example - naming is up to you as well as location of the file, though I'd recommend using /var/run for consistency with other applications.



            Side note:



            The | echo $! > ${RUNNING_PID}" part wouldn't be appropriate for at least one reason: ${RUNNING_PID} is a variable, however it is not declared anywhere, so it would be a replaced by plank in shell. | is pointless - stdout of java application is connected to stdin of echo which in case where you want output of application sent to another application is appropriate, but echo does not read stdin - i.e. here it is pointless.



            Of course, if the application itself ( i.e. the java application ) forks - i.e creates - another process the $$ won't help much, though to be perfectly fair if the application does indeed create multiple forks then it probably should be the job of the application itself to create and manage the .pid file and report main process pid.






            share|improve this answer














            The way I'd approach this problem is this way:



            ExecStart=/bin/bash -c 'echo $$ > /var/run/tset.pid; exec /usr/bin/java -jar /var/www/tset.com/*.jar'


            There's a couple things that require attention:




            • what you're trying to do is to write .pid file which contains the PID of the process, and on Linux the "de facto" location is to put it into /var/run


            • The > operator is for redirecting stdout of echo, which will be the contents of $$ or the shell PID.


            • Why shell PID ? Well, that's because next thing we're using is exec command which will replace shell process with your java application. What was PID 1234 representing shell will now be 1234 representing java application.



            Such approach, while seems convoluted, is actually more appropriate:




            • we know the PID instead of using ps or pgrep to find it, we don't have to apply complex parsing, nor we have a problem if there's multiple instances of the application running already ( which shouldn't be the case if you're using a systemd service to start it, but there's no harm in accounting for a possibility of that as well )

            • and with exec resources won't be wasted for starting multiple processes. When you have something like bash -c '/usr/bin/java -jar myapp.jar & pgrep -f myapp.jar > /var/run/myapp.pid ' that's 3 processes. In the above command suggested - we have one process replacing another.


            Of course, this is just an example. Adjust how necessary to your case, and /var/run/tset.pid is chosen here just as an example - naming is up to you as well as location of the file, though I'd recommend using /var/run for consistency with other applications.



            Side note:



            The | echo $! > ${RUNNING_PID}" part wouldn't be appropriate for at least one reason: ${RUNNING_PID} is a variable, however it is not declared anywhere, so it would be a replaced by plank in shell. | is pointless - stdout of java application is connected to stdin of echo which in case where you want output of application sent to another application is appropriate, but echo does not read stdin - i.e. here it is pointless.



            Of course, if the application itself ( i.e. the java application ) forks - i.e creates - another process the $$ won't help much, though to be perfectly fair if the application does indeed create multiple forks then it probably should be the job of the application itself to create and manage the .pid file and report main process pid.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 17 '18 at 3:51

























            answered Dec 17 '18 at 3:31









            Sergiy Kolodyazhnyy

            69.9k9144307




            69.9k9144307












            • Can we use another method insted of this? As a example java -jar script is somewhere else and start that script using systemd ?
              – Janith
              Dec 17 '18 at 3:56












            • @Janith That depends on the details, though I'd say exec way is better. You say you have a script file somewhere. Well, is that a shell script ? If that's so, it could be edited to perform pretty much same function as show in the answer. Again, everything depends on the details - I can't give any concrete suggestions without knowing them.
              – Sergiy Kolodyazhnyy
              Dec 17 '18 at 4:04










            • Yes it is a shell script. I have a shell script to start and write pid into a file then kill pid. Can I run using systemd service.
              – Janith
              Dec 17 '18 at 4:09






            • 1




              @Janith Sure, starting things via systemd is perfectly fine. However, have you considered using systemd stop servicename instead of pid file ? This may be a more appropriate mechanism for manually stopping a service. Typically .pid files are intended for ensuring there exists only one instance of a process/application. I'd recommend either editing your question with more details, or asking a new question which describes the situation exactly of what you want to do with the script. Again, personally I'd just the method I described in this answer. It's appropriate for scripts,too
              – Sergiy Kolodyazhnyy
              Dec 17 '18 at 4:22










            • I have asked once. But did not get the appropriate answer. askubuntu.com/questions/1100256/run-a-script-on-boot/…. I tried to do that but not worked.
              – Janith
              Dec 17 '18 at 4:28




















            • Can we use another method insted of this? As a example java -jar script is somewhere else and start that script using systemd ?
              – Janith
              Dec 17 '18 at 3:56












            • @Janith That depends on the details, though I'd say exec way is better. You say you have a script file somewhere. Well, is that a shell script ? If that's so, it could be edited to perform pretty much same function as show in the answer. Again, everything depends on the details - I can't give any concrete suggestions without knowing them.
              – Sergiy Kolodyazhnyy
              Dec 17 '18 at 4:04










            • Yes it is a shell script. I have a shell script to start and write pid into a file then kill pid. Can I run using systemd service.
              – Janith
              Dec 17 '18 at 4:09






            • 1




              @Janith Sure, starting things via systemd is perfectly fine. However, have you considered using systemd stop servicename instead of pid file ? This may be a more appropriate mechanism for manually stopping a service. Typically .pid files are intended for ensuring there exists only one instance of a process/application. I'd recommend either editing your question with more details, or asking a new question which describes the situation exactly of what you want to do with the script. Again, personally I'd just the method I described in this answer. It's appropriate for scripts,too
              – Sergiy Kolodyazhnyy
              Dec 17 '18 at 4:22










            • I have asked once. But did not get the appropriate answer. askubuntu.com/questions/1100256/run-a-script-on-boot/…. I tried to do that but not worked.
              – Janith
              Dec 17 '18 at 4:28


















            Can we use another method insted of this? As a example java -jar script is somewhere else and start that script using systemd ?
            – Janith
            Dec 17 '18 at 3:56






            Can we use another method insted of this? As a example java -jar script is somewhere else and start that script using systemd ?
            – Janith
            Dec 17 '18 at 3:56














            @Janith That depends on the details, though I'd say exec way is better. You say you have a script file somewhere. Well, is that a shell script ? If that's so, it could be edited to perform pretty much same function as show in the answer. Again, everything depends on the details - I can't give any concrete suggestions without knowing them.
            – Sergiy Kolodyazhnyy
            Dec 17 '18 at 4:04




            @Janith That depends on the details, though I'd say exec way is better. You say you have a script file somewhere. Well, is that a shell script ? If that's so, it could be edited to perform pretty much same function as show in the answer. Again, everything depends on the details - I can't give any concrete suggestions without knowing them.
            – Sergiy Kolodyazhnyy
            Dec 17 '18 at 4:04












            Yes it is a shell script. I have a shell script to start and write pid into a file then kill pid. Can I run using systemd service.
            – Janith
            Dec 17 '18 at 4:09




            Yes it is a shell script. I have a shell script to start and write pid into a file then kill pid. Can I run using systemd service.
            – Janith
            Dec 17 '18 at 4:09




            1




            1




            @Janith Sure, starting things via systemd is perfectly fine. However, have you considered using systemd stop servicename instead of pid file ? This may be a more appropriate mechanism for manually stopping a service. Typically .pid files are intended for ensuring there exists only one instance of a process/application. I'd recommend either editing your question with more details, or asking a new question which describes the situation exactly of what you want to do with the script. Again, personally I'd just the method I described in this answer. It's appropriate for scripts,too
            – Sergiy Kolodyazhnyy
            Dec 17 '18 at 4:22




            @Janith Sure, starting things via systemd is perfectly fine. However, have you considered using systemd stop servicename instead of pid file ? This may be a more appropriate mechanism for manually stopping a service. Typically .pid files are intended for ensuring there exists only one instance of a process/application. I'd recommend either editing your question with more details, or asking a new question which describes the situation exactly of what you want to do with the script. Again, personally I'd just the method I described in this answer. It's appropriate for scripts,too
            – Sergiy Kolodyazhnyy
            Dec 17 '18 at 4:22












            I have asked once. But did not get the appropriate answer. askubuntu.com/questions/1100256/run-a-script-on-boot/…. I tried to do that but not worked.
            – Janith
            Dec 17 '18 at 4:28






            I have asked once. But did not get the appropriate answer. askubuntu.com/questions/1100256/run-a-script-on-boot/…. I tried to do that but not worked.
            – Janith
            Dec 17 '18 at 4:28















            1














            You should be able to run java directly rather than running a bash shell that runs java. If you do that you can run the systemctl show command to get the PID



            systemctl show -p MainPID <your service name>


            assuming of course that your java application doesn't create more processes.



            If your service name file ends in .service then you can omit the .service suffix from <your service name>



            If you run via a bash script then you'll get the pid of the bash shell that's running your java process.






            share|improve this answer


























              1














              You should be able to run java directly rather than running a bash shell that runs java. If you do that you can run the systemctl show command to get the PID



              systemctl show -p MainPID <your service name>


              assuming of course that your java application doesn't create more processes.



              If your service name file ends in .service then you can omit the .service suffix from <your service name>



              If you run via a bash script then you'll get the pid of the bash shell that's running your java process.






              share|improve this answer
























                1












                1








                1






                You should be able to run java directly rather than running a bash shell that runs java. If you do that you can run the systemctl show command to get the PID



                systemctl show -p MainPID <your service name>


                assuming of course that your java application doesn't create more processes.



                If your service name file ends in .service then you can omit the .service suffix from <your service name>



                If you run via a bash script then you'll get the pid of the bash shell that's running your java process.






                share|improve this answer












                You should be able to run java directly rather than running a bash shell that runs java. If you do that you can run the systemctl show command to get the PID



                systemctl show -p MainPID <your service name>


                assuming of course that your java application doesn't create more processes.



                If your service name file ends in .service then you can omit the .service suffix from <your service name>



                If you run via a bash script then you'll get the pid of the bash shell that's running your java process.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 17 '18 at 5:12









                Robert Longson

                259410




                259410






























                    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%2f1102458%2fcan-we-write-pid-into-a-file-which-store-in-systemd%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

                    Quarter-circle Tiles

                    build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

                    Mont Emei