UNIX - Process in background

It is very useful to run Unix commands in the background if it is going to take a long time to process or if you are working from a remote system, with dial up connection (you never know when your computer disconnects!!). It is very easy to submit a job in the background in Unix. A "&" at the end of the command puts the process into background. One requirement is that the job cannot be interactive.

Let's consider you need to take a database export in the background. The command would be

exp parfile=myparfile.par > /tmp/exp.out &

here the parameter file has all the required parameters, including the username and password (make sure you have the permissions on the password file set to 600, so that other user cannot read this file). The output from the export is redirected to a file /tmp/exp.out.

Now the problem is if you, terminate the session, the background process also will die, because the process we started is a child of this parent shell. The solution would be to tell UNIX, not to hang up, even if I terminate the parent session. It is done by using the command "nohup".

nohup exp parfile=myparfile.par > /tmp/exp.out &

Here is an idea to run SQL scripts or PL/SQL procedures in the background. To run these, you need to login to the database. The following script will run a SQL script, which is passed in as a parameter,  without displaying the username and password in the process list. (To see the oracle process list, do "ps -ef | grep ora", or to see the sqlplus sessions do "ps -ef | grep sqlplus")

#! bin/ksh
# Script Name : runscript.sh
# To run a SQL script - Script name is the first parameter
#
sqlplus -s <<EOF
myusername/mypassword
spool $1.lst
set serveroutput on time on feedback on
@/home/biju/app/$1.sql
exit
EOF
echo "Script Completed" | mailx pagernumber@mypager.com

To run mysql.sql script using the about script, in the background, the command would be

nohup ./runscript.sh   "/mypath/mysql" > runscript.out &

Since the username and password are in the second line, it will not be visible in the process list. As earlier make sure the file permissions are 700 (chmod 700 runscript.sh).

You may even add a line towards the end of runscript.sh to page you when the script run is complete, so that you do not have to baby sit some long running processes.

Search BijooS.com Exact Match Search      
Home Oracle DBA TKMCE Alumni H1B Info Guestbook

Biju Thomas is Oracle7.3 OCP, Oracle8 OCP, 
Oracle8i OCP and Oracle9i OCA/OCP Certified DBA

Questions/Comments? Write to webmaster@bijoos.com. © The scripts, tips and articles appearing on BijooS.com cannot be reproduced elsewhere without the prior permission from the webmaster.