jobs command in a script.

When you run this script in a command prompt it runs fines and echos the background jobs but when written to a script and run, it outputs nothing.

for job in `jobs -p`
do
echo $job
done

Any ideas what I might be doing wrong.

Thanks,

SK

My bet is it could be an environment issue. What shell are you in when you run it from the command line? Are you also specifying the shell at the top of your script? For example, if you were in the korn shell you might have in the first line of your script:

#!/bin/ksh

Also I'd make sure your `jobs` command is in your $PATH when the script executes. After your script executes you might also check $? to see what the return value is. Hope that helps.

'jobs' is a built-in for ksh.

There is a section in the Bash man-page, which goes some way to explaining your predicament (the same also holds of other shells which support job control, but man ksh doesn't word it quite like that):

COMMAND EXECUTION ENVIRONMENT
       The shell has an execution environment, which consists of the following:

       o ....

       o ....
...
       o      various process IDs, including those of background jobs, the value of $$, and the value of $PPID

That's to say that jobs won't show you background processes started in your shell from your script (which runs in a different environment from your shell).

Your best bet, if it's practicable, is to run your script in the same environment as the shell which started the jobs.

i.e.

$ . ./myscript

or to pass the jobs PID's into the script, i.e, like

./myscript $(jobs -p) 

myscript:

for job in $@; do
  echo $job
done