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.
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. |
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.
1 > find . -name core -exec rm {} ; & 2 [1] 543 3 > |
Explanation
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.
The prompt returns immediately. The shell waits for user input.
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 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.
(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
The jobs command lists the currently active jobs.
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.
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.
After sleep has been running for 25 seconds, the job will complete and a message saying that it has finished appears on the screen.
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.).
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
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.
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
The jobs command lists currently running processes, called jobs.
The job numbered [1] is brought to the foreground. The number is preceded with a percent sign.
The sleep command is stopped by pressing ^Z. The sleep command is not using the CPU and is suspended in the background.
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.
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
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
The sched command will schedule the echo command to be executed at 5 PM.
The script, logfile.sc, is scheduled to be executed 1 hour and 30 minutes from now.
The sched command displays the scheduled events, in numeric order, the last one first.
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.
3.144.97.187