Total Pageviews

Wednesday 8 August 2012

Submit a Task to Run in the Background

The nohup command can be used to submit a task as a background process. This is useful for long-running Oracle jobs, because it frees up your command prompt so that you can do other work. It is especially useful when you are dialed in to an Oracle server using a modem, and you want to free up your terminal session.
Assume that you have a script named run_sql.ksh that executes SQL*Plus commands. The following nohup command can be used to submit that script for background processing:
nohup sh run_sql.ksh > logfile.lst 2>&1 &
There's obviously more to this command than just nohup, and it's important to understand just what each element of the command is doing. For this example, the elements are as follows:
nohup
Submits the task so that it continues to run even after you disconnect your terminal session.
run_sql.ksh
Specifies the Unix shell script that you want to run in the background.
> logfile.lst
Redirects standard output to the specified file.
2 > &1
Redirects standard error messages to the standard output device. The 2 represents the standard error device, and 1 represents the standard output device.
&
Runs the task in the background.
You need to have a space in front of the trailing ampersand (&) character, and it's that & that causes the task to run as a background task. The nohup command is frequently used with background tasks, because without it all your background tasks would terminate the moment you logged off of Unix. The nohup command allows a task to continue running long after you've logged off and gone home for the night.

Watch the Execution of a Background Process
If you've redirected the output of a background job to a file, you can monitor the execution of that background process by using the tail -f command. For example:
tail -f logfile.lst
The tail -f command continuously displays new lines as they are written to the output file, allowing you to easily watch the progress of your background task. To exit the tail -f command, enter Ctrl-C at any time.

No comments:

Post a Comment