LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-04-2009, 01:15 PM   #1
Suncoast
Member
 
Registered: Apr 2009
Location: Largo, Florida
Distribution: Slackware
Posts: 208

Rep: Reputation: 35
Newbie! How do I get background jobs to execute sequentially?


Hello everyone

I'm looking for a general pointer, or label for what I am trying to do. Not a specific line of code.

I've written a couple of small sh script that gets some user input, then calls several programs to run in the background with the &. My problem is, they all run at the same time.

Is there some way to get these jobs to run sequentially rather than consecutively? If yes, what is this process called? I'm thinking there should be a way to line up background jobs in a job queue, similar to how a print queue works, one job at a time. After searching for a couple of hours, I'm thinking there must be a name for this, but I don't know what it is.

Thanks,
Steve

Edit: Linux Slackware Kernel 2.6.x

Last edited by Suncoast; 05-04-2009 at 01:18 PM.
 
Old 05-04-2009, 02:56 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,064
Blog Entries: 14

Rep: Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248
If you need them sequential simply don't put the "&" behind all of them. Run the script that calls them with an "&" instead. That backgrounds the whole script which does everything sequentially within the backgrounded process.
 
Old 05-04-2009, 03:15 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
A simple alternative to jlightner's suggestion (and that suggestion is a good one) is to build a script in /tmp that just invokes the jobs with the answers as parameters, run the script with the "&," do a disown, do a rm /tmp/<script_name> (which you can do while it's running), and exit the "master" script.
 
Old 05-04-2009, 05:33 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,254

Rep: Reputation: 1420Reputation: 1420Reputation: 1420Reputation: 1420Reputation: 1420Reputation: 1420Reputation: 1420Reputation: 1420Reputation: 1420Reputation: 1420
Wouldn't it be even simpler to use a subshell?
Code:
(program1 args
 program2 args
 etc...) &
Is disown needed either way though?
 
Old 05-05-2009, 03:11 PM   #5
Suncoast
Member
 
Registered: Apr 2009
Location: Largo, Florida
Distribution: Slackware
Posts: 208

Original Poster
Rep: Reputation: 35
Thank you for your kind replies. I'm taking this to mean there is no job queue system in Gnu/Linux.

The /tmp suggestion is something new to me, I will work on that. Thank you.
 
Old 05-05-2009, 04:03 PM   #6
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,064
Blog Entries: 14

Rep: Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248
There is a basic job queuing and scheduling mechanism called cron (and another called anacron for systems like laptops that aren't on all the time).

There are also full blown commercial tools like Tivoli Maestro that allow you to do jobs, schedules etc... across multiple UNIX/Linux machines and make things dependent on each other.

However, you don't have to "queue" things here though since you want them to run sequentially. You just run them in order. You are misunderstanding use of the "&". It doesn't simply background - it also says to run "asynchronously" i.e. the opposite of sequentially.

Rather than trying to impose whatever OS world view you're used to on Linux you should work out WHAT you want to do then ask that question which is what we thought you were doing. Making an assumption based on the question you did NOT ask is faulty logic.
 
Old 05-05-2009, 07:25 PM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

Rep: Reputation: 200Reputation: 200Reputation: 200
Quote:
Originally Posted by Suncoast View Post
I'm taking this to mean there is no job queue system in Gnu/Linux.
very very wrong.

as mentioned cron.
also at and batch


observe:
Code:
for f in date ls ps pwd
do 
echo $f | at now + 1 minute
done 
Job 3 will be executed using /bin/sh
Job 4 will be executed using /bin/sh
Job 5 will be executed using /bin/sh
Job 6 will be executed using /bin/sh
check the queue
$ atq
Date                            Owner           Queue   Job#
Wed May  6 00:24:00 BST 2009    billy           c       3
Wed May  6 00:24:00 BST 2009    billy           c       4
Wed May  6 00:24:00 BST 2009    billy           c       5
Wed May  6 00:24:00 BST 2009    billy           c       6
$
 
Old 05-05-2009, 07:28 PM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

Rep: Reputation: 200Reputation: 200Reputation: 200
Quote:
Originally Posted by jlightner View Post
Rather than trying to impose whatever OS world view you're used to on Linux you should work out WHAT you want to do then ask that question which is what we thought you were doing. Making an assumption based on the question you did NOT ask is faulty logic.
exactly,
tell us what you are trying to do, not how you want it done.
 
Old 05-05-2009, 08:19 PM   #9
Suncoast
Member
 
Registered: Apr 2009
Location: Largo, Florida
Distribution: Slackware
Posts: 208

Original Poster
Rep: Reputation: 35
Quote:
Originally Posted by jlightner View Post
Rather than trying to impose whatever OS world view you're used to on Linux you should work out WHAT you want to do then ask that question which is what we thought you were doing. Making an assumption based on the question you did NOT ask is faulty logic.
Well hold on. I'm in new territory here, and I'm trying to learn. I thought I summarized what I was looking for by saying;

Quote:
Originally Posted by Suncoast
I'm thinking there should be a way to line up background jobs in a job queue, similar to how a print queue works, one job at a time. After searching for a couple of hours, I'm thinking there must be a name for this, but I don't know what it is.
Apparently I failed.

I normally try any suggestions before responding, but seeing where this thread is headed I felt an immediate response was needed. I will now familiarize myself with at, batch, and look again at cron.

Thank you.
 
Old 05-05-2009, 08:33 PM   #10
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
bigearsbilly's demonstration suggests another possible solution:
  1. Run the script to get your parameters, and write the parameters for each job to a file, either a per-job file or a shared one. (I'd use per-job files in /tmp a delete them when finished, but you might want to use the "last run" values as defaults for your "next run.")
  2. Have the script start the first job with an at -f <script_name> now, where that script reads its parameters from the file.
  3. When the script terminates, have it issue an at -f <next_script> now to start the next script.
  4. Repeat step 3 for each job in the chain.

Note that you don't, in fact, need to use the at command for this. A simple sh <script_name> &;disown would also work.

You could also just run a script like this:
Code:
#!/bin/bash
#
# Ask questions to set parameters
....
#
# Run the jobs in sequence
for ((i=0;[ -e Job_$i ];++i))
do
  . Job_$i $Job_i_parameter_1 . . .
done

Last edited by PTrenholme; 05-05-2009 at 08:35 PM.
 
Old 05-06-2009, 12:34 PM   #11
amysaraantony
Member
 
Registered: Apr 2009
Posts: 42

Rep: Reputation: 16
cron is your friend Suncoast

Debian

Last edited by amysaraantony; 05-15-2009 at 09:11 PM.
 
Old 05-06-2009, 01:31 PM   #12
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,064
Blog Entries: 14

Rep: Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248Reputation: 1248
You might want to look at "JOB CONTROL" section of bash man page (type man bash then do a locate for JOB CONTROL in that page).

However talking about the queuing a printer does is sort of the antithesis of the purpose of UNIX/Linux. That is to say UNIX was designed to be a "multiuser/multiprocess" system. Linux was written as a clone of UNIX. What you want to do could help insure resources are available for later processes as they're not tied up by earlier ones but except in extreme situations this is simply not necessary due to the built in process scheduling/clock ticks etc... done at a very low level.

If you have a specific set of jobs with no set time period easily defined in cron then what you might do is write each job so that IT executes the next one after it completes. That is easily doable with scripts and programs. Also you could make job end markers (e.g. touch /tmp/job1end or touch /var/tmp/billybob) then make the next job look for that file - if it isn't there for today (assuming you expect all jobs within a 24 hour period) then the subsequent job simply waits a period of time and checks again. This too is easily accomplished with a while loop in a script.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Background jobs tolano Linux - General 4 04-17-2007 05:52 PM
running jobs on the background feetyouwell Linux - Software 3 08-02-2005 11:36 AM
Background jobs paddyjoy Linux - Newbie 2 07-19-2005 02:46 AM
background jobs seize630 Linux - General 6 10-14-2003 04:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration