If you want to restart JBoss (or Tomcat or Resin etc.) regularly to prevent its unexpected degradation, you might have temptation to insert to crontab something like:
52 3 * * mon pgrep -d" " -f "jboss" | xargs kill; sleep 16; pgrep -d" " -f "jboss" | xargs kill -9; sleep 5; /restart/jbossstart #this doesn't work!
If you test it (as root) it will work, however when activated by cron it will terminate jboss and itself - what a surprise. First command
pgrep -d" " -f "jboss" | xargs kill;
will target all running JBoss processes and itself since cron will run it as shell process which also has 'jboss' string. Let's fix the script and make it bullet-proof. First let's get list of processes we need to terminate, separated by space, to use as argument for kill command:
JBPIDS=$(pgrep -d" " -f "jboss");
Then let's create background (parallel) process which will start JBoss 48 seconds later.
(sleep 48 && /restart/jbossstart &)
Then let's spawn another process which will kill remaining processes 32 second later unless already terminated:
(sleep 32 && kill -9 $JBPIDS &);
And then politely ask processes to terminate:
kill $JBPIDS
So final working version will look like
52 3 * * mon JBPIDS=$(pgrep -d" " -f "jboss"); (sleep 48 && /restart/jbossstart &); (sleep 32 && kill -9 $JBPIDS &); kill $JBPIDS
So now the question is can you let the Java application server run for the whole week and still trust it?