Why does the jobs command not work in shell script?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












If I run jobs -l on command prompt it shows me the status of running jobs but if I run below file ./tmp.sh



#! /bin/bash
jobs -l


It shows empty output.



Why is that and how can I obtain information about a status of a particular job inside of a shell script?







share|improve this question

























    up vote
    2
    down vote

    favorite












    If I run jobs -l on command prompt it shows me the status of running jobs but if I run below file ./tmp.sh



    #! /bin/bash
    jobs -l


    It shows empty output.



    Why is that and how can I obtain information about a status of a particular job inside of a shell script?







    share|improve this question





















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      If I run jobs -l on command prompt it shows me the status of running jobs but if I run below file ./tmp.sh



      #! /bin/bash
      jobs -l


      It shows empty output.



      Why is that and how can I obtain information about a status of a particular job inside of a shell script?







      share|improve this question











      If I run jobs -l on command prompt it shows me the status of running jobs but if I run below file ./tmp.sh



      #! /bin/bash
      jobs -l


      It shows empty output.



      Why is that and how can I obtain information about a status of a particular job inside of a shell script?









      share|improve this question










      share|improve this question




      share|improve this question









      asked May 19 '16 at 10:56









      user13107

      2,20982551




      2,20982551




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          jobs



          shows the jobs managed by the current shell. Your script runs inside its own shell, so jobs there will only show jobs managed by the script's shell...



          To see this in action, run



          #!/bin/bash
          sleep 60 &
          jobs -l


          To see information about "jobs" started before a shell script, inside the script, you need to treat them like regular processes, and use ps etc. You can limit yourself to processes started by the parent shell (the shell from which the script was started, if any) by using the $PPID variable inside the script, and looking for processes sharing the same parent PID.






          share|improve this answer



















          • 1




            thanks. could you also answer how can I obtain information about a status of a particular job inside of a shell script?
            – user13107
            May 19 '16 at 11:06










          • (i'll accept your answer later in case someone has a better method) by the way, can it happen that the process id gets assigned to some other process by the system, after the original process is done? it could create a confusion then!
            – user13107
            May 19 '16 at 11:21










          • PIDs are recycled, yes. Looking at the issue more generally, to manage groups of processes together, you could use process groups or cgroups, but that feels like it's outside the scope of your question...
            – Stephen Kitt
            May 19 '16 at 11:37

















          up vote
          0
          down vote













          Like Stephen said, spawning a new shell is probably not what you want to do. You have to run the code in the current shell.



          Your code would work by either doing source myscript.sh or declaring your code in a function (which could be in your bashrc or a separate file that is sourced).



          myfunction () 
          jobs -l



          I used this in my dotfiles, if you're interested.






          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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            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%2f284129%2fwhy-does-the-jobs-command-not-work-in-shell-script%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            5
            down vote



            accepted










            jobs



            shows the jobs managed by the current shell. Your script runs inside its own shell, so jobs there will only show jobs managed by the script's shell...



            To see this in action, run



            #!/bin/bash
            sleep 60 &
            jobs -l


            To see information about "jobs" started before a shell script, inside the script, you need to treat them like regular processes, and use ps etc. You can limit yourself to processes started by the parent shell (the shell from which the script was started, if any) by using the $PPID variable inside the script, and looking for processes sharing the same parent PID.






            share|improve this answer



















            • 1




              thanks. could you also answer how can I obtain information about a status of a particular job inside of a shell script?
              – user13107
              May 19 '16 at 11:06










            • (i'll accept your answer later in case someone has a better method) by the way, can it happen that the process id gets assigned to some other process by the system, after the original process is done? it could create a confusion then!
              – user13107
              May 19 '16 at 11:21










            • PIDs are recycled, yes. Looking at the issue more generally, to manage groups of processes together, you could use process groups or cgroups, but that feels like it's outside the scope of your question...
              – Stephen Kitt
              May 19 '16 at 11:37














            up vote
            5
            down vote



            accepted










            jobs



            shows the jobs managed by the current shell. Your script runs inside its own shell, so jobs there will only show jobs managed by the script's shell...



            To see this in action, run



            #!/bin/bash
            sleep 60 &
            jobs -l


            To see information about "jobs" started before a shell script, inside the script, you need to treat them like regular processes, and use ps etc. You can limit yourself to processes started by the parent shell (the shell from which the script was started, if any) by using the $PPID variable inside the script, and looking for processes sharing the same parent PID.






            share|improve this answer



















            • 1




              thanks. could you also answer how can I obtain information about a status of a particular job inside of a shell script?
              – user13107
              May 19 '16 at 11:06










            • (i'll accept your answer later in case someone has a better method) by the way, can it happen that the process id gets assigned to some other process by the system, after the original process is done? it could create a confusion then!
              – user13107
              May 19 '16 at 11:21










            • PIDs are recycled, yes. Looking at the issue more generally, to manage groups of processes together, you could use process groups or cgroups, but that feels like it's outside the scope of your question...
              – Stephen Kitt
              May 19 '16 at 11:37












            up vote
            5
            down vote



            accepted







            up vote
            5
            down vote



            accepted






            jobs



            shows the jobs managed by the current shell. Your script runs inside its own shell, so jobs there will only show jobs managed by the script's shell...



            To see this in action, run



            #!/bin/bash
            sleep 60 &
            jobs -l


            To see information about "jobs" started before a shell script, inside the script, you need to treat them like regular processes, and use ps etc. You can limit yourself to processes started by the parent shell (the shell from which the script was started, if any) by using the $PPID variable inside the script, and looking for processes sharing the same parent PID.






            share|improve this answer















            jobs



            shows the jobs managed by the current shell. Your script runs inside its own shell, so jobs there will only show jobs managed by the script's shell...



            To see this in action, run



            #!/bin/bash
            sleep 60 &
            jobs -l


            To see information about "jobs" started before a shell script, inside the script, you need to treat them like regular processes, and use ps etc. You can limit yourself to processes started by the parent shell (the shell from which the script was started, if any) by using the $PPID variable inside the script, and looking for processes sharing the same parent PID.







            share|improve this answer















            share|improve this answer



            share|improve this answer








            edited May 19 '16 at 11:14


























            answered May 19 '16 at 11:04









            Stephen Kitt

            139k22296359




            139k22296359







            • 1




              thanks. could you also answer how can I obtain information about a status of a particular job inside of a shell script?
              – user13107
              May 19 '16 at 11:06










            • (i'll accept your answer later in case someone has a better method) by the way, can it happen that the process id gets assigned to some other process by the system, after the original process is done? it could create a confusion then!
              – user13107
              May 19 '16 at 11:21










            • PIDs are recycled, yes. Looking at the issue more generally, to manage groups of processes together, you could use process groups or cgroups, but that feels like it's outside the scope of your question...
              – Stephen Kitt
              May 19 '16 at 11:37












            • 1




              thanks. could you also answer how can I obtain information about a status of a particular job inside of a shell script?
              – user13107
              May 19 '16 at 11:06










            • (i'll accept your answer later in case someone has a better method) by the way, can it happen that the process id gets assigned to some other process by the system, after the original process is done? it could create a confusion then!
              – user13107
              May 19 '16 at 11:21










            • PIDs are recycled, yes. Looking at the issue more generally, to manage groups of processes together, you could use process groups or cgroups, but that feels like it's outside the scope of your question...
              – Stephen Kitt
              May 19 '16 at 11:37







            1




            1




            thanks. could you also answer how can I obtain information about a status of a particular job inside of a shell script?
            – user13107
            May 19 '16 at 11:06




            thanks. could you also answer how can I obtain information about a status of a particular job inside of a shell script?
            – user13107
            May 19 '16 at 11:06












            (i'll accept your answer later in case someone has a better method) by the way, can it happen that the process id gets assigned to some other process by the system, after the original process is done? it could create a confusion then!
            – user13107
            May 19 '16 at 11:21




            (i'll accept your answer later in case someone has a better method) by the way, can it happen that the process id gets assigned to some other process by the system, after the original process is done? it could create a confusion then!
            – user13107
            May 19 '16 at 11:21












            PIDs are recycled, yes. Looking at the issue more generally, to manage groups of processes together, you could use process groups or cgroups, but that feels like it's outside the scope of your question...
            – Stephen Kitt
            May 19 '16 at 11:37




            PIDs are recycled, yes. Looking at the issue more generally, to manage groups of processes together, you could use process groups or cgroups, but that feels like it's outside the scope of your question...
            – Stephen Kitt
            May 19 '16 at 11:37












            up vote
            0
            down vote













            Like Stephen said, spawning a new shell is probably not what you want to do. You have to run the code in the current shell.



            Your code would work by either doing source myscript.sh or declaring your code in a function (which could be in your bashrc or a separate file that is sourced).



            myfunction () 
            jobs -l



            I used this in my dotfiles, if you're interested.






            share|improve this answer



























              up vote
              0
              down vote













              Like Stephen said, spawning a new shell is probably not what you want to do. You have to run the code in the current shell.



              Your code would work by either doing source myscript.sh or declaring your code in a function (which could be in your bashrc or a separate file that is sourced).



              myfunction () 
              jobs -l



              I used this in my dotfiles, if you're interested.






              share|improve this answer

























                up vote
                0
                down vote










                up vote
                0
                down vote









                Like Stephen said, spawning a new shell is probably not what you want to do. You have to run the code in the current shell.



                Your code would work by either doing source myscript.sh or declaring your code in a function (which could be in your bashrc or a separate file that is sourced).



                myfunction () 
                jobs -l



                I used this in my dotfiles, if you're interested.






                share|improve this answer















                Like Stephen said, spawning a new shell is probably not what you want to do. You have to run the code in the current shell.



                Your code would work by either doing source myscript.sh or declaring your code in a function (which could be in your bashrc or a separate file that is sourced).



                myfunction () 
                jobs -l



                I used this in my dotfiles, if you're interested.







                share|improve this answer















                share|improve this answer



                share|improve this answer








                edited Feb 5 '17 at 5:05


























                answered Feb 5 '17 at 4:58









                jasonszhao

                1014




                1014






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f284129%2fwhy-does-the-jobs-command-not-work-in-shell-script%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay