systemd: how to redirect stdout to logfile












3














I have a systemd service:



[Unit]
Description=My application

[Service]
ExecStart=/bin/java myapp.jar
Type=simple
User=photo


There is an option: StandardOutput= but I don't understand how to use it to write to a file.
https://www.freedesktop.org/software/systemd/man/systemd.exec.html



I was expecting to put a filepath somehere but the documentation talks about sockets and file descriptors. seems it needs more configuration than just that keyword.



Where to put the filepath ?
I can't find any examples of that use



Thanks










share|improve this question














bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 1




    This is a duplicate of Redirect systemd service logs to file... However, the answer there does not answer your question (it doesn't answer the duplicate question either). You might find this answer useful...
    – don_crissti
    Dec 5 '17 at 13:16










  • my workaround: create a shell launcher that redirect stdout to file and launch that script through systemd.
    – exeral
    Dec 13 '17 at 13:23
















3














I have a systemd service:



[Unit]
Description=My application

[Service]
ExecStart=/bin/java myapp.jar
Type=simple
User=photo


There is an option: StandardOutput= but I don't understand how to use it to write to a file.
https://www.freedesktop.org/software/systemd/man/systemd.exec.html



I was expecting to put a filepath somehere but the documentation talks about sockets and file descriptors. seems it needs more configuration than just that keyword.



Where to put the filepath ?
I can't find any examples of that use



Thanks










share|improve this question














bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 1




    This is a duplicate of Redirect systemd service logs to file... However, the answer there does not answer your question (it doesn't answer the duplicate question either). You might find this answer useful...
    – don_crissti
    Dec 5 '17 at 13:16










  • my workaround: create a shell launcher that redirect stdout to file and launch that script through systemd.
    – exeral
    Dec 13 '17 at 13:23














3












3








3







I have a systemd service:



[Unit]
Description=My application

[Service]
ExecStart=/bin/java myapp.jar
Type=simple
User=photo


There is an option: StandardOutput= but I don't understand how to use it to write to a file.
https://www.freedesktop.org/software/systemd/man/systemd.exec.html



I was expecting to put a filepath somehere but the documentation talks about sockets and file descriptors. seems it needs more configuration than just that keyword.



Where to put the filepath ?
I can't find any examples of that use



Thanks










share|improve this question













I have a systemd service:



[Unit]
Description=My application

[Service]
ExecStart=/bin/java myapp.jar
Type=simple
User=photo


There is an option: StandardOutput= but I don't understand how to use it to write to a file.
https://www.freedesktop.org/software/systemd/man/systemd.exec.html



I was expecting to put a filepath somehere but the documentation talks about sockets and file descriptors. seems it needs more configuration than just that keyword.



Where to put the filepath ?
I can't find any examples of that use



Thanks







systemd logs services stdout






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 5 '17 at 13:11









exeralexeral

16114




16114





bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.










  • 1




    This is a duplicate of Redirect systemd service logs to file... However, the answer there does not answer your question (it doesn't answer the duplicate question either). You might find this answer useful...
    – don_crissti
    Dec 5 '17 at 13:16










  • my workaround: create a shell launcher that redirect stdout to file and launch that script through systemd.
    – exeral
    Dec 13 '17 at 13:23














  • 1




    This is a duplicate of Redirect systemd service logs to file... However, the answer there does not answer your question (it doesn't answer the duplicate question either). You might find this answer useful...
    – don_crissti
    Dec 5 '17 at 13:16










  • my workaround: create a shell launcher that redirect stdout to file and launch that script through systemd.
    – exeral
    Dec 13 '17 at 13:23








1




1




This is a duplicate of Redirect systemd service logs to file... However, the answer there does not answer your question (it doesn't answer the duplicate question either). You might find this answer useful...
– don_crissti
Dec 5 '17 at 13:16




This is a duplicate of Redirect systemd service logs to file... However, the answer there does not answer your question (it doesn't answer the duplicate question either). You might find this answer useful...
– don_crissti
Dec 5 '17 at 13:16












my workaround: create a shell launcher that redirect stdout to file and launch that script through systemd.
– exeral
Dec 13 '17 at 13:23




my workaround: create a shell launcher that redirect stdout to file and launch that script through systemd.
– exeral
Dec 13 '17 at 13:23










1 Answer
1






active

oldest

votes


















1














Use:



[Unit]
Description=My application

[Service]
ExecStart=/usr/bin/java -jar myapp.jar
Type=simple
User=photo
StandardOutput=file:/var/log/logfile


as documented here: https://www.freedesktop.org/software/systemd/man/systemd.exec.html#StandardOutput=



Note that this way log files contents will be overwritten each time service restarts. StandardOutput/Error systemd directives do not support appending to files.



If you want to maintain file log between service restarts and just append new logged lines to it, use instead:



[Unit]
Description=My application

[Service]
ExecStart=/usr/bin/sh -c 'exec /usr/bin/java -jar myapp.jar'
Type=simple
User=photo


exec means that shell program will be substituted with /bin/java program after setting up redirections without forking. So there will be no difference from running /bin/java directly after ExecStart=.






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    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: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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%2funix.stackexchange.com%2fquestions%2f408929%2fsystemd-how-to-redirect-stdout-to-logfile%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Use:



    [Unit]
    Description=My application

    [Service]
    ExecStart=/usr/bin/java -jar myapp.jar
    Type=simple
    User=photo
    StandardOutput=file:/var/log/logfile


    as documented here: https://www.freedesktop.org/software/systemd/man/systemd.exec.html#StandardOutput=



    Note that this way log files contents will be overwritten each time service restarts. StandardOutput/Error systemd directives do not support appending to files.



    If you want to maintain file log between service restarts and just append new logged lines to it, use instead:



    [Unit]
    Description=My application

    [Service]
    ExecStart=/usr/bin/sh -c 'exec /usr/bin/java -jar myapp.jar'
    Type=simple
    User=photo


    exec means that shell program will be substituted with /bin/java program after setting up redirections without forking. So there will be no difference from running /bin/java directly after ExecStart=.






    share|improve this answer




























      1














      Use:



      [Unit]
      Description=My application

      [Service]
      ExecStart=/usr/bin/java -jar myapp.jar
      Type=simple
      User=photo
      StandardOutput=file:/var/log/logfile


      as documented here: https://www.freedesktop.org/software/systemd/man/systemd.exec.html#StandardOutput=



      Note that this way log files contents will be overwritten each time service restarts. StandardOutput/Error systemd directives do not support appending to files.



      If you want to maintain file log between service restarts and just append new logged lines to it, use instead:



      [Unit]
      Description=My application

      [Service]
      ExecStart=/usr/bin/sh -c 'exec /usr/bin/java -jar myapp.jar'
      Type=simple
      User=photo


      exec means that shell program will be substituted with /bin/java program after setting up redirections without forking. So there will be no difference from running /bin/java directly after ExecStart=.






      share|improve this answer


























        1












        1








        1






        Use:



        [Unit]
        Description=My application

        [Service]
        ExecStart=/usr/bin/java -jar myapp.jar
        Type=simple
        User=photo
        StandardOutput=file:/var/log/logfile


        as documented here: https://www.freedesktop.org/software/systemd/man/systemd.exec.html#StandardOutput=



        Note that this way log files contents will be overwritten each time service restarts. StandardOutput/Error systemd directives do not support appending to files.



        If you want to maintain file log between service restarts and just append new logged lines to it, use instead:



        [Unit]
        Description=My application

        [Service]
        ExecStart=/usr/bin/sh -c 'exec /usr/bin/java -jar myapp.jar'
        Type=simple
        User=photo


        exec means that shell program will be substituted with /bin/java program after setting up redirections without forking. So there will be no difference from running /bin/java directly after ExecStart=.






        share|improve this answer














        Use:



        [Unit]
        Description=My application

        [Service]
        ExecStart=/usr/bin/java -jar myapp.jar
        Type=simple
        User=photo
        StandardOutput=file:/var/log/logfile


        as documented here: https://www.freedesktop.org/software/systemd/man/systemd.exec.html#StandardOutput=



        Note that this way log files contents will be overwritten each time service restarts. StandardOutput/Error systemd directives do not support appending to files.



        If you want to maintain file log between service restarts and just append new logged lines to it, use instead:



        [Unit]
        Description=My application

        [Service]
        ExecStart=/usr/bin/sh -c 'exec /usr/bin/java -jar myapp.jar'
        Type=simple
        User=photo


        exec means that shell program will be substituted with /bin/java program after setting up redirections without forking. So there will be no difference from running /bin/java directly after ExecStart=.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 7 '18 at 17:31









        Evgeny Lebedev

        14715




        14715










        answered Nov 4 '18 at 0:42









        Piotr JurkiewiczPiotr Jurkiewicz

        744512




        744512






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • 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%2funix.stackexchange.com%2fquestions%2f408929%2fsystemd-how-to-redirect-stdout-to-logfile%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

            How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

            is 'sed' thread safe

            How to make a Squid Proxy server?