Sunday, December 27, 2015

Running a Command or Script in the Background on Linux

Many times we (DBAs) are in a situation when we need to run some command or script from the command prompt on the Linux, and during command/script execution; we face disconnection of session and our process stops in the middle leaving a lot of mess for us to clear. To avoid this kind of situation, it is advised to execute long running scripts or commands in the background using Linux “nohup” command. In the following example, I have a script “recovery.sh” that contains RMAN commands to restore and recover a huge database on a server, so I would execute this script in background as follows
$ nohup ./recovery.sh &

Immediately after executing this command, my session would return to the command prompt, and my script would keep executing in the background. A file “nohup.out” would get created in the current directory that will contain all the output returned by the script. We can monitor the progress by using “tail” command on this output file
$ tail –f nohup.out

Likewise, if I am applying a patch using opatch, I can run command in the background as follows
[root@myserver]# nohup opatch apply &

Note that if we are running a command/script that needs an input during its execution, above example would not work for that because using nohup in above fashion does not return any prompt to get any input from the user.


No comments: