Why does the jobs command not work in shell script?
Clash 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?
shell shell-script job-control jobs
add a comment |Â
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?
shell shell-script job-control jobs
add a comment |Â
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?
shell shell-script job-control jobs
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?
shell shell-script job-control jobs
asked May 19 '16 at 10:56
user13107
2,20982551
2,20982551
add a comment |Â
add a comment |Â
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.
1
thanks. could you also answerhow 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
add a comment |Â
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.
add a comment |Â
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.
1
thanks. could you also answerhow 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
add a comment |Â
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.
1
thanks. could you also answerhow 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
add a comment |Â
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.
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.
edited May 19 '16 at 11:14
answered May 19 '16 at 11:04
Stephen Kitt
139k22296359
139k22296359
1
thanks. could you also answerhow 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
add a comment |Â
1
thanks. could you also answerhow 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
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
edited Feb 5 '17 at 5:05
answered Feb 5 '17 at 4:58
jasonszhao
1014
1014
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password