10.4. 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 10.12.

Table 10.12. 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 Represents
%n Job number n.
%string Job name starting with string.
%?string Job name containing string.
%% Current job.
%+ Current job.
%- Previous job, before current job.

10.4.1. Background Jobs

The Ampersand

If a command takes a long time to complete, you can append the command with an ampersand and the job will execute in the background. The tcsh prompt returns immediately and now you can type another command. Now the two commands are running concurrently, one in the background and one in the foreground. They both send their standard output to the screen. If you place a job in the background, it is a good idea to redirect its output either to a file or pipe it to a device such as a printer.

Example 10.40.
1  >  find . -name core -exec rm {} ; &
2  [1]  543
3  >

Explanation

  1. The find command runs in the background. (Without the -print option, the find command does not send any output to the screen).[a]

    [a] The find requires a semicolon at the end of an exec statement. The semicolon is preceded by a backslash to prevent the shell from interpreting it

  2. The number in square brackets indicates this is the first job to be run in the background and the PID for this process is 543.

  3. The prompt returns immediately. The shell waits for user input.

The Suspend Key Sequence

To suspend a program, the suspend key sequence, ^Z, is issued. The job is now suspended (stopped), the shell prompt is displayed, and the program will not resume until the fg or bg commands are issued. (When using the vi editor, the ZZ command writes and saves a file. Do not confuse this with ^Z, which would suspend the vi session.) If you try to log out when a job is suspended, the message "There are suspended jobs" appears on the screen.

The jobs Command and the listjobs Variable

The tcsh built-in command, jobs, 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 10.41.
(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 Control-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 10.25 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
    								

10.4.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 10.42.
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 matachracters, called regular expression metacharacters, for pattern matching. These should not be confused with shell metacharacters.

10.4.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 time with a + sign; i.e., relative to the current time, and with a - sign, the event is removed from the list.[9]

[9] From the tesh 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 10.43.
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 (Control-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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.97.187