Previous Section  < Day Day Up >  Next Section

9.18. TC Shell Job Control

Job control is a powerful feature of the TC shell that allows you to run programs, called jobs, in the background or foreground. Normally, a command typed at the command line is running in the foreground and will continue until it has finished. If you have a windowing program, job control may not be necessary, because you can simply open another window to start a new task. On the other hand, with a single terminal, job control is a very useful feature. For a list of job commands, see Table 9.20.

Table 9.20. Job Control Commands

Command

Meaning

jobs

Lists all the jobs running

^Z (Ctrl-Z)

Stops (suspends) the job; the prompt appears on the screen

bg

Starts running the stopped job in the background

fg

Brings a background job to the foreground

kill

Sends the kill signal to a specified job

Argument to jobs Command

%n

Job number n

%string

Job name starting with string

%?string

Job name containing string

%%

Current job

%+

Current job

%-

Previous job, before current job


9.18.1 The jobs Command and the listjobs Variable

The tcsh built-in jobs command displays the programs that are currently active and either running or suspended in the background. Running means the job is executing in the background. When a job is suspended, it is stopped; it is not in execution. In both cases, the terminal is free to accept other commands. If you attempt to exit the shell while jobs are stopped, the warning, "There are suspended jobs" will appear on the screen. When you attempt to exit immediately a second time, the shell will go ahead and terminate the suspended jobs. You set the tcsh built-in listjobs variable if you want to automatically print a message when you suspend a job.

Example 9.98.

(The Command Line)



1   > jobs

2   [1]  +           Suspended    vi filex

    [2]  -           Running      sleep 25

3   > jobs -l

    [1]  +   355     Suspended    vi filex

    [2]  -   356     Running      sleep 25

4   [2] Done                      sleep 25

5   > set listjobs = long

    > sleep 1000

    Press Ctrl-Z to suspend job

    [1]  +  3337     Suspended      sleep 1000

    >

6   > set notify


EXPLANATION

  1. The jobs command lists the currently active jobs.

  2. The notation [1] is the number of the first job; the plus sign indicates that the job is not the most recent job to be placed in the background; the dash indicates that this is the most recent job put in the background; Suspended means that this job was stopped with ^Z and is not currently active.

  3. The –l option (long listing) displays the number of the job as well as the PID of the job. The notation [2] is the number of the second job, in this case, the last job placed in the background. The dash indicates that this is the most recent job. The sleep command is running in the background.

  4. After sleep has been running for 25 seconds, the job will complete and a message saying that it has finished appears on the screen.

  5. The tcsh listjobs variable, when set to long, will print the number of a job as well as its process id number when it is suspended. (See Table 9.26 on page 514 for a list of built-in tcsh variables.).

  6. Normally the shell notifies you if a job is stopped just before it prints a prompt, but if the shell variable notify is set, the shell will notify you immediately if there is any change in the status of a background job. For example, if you are working in the vi editor, and a background job is terminated, a message will appear immediately in your vi window like this:

    
    [1]      Terminated                 sleep 20
    
    

9.18.2 Foreground and Background Commands

The fg command brings a background job into the foreground. The bg command starts a suspended job running in the background. A percent sign and the number of a job can be used as arguments to fg and bg if you want to select a particular job for job control.

Example 9.99.

1   > jobs

2   [1] + Suspended                 vi filex

    [2] - Running                   cc prog.c -o prog

3   > fg %1

    vi filex

    (vi session starts)

4   > kill %2

    [2] Terminated                  c prog.c -o prog

5   > sleep 15

    (Press ^z)

     Suspended

6   > bg

    [1] sleep 15 &

    [1] Done   sleep 15


EXPLANATION

  1. The jobs command lists currently running processes, called jobs.

  2. The first job stopped is the vi session, the second job is the cc command.

  3. The job numbered [1] is brought to the foreground. The number is preceded with a percent sign.

  4. The kill command is built-in. It sends the TERM (terminate) signal, by default, to a process. The argument is either the number or the PID of the process.

  5. The sleep command is stopped by pressing ^Z. The sleep command is not using the CPU and is suspended in the background.

  6. The bg command causes the last background job to start executing in the background. The sleep program will start the countdown in seconds before execution resumes.[a]

    [a] Programs such as grep, sed, and awk have a set of metacharacters, called regular expression metacharacters, for pattern matching. These should not be confused with shell metacharacters.

9.18.3 Scheduling Jobs

The sched built-in command allows you to create a list of jobs that will be scheduled to run at some specific time. The sched command, without arguments, displays a numbered list of all the scheduled events. It sets times in the form hh:mm (hour:minute) where hour can be in military or 12-hour AM/PM format. Time can also be specified as a relative to the current time with a + sign. With a – sign, the event is removed from the list.[8]

[8] From the tcsh man page: "A command in the scheduled-event list is executed just before the first prompt is printed after the time when the command is scheduled. It is possible to miss the exact time when the command is to be run, but an overdue command will execute at the next prompt."

FORMAT


sched

sched [+]hh:mm  command

sched -n


Example 9.100.

1   > sched 14:30  echo '^G Time to start your lecture!'

2   > sched 5PM  echo Time to go home.

3   > sched +1:30 /home/ellie/scripts/logfile.sc

4   > sched

    1     17:47 /home/scripts/logfile.sc

    2     5PM  echo Time to go home.

    3     14:30  echo '^G Time to start your lecture!'

5   > sched -2

    > sched

    1     17:47 /home/scripts/logfile.sc

    2     14:30  echo '^G Time to start your lecture!'


EXPLANATION

  1. The sched command schedules the echo command to be executed at 14:30. At that time a beep will sound (Ctrl-G)[a] and the message will be displayed.

    [a] To get the ^G into the echo statement, type Ctrl-M, followed by Ctrl-V, followed by Ctrl-G.

  2. The sched command will schedule the echo command to be executed at 5 PM.

  3. The script, logfile.sc, is scheduled to be executed 1 hour and 30 minutes from now.

  4. The sched command displays the scheduled events, in numeric order, the last one first.

  5. With a numeric argument, sched will remove the numbered job from the scheduled list. Job number 2 was removed, as shown in the output of sched.

    Previous Section  < Day Day Up >  Next Section