nohup output redirection to a different file












5















I use nohup quite often for important long running processes under linux/bash, so much so that nohup time my command with arguments && mv nohup.out my.log is almost an idiom for me.



The problem is that nohup puts both stdout and stderr into nohup.out, and I cannot control the name of the file. This means that if I accidentally start two nohups in the same directory, their output will be interleaved in nohup.out.



The questions are:




  1. How do I deal with this problem? Always running nohups in separate directories and writing a shell function which will first check for ./nohup.out are two sucky options I see.


  2. How come I cannot tell nohup where to redirect the output? GNU tools tend to have so many options, why not nohup?











share|improve this question





























    5















    I use nohup quite often for important long running processes under linux/bash, so much so that nohup time my command with arguments && mv nohup.out my.log is almost an idiom for me.



    The problem is that nohup puts both stdout and stderr into nohup.out, and I cannot control the name of the file. This means that if I accidentally start two nohups in the same directory, their output will be interleaved in nohup.out.



    The questions are:




    1. How do I deal with this problem? Always running nohups in separate directories and writing a shell function which will first check for ./nohup.out are two sucky options I see.


    2. How come I cannot tell nohup where to redirect the output? GNU tools tend to have so many options, why not nohup?











    share|improve this question



























      5












      5








      5


      2






      I use nohup quite often for important long running processes under linux/bash, so much so that nohup time my command with arguments && mv nohup.out my.log is almost an idiom for me.



      The problem is that nohup puts both stdout and stderr into nohup.out, and I cannot control the name of the file. This means that if I accidentally start two nohups in the same directory, their output will be interleaved in nohup.out.



      The questions are:




      1. How do I deal with this problem? Always running nohups in separate directories and writing a shell function which will first check for ./nohup.out are two sucky options I see.


      2. How come I cannot tell nohup where to redirect the output? GNU tools tend to have so many options, why not nohup?











      share|improve this question
















      I use nohup quite often for important long running processes under linux/bash, so much so that nohup time my command with arguments && mv nohup.out my.log is almost an idiom for me.



      The problem is that nohup puts both stdout and stderr into nohup.out, and I cannot control the name of the file. This means that if I accidentally start two nohups in the same directory, their output will be interleaved in nohup.out.



      The questions are:




      1. How do I deal with this problem? Always running nohups in separate directories and writing a shell function which will first check for ./nohup.out are two sucky options I see.


      2. How come I cannot tell nohup where to redirect the output? GNU tools tend to have so many options, why not nohup?








      bash nohup






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 8 at 15:15







      sds

















      asked Feb 14 '13 at 15:38









      sdssds

      1,24521430




      1,24521430






















          3 Answers
          3






          active

          oldest

          votes


















          7














          You can redirect both stdout and stderr to one file. With Bash 4 (or others such as Zsh), as easy as:



          nohup <some-command> &> output.log


          I can't tell you why there's no separate option for nohup to set the output, but if your shell can take care of that, you don't really need an option.






          share|improve this answer
























          • Will it prevent nohup from performing its own redirection?

            – grawity
            Feb 14 '13 at 15:45






          • 1





            GNU nohup says that it'll redirect stdout to a file if you specify nohup command > file. BSD nohup doesn't mention this at all, but it worked for me.

            – slhck
            Feb 14 '13 at 15:50











          • GNU nohup says that it'll redirect stdout to a file if you specify nohup command > file Isn't redirection in case of nohup command > file being handled by shell and not nohup? If so then how can nohup take any action based on weather redirection is present or not?

            – Piotr Dobrogost
            Jan 21 '15 at 8:29











          • The file nohup.out will be created in the case of error output, too; but if you redirect both stdout and stderr, there cannot be any output, so the file will not be created. But the &> syntax is not POSIX-compliant; you're better off with the answer by Irshad Khan elsewhere on this page.

            – tripleee
            Apr 20 '18 at 7:14



















          6














          To Append output in user defined file you can use >> in nohup command.



          nohup php your_command >> filename.out 2>&1 &


          This command will append all output in your file without removing old data.






          share|improve this answer





















          • 1





            And if you want to overwrite the output file using POSIX sh syntax, that's simply nohup your_command >filename.out 2>&1 &

            – tripleee
            Apr 20 '18 at 7:15



















          3














          There are more ways than just nohup to start a process so that it would ignore SIGHUP; for example: (written as shell functions)



          nohup() {
          setsid "$@"
          }

          nohup() {
          ("$@" &)
          }

          nohup() {
          "$@" & disown
          }


          (setsid, or even (setsid "$@" &), might be the best choice.)



          All of them allow you to specify your own redirections with >, 2>, and &>/>&.






          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "3"
            };
            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%2fsuperuser.com%2fquestions%2f552133%2fnohup-output-redirection-to-a-different-file%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            7














            You can redirect both stdout and stderr to one file. With Bash 4 (or others such as Zsh), as easy as:



            nohup <some-command> &> output.log


            I can't tell you why there's no separate option for nohup to set the output, but if your shell can take care of that, you don't really need an option.






            share|improve this answer
























            • Will it prevent nohup from performing its own redirection?

              – grawity
              Feb 14 '13 at 15:45






            • 1





              GNU nohup says that it'll redirect stdout to a file if you specify nohup command > file. BSD nohup doesn't mention this at all, but it worked for me.

              – slhck
              Feb 14 '13 at 15:50











            • GNU nohup says that it'll redirect stdout to a file if you specify nohup command > file Isn't redirection in case of nohup command > file being handled by shell and not nohup? If so then how can nohup take any action based on weather redirection is present or not?

              – Piotr Dobrogost
              Jan 21 '15 at 8:29











            • The file nohup.out will be created in the case of error output, too; but if you redirect both stdout and stderr, there cannot be any output, so the file will not be created. But the &> syntax is not POSIX-compliant; you're better off with the answer by Irshad Khan elsewhere on this page.

              – tripleee
              Apr 20 '18 at 7:14
















            7














            You can redirect both stdout and stderr to one file. With Bash 4 (or others such as Zsh), as easy as:



            nohup <some-command> &> output.log


            I can't tell you why there's no separate option for nohup to set the output, but if your shell can take care of that, you don't really need an option.






            share|improve this answer
























            • Will it prevent nohup from performing its own redirection?

              – grawity
              Feb 14 '13 at 15:45






            • 1





              GNU nohup says that it'll redirect stdout to a file if you specify nohup command > file. BSD nohup doesn't mention this at all, but it worked for me.

              – slhck
              Feb 14 '13 at 15:50











            • GNU nohup says that it'll redirect stdout to a file if you specify nohup command > file Isn't redirection in case of nohup command > file being handled by shell and not nohup? If so then how can nohup take any action based on weather redirection is present or not?

              – Piotr Dobrogost
              Jan 21 '15 at 8:29











            • The file nohup.out will be created in the case of error output, too; but if you redirect both stdout and stderr, there cannot be any output, so the file will not be created. But the &> syntax is not POSIX-compliant; you're better off with the answer by Irshad Khan elsewhere on this page.

              – tripleee
              Apr 20 '18 at 7:14














            7












            7








            7







            You can redirect both stdout and stderr to one file. With Bash 4 (or others such as Zsh), as easy as:



            nohup <some-command> &> output.log


            I can't tell you why there's no separate option for nohup to set the output, but if your shell can take care of that, you don't really need an option.






            share|improve this answer













            You can redirect both stdout and stderr to one file. With Bash 4 (or others such as Zsh), as easy as:



            nohup <some-command> &> output.log


            I can't tell you why there's no separate option for nohup to set the output, but if your shell can take care of that, you don't really need an option.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 14 '13 at 15:44









            slhckslhck

            162k47448471




            162k47448471













            • Will it prevent nohup from performing its own redirection?

              – grawity
              Feb 14 '13 at 15:45






            • 1





              GNU nohup says that it'll redirect stdout to a file if you specify nohup command > file. BSD nohup doesn't mention this at all, but it worked for me.

              – slhck
              Feb 14 '13 at 15:50











            • GNU nohup says that it'll redirect stdout to a file if you specify nohup command > file Isn't redirection in case of nohup command > file being handled by shell and not nohup? If so then how can nohup take any action based on weather redirection is present or not?

              – Piotr Dobrogost
              Jan 21 '15 at 8:29











            • The file nohup.out will be created in the case of error output, too; but if you redirect both stdout and stderr, there cannot be any output, so the file will not be created. But the &> syntax is not POSIX-compliant; you're better off with the answer by Irshad Khan elsewhere on this page.

              – tripleee
              Apr 20 '18 at 7:14



















            • Will it prevent nohup from performing its own redirection?

              – grawity
              Feb 14 '13 at 15:45






            • 1





              GNU nohup says that it'll redirect stdout to a file if you specify nohup command > file. BSD nohup doesn't mention this at all, but it worked for me.

              – slhck
              Feb 14 '13 at 15:50











            • GNU nohup says that it'll redirect stdout to a file if you specify nohup command > file Isn't redirection in case of nohup command > file being handled by shell and not nohup? If so then how can nohup take any action based on weather redirection is present or not?

              – Piotr Dobrogost
              Jan 21 '15 at 8:29











            • The file nohup.out will be created in the case of error output, too; but if you redirect both stdout and stderr, there cannot be any output, so the file will not be created. But the &> syntax is not POSIX-compliant; you're better off with the answer by Irshad Khan elsewhere on this page.

              – tripleee
              Apr 20 '18 at 7:14

















            Will it prevent nohup from performing its own redirection?

            – grawity
            Feb 14 '13 at 15:45





            Will it prevent nohup from performing its own redirection?

            – grawity
            Feb 14 '13 at 15:45




            1




            1





            GNU nohup says that it'll redirect stdout to a file if you specify nohup command > file. BSD nohup doesn't mention this at all, but it worked for me.

            – slhck
            Feb 14 '13 at 15:50





            GNU nohup says that it'll redirect stdout to a file if you specify nohup command > file. BSD nohup doesn't mention this at all, but it worked for me.

            – slhck
            Feb 14 '13 at 15:50













            GNU nohup says that it'll redirect stdout to a file if you specify nohup command > file Isn't redirection in case of nohup command > file being handled by shell and not nohup? If so then how can nohup take any action based on weather redirection is present or not?

            – Piotr Dobrogost
            Jan 21 '15 at 8:29





            GNU nohup says that it'll redirect stdout to a file if you specify nohup command > file Isn't redirection in case of nohup command > file being handled by shell and not nohup? If so then how can nohup take any action based on weather redirection is present or not?

            – Piotr Dobrogost
            Jan 21 '15 at 8:29













            The file nohup.out will be created in the case of error output, too; but if you redirect both stdout and stderr, there cannot be any output, so the file will not be created. But the &> syntax is not POSIX-compliant; you're better off with the answer by Irshad Khan elsewhere on this page.

            – tripleee
            Apr 20 '18 at 7:14





            The file nohup.out will be created in the case of error output, too; but if you redirect both stdout and stderr, there cannot be any output, so the file will not be created. But the &> syntax is not POSIX-compliant; you're better off with the answer by Irshad Khan elsewhere on this page.

            – tripleee
            Apr 20 '18 at 7:14













            6














            To Append output in user defined file you can use >> in nohup command.



            nohup php your_command >> filename.out 2>&1 &


            This command will append all output in your file without removing old data.






            share|improve this answer





















            • 1





              And if you want to overwrite the output file using POSIX sh syntax, that's simply nohup your_command >filename.out 2>&1 &

              – tripleee
              Apr 20 '18 at 7:15
















            6














            To Append output in user defined file you can use >> in nohup command.



            nohup php your_command >> filename.out 2>&1 &


            This command will append all output in your file without removing old data.






            share|improve this answer





















            • 1





              And if you want to overwrite the output file using POSIX sh syntax, that's simply nohup your_command >filename.out 2>&1 &

              – tripleee
              Apr 20 '18 at 7:15














            6












            6








            6







            To Append output in user defined file you can use >> in nohup command.



            nohup php your_command >> filename.out 2>&1 &


            This command will append all output in your file without removing old data.






            share|improve this answer















            To Append output in user defined file you can use >> in nohup command.



            nohup php your_command >> filename.out 2>&1 &


            This command will append all output in your file without removing old data.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 20 '18 at 7:10

























            answered Aug 18 '17 at 15:10









            Irshad KhanIrshad Khan

            16114




            16114








            • 1





              And if you want to overwrite the output file using POSIX sh syntax, that's simply nohup your_command >filename.out 2>&1 &

              – tripleee
              Apr 20 '18 at 7:15














            • 1





              And if you want to overwrite the output file using POSIX sh syntax, that's simply nohup your_command >filename.out 2>&1 &

              – tripleee
              Apr 20 '18 at 7:15








            1




            1





            And if you want to overwrite the output file using POSIX sh syntax, that's simply nohup your_command >filename.out 2>&1 &

            – tripleee
            Apr 20 '18 at 7:15





            And if you want to overwrite the output file using POSIX sh syntax, that's simply nohup your_command >filename.out 2>&1 &

            – tripleee
            Apr 20 '18 at 7:15











            3














            There are more ways than just nohup to start a process so that it would ignore SIGHUP; for example: (written as shell functions)



            nohup() {
            setsid "$@"
            }

            nohup() {
            ("$@" &)
            }

            nohup() {
            "$@" & disown
            }


            (setsid, or even (setsid "$@" &), might be the best choice.)



            All of them allow you to specify your own redirections with >, 2>, and &>/>&.






            share|improve this answer




























              3














              There are more ways than just nohup to start a process so that it would ignore SIGHUP; for example: (written as shell functions)



              nohup() {
              setsid "$@"
              }

              nohup() {
              ("$@" &)
              }

              nohup() {
              "$@" & disown
              }


              (setsid, or even (setsid "$@" &), might be the best choice.)



              All of them allow you to specify your own redirections with >, 2>, and &>/>&.






              share|improve this answer


























                3












                3








                3







                There are more ways than just nohup to start a process so that it would ignore SIGHUP; for example: (written as shell functions)



                nohup() {
                setsid "$@"
                }

                nohup() {
                ("$@" &)
                }

                nohup() {
                "$@" & disown
                }


                (setsid, or even (setsid "$@" &), might be the best choice.)



                All of them allow you to specify your own redirections with >, 2>, and &>/>&.






                share|improve this answer













                There are more ways than just nohup to start a process so that it would ignore SIGHUP; for example: (written as shell functions)



                nohup() {
                setsid "$@"
                }

                nohup() {
                ("$@" &)
                }

                nohup() {
                "$@" & disown
                }


                (setsid, or even (setsid "$@" &), might be the best choice.)



                All of them allow you to specify your own redirections with >, 2>, and &>/>&.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 14 '13 at 15:48









                grawitygrawity

                241k37508563




                241k37508563






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Super User!


                    • 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%2fsuperuser.com%2fquestions%2f552133%2fnohup-output-redirection-to-a-different-file%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

                    flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

                    Mangá

                    Eduardo VII do Reino Unido