Background, running jobs in

From FreeBSDwiki
Revision as of 18:19, 16 August 2012 by DavidYoung (Talk | contribs)
Jump to: navigation, search

There are three basic methods to run jobs in the background: by ampersand from the shell, by the cron scheduler , and by the at scheduler. We will cover the basics of all three methods here.



This is the simplest method: when executing a command from the shell, simply append an ampersand to it, and the shell will execute it in the background and return you a process id number:

ph34r# /usr/bin/perl /home/jimbo/ &
[1] 84190
ph34r# ps waux | grep 84190
root       84190  0.0  0.1  1460   788  ??  Is   4:02PM   0:02.19 /usr/bin/perl /home/jimbo/

However, if you should exit the shell, the background process will also be killed:

ph34r# exit
% ps waux | grep 84190

Our job got killed when we exited the shell because it was a child process of the shell. If we wanted to be able to close the shell and still keep the job running, we should have used at.

Another interesting use of the ampersand character from the shell is to cause one task to run after another task finishes:

ph34r# echo this && echo is && echo just && echo an && echo example

You can also append a single ampersand at the end of a bunch of double-ampersand linked commands in order to cause the whole kit and kaboodle to run in the background, while still making each individual command wait on the last one to finish before it starts.


cron is the system's automated scheduling utility for repeated tasks. Its usage is complex enough that it's better for you to visit the cron article itself if you want to learn how to use it; but in a nutshell special files called crontabs are used to tell cron what tasks you want it to perform, under which user contexts, and when. cron jobs are not bound to any particular shell, and will fire off on schedule without any user intervention and regardless of whether any particular user - or anyone at all - is interactively logged into the system.


at is the system's scheduling utility for single-execution jobs: when you want something to happen at some particular point in time, but only once, you schedule it with at. Once every five minutes, cron fires up atrun, which checks the at queue and pops jobs that are due to process off the queue and runs them.

at jobs are not children of any shell process, and just like cron jobs, will run regardless of whether any particular user or even any user at all is logged into the system interactively. This makes at a handy way to cause a long-running job to start up immediately, while allowing you to close your shell and go elsewhere (or to make sure a REALLY long-running job will continue running even if your shell closes for some reason out of your control, such as an internet outage).

When using at to schedule a job to run immediately, however, you may get annoyed by having to wait for the next 5-minute mark for the job to run: if so, you can always fire up atrun yourself, rather than waiting for cron to do it for you. This won't hurt anything, and it will go ahead and pop jobs that are due to run out of the queue immediately so you don't have to sit around and wait.

ph34r# date
Fri Feb 29 15:50:29 EST 2008
ph34r# at now
/bin/sh /home/jimbo/
^D (user pressed ctrl-D)
Job 9 will be executed using /bin/sh
ph34r# atq
Date                            Owner           Queue   Job#
Fri Feb 29 15:50:48 EST 2008    root            c       9
ph34r# /usr/libexec/atrun
ph34r# atq
ph34r# exit
% ps waux | grep some_long_job
root       20472  0.0  0.1  1460   788  ??  Ss   12:02PM   0:02.19 /bin/sh /home/jimbo/

As you can see, we scheduled the job for immediate execution, saw that it was pushed into the queue, manually fired off atrun, then checked to see that it had popped from the queue. Seeing that it had popped from the queue, we exited our shell (note the transition from the "ph34r# " prompt to the "% " prompt) and checked to see that the job was still running - and sure enough, it was. Miller time!

Research Reveals Largest Ancient Dam Built by Maya Civilization

Recent excavations, sediment coring and mapping by a multi-university team led by the University of Cincinnati at the pre-Columbian city of Tikal, a paramount urban center of the ancient Maya, have identified new landscaping and engineering feats, including the largest ancient dam built by the Maya of Central America.

[Research Reveals Largest Ancient Dam Built by Maya Civilization]

[ - good, positive news, inspirational stories, articles]

Cat Saves Owner Hours After Adoption

A newly-adopted cat repaid his owners loving gesture earlier this month by saving her from a medical emergency just hours after he was brought home, the Green Bay Press Gazette reports.

[Cat Saves Owner Hours After Adoption]

[ - good, positive news, inspirational stories, articles]

11 Things You Should Start Doing for Yourself Today

Enjoy everything that happens in your life, but never make your happiness or success dependent on an attachment to any person, place or thing. Wayne DyerYou deserve to live a more balanced, harmonious and happier life, starting today and starting now. Today, not tomorrow, nor the day after tomorrow is where your life is, where your life starts.

[11 Things You Should Start Doing for Yourself Today]

[ - good, positive news, inspirational stories, articles]

Discovering My Own Values

At the end of your life a friend once asked, What do you hope to have happened? I thought it a great question and decided to give him a thoughtful answer, so I pocketed it for later and bought myself a month for the assignment. For a while my mind flooded with questions of plot.

[Discovering My Own Values]

[ - good, positive news, inspirational stories, articles]

What Ive Learned About Learning

We learn more by looking for the answer to a question and not finding it than we do from learning the answer itself. ~Lloyd AlexanderI am a teacher and an avid learner, and Im passionate about both.Im a teacher because I help Eva homeschool our kids OK, she does most of the work, but I do help, mostly with math but with everything else too.

[What Ive Learned About Learning]

[ - good, positive news, inspirational stories, articles]

Personal tools