Wednesday 23 August 2017

Background job in bash is shutting down when the firing window is closed: how to avoid this?


You can go quickly to the question starting reading from 'Having said that'
The following are details on my specific needs


I'm on lubuntu oneiric and trying to create a desktop launcher to start / stop tomcat. I'm experiencing problems which are explained in this thread I opened yesterday on lxde forum.


Basically the desktop launcher, which just executes the command
/usr/local/tomcat6/bin/startup.sh
inside a lxterminal window works, but if I close the lxterminal window opened by the launcher and which executed the command .. then tomcat is just shut down.


I would like instead to close that window while having tomcat keeping running of course.
The window needs just to execute the command to start it and nothing else.


Searching inside the whoooole code run by the above command I finally found that the guilty is the following code:


/opt/jdk1.6.0_22/bin/java  \
-Dsolr.solr.home=/home/foo/solr/solr_config/current/master/ \
-Dsolr.data.dir=/home/foo/solr/data \
-Dsolr.solr.home=/home/foo/solr/solr_config/current/master/ \
-Dsolr.data.dir=/home/foo/solr/data \
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager \
-Djava.util.logging.config.file=/usr/local/tomcat6/conf/logging.properties \
-Djava.endorsed.dirs="/endorsed" \
-classpath ":/usr/local/tomcat6/bin/bootstrap.jar:/usr/local/tomcat6/bin/commons-logging-api.jar" \
-Dcatalina.base="/usr/local/tomcat6" \
-Dcatalina.home="/usr/local/tomcat6" \
-Djava.io.tmpdir="/usr/local/tomcat6/temp" \
org.apache.catalina.startup.Bootstrap "" start \
>> "/usr/local/tomcat6"/logs/catalina.out 2>&1 &

In fact if I run this command inside a new lxterminal window, the same behavior is noticed:
A) tomcat starts: if I open a new lxterminal and type:


$ ps aux | grep tomcat
foo 21492 0.4 1.5 934436 47248 pts/2 Sl 11:37 0:03 /opt/jdk1.6.0_22/bin/java -Dsolr.solr.home=/home/foo/solr/solr_config/current/master/ -Dsolr.data.dir=/home/foo/solr/data -Dsolr.solr.home=/home/foo/solr/solr_config/current/master/ -Dsolr.data.dir=/home/foo/solr/data -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=/usr/local/tomcat6/conf/logging.properties -Djava.endorsed.dirs=/endorsed -classpath :/usr/local/tomcat6/bin/bootstrap.jar:/usr/local/tomcat6/bin/commons-logging-api.jar -Dcatalina.base=/usr/local/tomcat6 -Dcatalina.home=/usr/local/tomcat6 -Djava.io.tmpdir=/usr/local/tomcat6/temp org.apache.catalina.startup.Bootstrap start
foo 21666 0.0 0.0 4476 768 pts/4 S+ 11:53 0:00 grep --color=auto tomcat

B) if I close the first lxterminal window (the one in which I ran the long command above) then tomcat shuts down as well. If, from the second lxterminal window, I type again


$ ps aux | grep tomcat
foo 21694 0.0 0.0 4476 772 pts/4 S+ 11:57 0:00 grep --color=auto tomcat

Having said that
the above looong command is just a background job: if I open a lxterminal and simply type


$ top &
[1] 21921

and, while keeping this window opened, I open a second lxterminal window and type


$ ps aux | grep top
foo 21921 0.0 0.0 2452 764 pts/2 T 12:07 0:00 top
foo 21927 0.0 0.0 4476 772 pts/4 S+ 12:08 0:00 grep --color=auto top

I can see 'top' running, while if I close the first window and then from the second one type again


$ ps aux | grep top
foo 21927 0.0 0.0 4476 772 pts/4 S+ 12:08 0:00 grep --color=auto top

the 'top' command is shut down as well.

So is there a way to start a job from a lxterminal window and have this job still running even after closing the lxterminal window which fired it?


To be clear:
I want to open a lxterminal window which executes a command (like top &), close this lxterminal window and still have the executed command running.


For those who read my details on the top page:
the strange is that if I open a lxterminal window, directly type
/usr/local/tomcat6/bin/startup.sh
and then close that window .. then tomcat still runs. Not from the desktop launcher which simply opens an lxterminal window and fire the same command and then it closes!



Answer



SOLVED:


Thanks to your answers I finally solved it: the problem was also that I used interactive shell see this other question of mine.
So the solution was:


A) Put in .profile too what tomcat needs


export PATH=/opt/jdk1.6.0_22/bin:$PATH
export JAVA_HOME=/opt/jdk1.6.0_22
export JRE_HOME=/opt/jdk1.6.0_22
export JAVA_OPTS="$JAVA_OPTS -Dsolr.solr.home=/home/foo/solr/solr_config/current/master/ -Dsolr.data.dir=/home/foo/solr/data"

B) Remove any interactive shell and added nohup and exit:
so my desktop launcher has


[Desktop Entry]
...
Exec=lxterminal --command "bash '/home/foo/scripts/Tomcat/tomcat-startup.sh'"
...

while tomcat-startup.sh basically does:


...
nohup bash "/usr/local/tomcat6/bin/startup.sh"
...
exit

And everything just works fine! :D


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...