Why jobs doesn’t list commands from script

background-processcommandjobs

I have a script that runs some programs in the background, but after running it, they are not listed by the command 'jobs'. Why is this?

(./m_prog -t m_prog1 m_config) &
(./m_prog  -t m_prog2  m_config) &
(./m_prog  -t m_prog3  m_config) &

But if I execute each one of them from the terminal, they do appear in 'jobs'

How can I get the same effect from commands run in a script?

Best Answer

jobs works only for the instantiation of the shell that created the jobs. jobs n use numbers not the pid. Once the shell is run inside a script (another new process), the old shell that launched the script, jobs (issued in the old shell) no longer can reference job # 1 in the new shell.

Why? Because the current shell could have its own job #1. UNIX/Linux maintains what is known as a process group or session. The group leader of the session is the process that owns the tty and interacts with it via the keyboard. Look up the description of the setsid() function in your manual. If the process was launched and still runs as a child under the old parent shell - the leader, then jobs command will work. Otherwise no.

Related Question