long running script without nohup

You started a long running script and didn’t used nohup? No Problem!
To protect the script against a break caused by i.e. network errors, I usually use nohup. You have the output available in nohup.out. If you already started the script, it’s to late for nohup, but you can disown the job from your shell:

oracle$ ./script.sh 
^Z
[1]+ Stopped ./script.sh
oracle$ bg
[1]+ ./script.sh &
oracle$ jobs 
[1]+ Running ./script.sh &
oracle$ echo $$
17135
oracle$ ps -ef |grep -i [s]cript.sh
oracle 21099 17135 0 22:34 pts/4 00:00:00 /bin/bash ./script.sh
oracle$ disown %1
oracle$ jobs
oracle$ ps -ef |grep -i [s]cript.sh
oracle 21099 17135 0 22:34 pts/4 00:00:00 /bin/bash ./script.sh
oracle$ exit
logout 
# reconnect to server
oracle$ ps -ef |grep -i [s]cript.sh
oracle 21099 1 0 22:34 pts/4 00:00:00 /bin/bash ./script.sh

Now the parent of the process is 1 (init) . Please keep in mind you are not able to connect to the job again and all the output is lost!
Use jobs -l  so see the process id.

If you run something different then a shell script you have the shell between the init and your command:

$ ptree 97757
1 /sbin/init
 97745 -bash
  97757 sqlplus @my_script.sql
   97758 oracleXXX (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))

Please ensure a quit is at the end of your script, otherwise you must kill the session manually.

Much more comfortable are tools like screen or tmux , but both are additional tools (must be installed) and can’t help you if the script is already running.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s