The Linux operating system was designed to be a multitasking operating system ”that is, to allow multiple tasks to be executed together. Until a few years ago, end users of the system were not directly exposed to this aspect of the operating system.
As far as Linux is concerned , the job-control features of Bash allow users to take advantage of the multitasking features of the operating system. This section looks at managing multiple tasks, both attended and unattended. Let s start with an overview of how processes work in Linux.
Processes, as you saw in the Process Management Commands section, are programs executing in memory. A process may be associated with a terminal (for example, the date command is associated with the terminal because it prints its standard output to the terminal). The association of a process with a terminal also means that all the signals delivered to the terminal s group of processes will be delivered to the process in question.
Some processes, such as servers (or daemons), are seldom associated with a terminal. These processes are typically started as part of the system boot process, and they run during the entire time that the system is up and write output to log files. When a user starts a process (that is, when the user runs a command), the command is associated with the terminal and is, therefore, also described as running in the foreground.
While a process is running in the foreground, the shell does not return a prompt until the process has completed execution. However, a process may also be started such that the prompt is returned immediately; in this case, the process is called a background process.
To run a process as a background process, use the ampersand (&) character after the command:
$bash ls R / &
This indicates to the shell that the process must be disassociated from the terminal and executed as a background process. Its output continues to be written to the terminal.
Job control is a feature of Bash that allows the user to start and manage multiple programs at the same time rather than sequence their execution. You can suspend a program using Ctrl+Z , and you can send it to the background or foreground (using the bg and fg commands) or even leave it suspended . It is also possible to list all of the jobs (processes) started and terminate some of them.
Managing Processes with Job Control
Try using job control to manage a long-running process, the ls “R / command, which recursively lists all the files and directories on the system:
Step 1: Stopping the process
$ ls R / ^Z + Stopped ls R /
Step 2: Listing the jobs
$ jobs + Stopped ls R /
Step 3: Sending the job to background
$ bg %1 + ls R / &
Step 4: Bringing the job to foreground and stopping it
$ fg %1 ls R / ^Z + Stopped ls R /
Step 5: Killing a stopped job
$ kill s SIGKILL %1 $ + Killed ls R /
How it works
Start the program ls with the “R option. After a while, we decide to suspend the program using Ctrl+Z . The jobs command displays the current jobs and their status.
Use the bg command to send the process to the background. After a while, we decide to bring the process back to the foreground, for which we use the fg command. Both bg and fg take an argument that indicates the job number. The %1 argument indicates that we are referring to job number 1.
Finally, having had enough of the process, we suspend it once again and kill it (using the kill command).
Note that the job control commands are built-in commands, and not external commands.
Often, it is not necessary (or not possible) for the user to be present when a task needs to execute. For example, if a user wants to have a certain script executed at midnight to take advantage of the spare CPU cycles, what he or she needs is a mechanism by which the task can be scheduled and executed unattended. Alternatively, if a certain task takes hours to complete and may not require any user input, it is not necessary for the user to remain logged on until the task is complete.
You can use the cron utility to execute tasks automatically at arbitrary times, and even repeatedly if required. The cron daemon is a system process that runs at all times in the background, checking to see if any processes need to be started on behalf of users. You can schedule tasks for cron by editing the crontab file.
Let s schedule a cron job that needs to be started every Monday and Thursday at 11:55 PM to back up our system:
$ crontab e No crontab for deepakt using an empty one
This brings up the vi editor, using which we add the following crontab entry:
55 23 * * 1,4 /home/deepakt/mybackup >/home/deepakt/mybackup.out 2>&1
Save the file and exit the editor:
crontab: installing new crontab $ crontab -l # DO NOT EDIT THIS FILE - edit the master and reinstall. # (/tmp/crontab.6642 installed on Fri Jan 17 05:09:37 2003) # (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $) 55 23 * * 1,4 /home/deepakt/mybackup /home/deepakt/mybackup.out 2>&1
How it works
We need to use the crontab command to create new cron jobs. The “e option pops up the vi editor that allows us to add the new cron job. The entry for a cron job consists of six columns :
The first five columns indicate the time at which the job should execute and the frequency ”the minute (0 “59), the hour (0 “23), the day of the month (1 “31), the month of the year (1 “12), and the day of the week (0 “6, with 0 indicating Sunday). An asterisk represents all logical values, so asterisks appear for the day of the month and the month of the year because this job needs to run during all months of the year.
The last column indicates the actual command to be invoked at these times. You need to specify the full command, with the complete path leading to your backup program, and also redirect the output to a log file.
The 2 >& 1 indicates that the standard error is also redirected to the same log file.
The nohup command can be used to execute tasks that need to continue to execute even after the user has logged out:
$ nohup ls R / &
The nohup command is quite straightforward, in that it takes the program to be executed as the argument. You need to send the whole process to the background by using the & operator. The standard output and error of the nohup command will be written to the user s home directory in a file called nohup.out .