jobs command in a script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting jobs command in a script.
# 1  
Old 04-22-2011
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
# 2  
Old 04-22-2011
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:

Code:
#!/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.
# 3  
Old 04-22-2011
Quote:
Originally Posted by 2pugs
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:

Code:
#!/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.
# 4  
Old 04-22-2011
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):

Code:
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.
Code:
$ . ./myscript

or to pass the jobs PID's into the script, i.e, like
Code:
./myscript $(jobs -p)

myscript:
Code:
for job in $@; do
  echo $job
done


Last edited by Scott; 04-22-2011 at 06:17 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to run multiple jobs and it's dependent jobs

I have multiple jobs and each job dependent on other job. Each Job generates a log and If job completed successfully log file end's with JOB ENDED SUCCESSFULLY message and if it failed then it will end with JOB ENDED with FAILURE. I need an help how to start. Attaching the JOB dependency... (3 Replies)
Discussion started by: santoshkumarkal
3 Replies

2. Shell Programming and Scripting

Command to check only Autosys running jobs with autorep like command

Hi, Is there any specific command to use to check only say Running jobs via autorep or similar command for Autosys? (0 Replies)
Discussion started by: sidnow
0 Replies

3. Shell Programming and Scripting

Command to stop all the cron jobs

Hi All, Please provide the command to stop all the cron jobs. Thanks in Advance Regards, Sindu (2 Replies)
Discussion started by: indira_s
2 Replies

4. Shell Programming and Scripting

jobs command behaving differently in script

Here is my test script: #!/bin/sh result=`jobs` echo " Jobs: "$result result=`ls` echo " LS "$result Here is the output: Jobs: LS 0 1 2 3 4 5 6 7 gcd initialize.sh #inter_round_clean.sh# inter_round_clean.sh inter_round_clean.sh~ look parallel_first_run.sh... (3 Replies)
Discussion started by: nealh
3 Replies

5. Shell Programming and Scripting

waiting on jobs in bash, allowing limited parallel jobs at one time, and then for all to finish

Hello, I am running GNU bash, version 3.2.39(1)-release (x86_64-pc-linux-gnu). I have a specific question pertaining to waiting on jobs run in sub-shells, based on the max number of parallel processes I want to allow, and then wait... (1 Reply)
Discussion started by: srao
1 Replies

6. UNIX for Dummies Questions & Answers

jobs command

Good morning! When I type in the command "jobs" it takes me back to the command prompt? Any idea why and how I can display all the jobs that are currently running off that host? Ben (6 Replies)
Discussion started by: bigben1220
6 Replies

7. UNIX for Dummies Questions & Answers

Unix Batch command, and running jobs in queues.

Hello all, I have a quick question. I work in a computational science laboratory, and we recently got a few mac pros to do molecular optimizations on. However, on our normal supercomputers, there are queue systems, mainly PBS. Anyway, the macs obviously don't have PBS, but I've read about... (0 Replies)
Discussion started by: corrado33
0 Replies

8. Shell Programming and Scripting

dsjob command to execute DataStage jobs

Hi Friends, I am using a dsjob command in a unix script to invoke DataStage jobs. DataStage server jobs (version 7.5.2) The command looks like thisL: $DSBinPath/dsjob -server :$SERVER_PORTID -run -mode NORMAL -jobstatus -param INPUT_GCDB_DIR=$InputFilePath -param... (0 Replies)
Discussion started by: sureshg_sampat
0 Replies

9. Shell Programming and Scripting

background jobs exit status and limit the number of jobs to run

i need to execute 5 jobs at a time in background and need to get the exit status of all the jobs i wrote small script below , i'm not sure this is right way to do it.any ideas please help. $cat run_job.ksh #!/usr/bin/ksh #################################### typeset -u SCHEMA_NAME=$1 ... (1 Reply)
Discussion started by: GrepMe
1 Replies

10. UNIX for Dummies Questions & Answers

Monitoring Server Jobs, Roles, and Performance via Command Line

Hey guys, I have an up and coming interview (tomorrow) and during the discussion via phone I was asked if I was familiar with "monitoring jobs in Linux/UNIX using the command line." Now, I currently work in the MS world and I am underneath the NOC hear at my company so I have had no reason to do... (2 Replies)
Discussion started by: Steve M
2 Replies
Login or Register to Ask a Question