Arian
Arian

Reputation: 7749

Running a process in background, linux ubuntu

I want to run a .jar file so what I do is that I put "&" at the very end of the command (actually there's no need to log the output , I only want to be able to disconnect from the remote server which hosts my java program, the program itself saves the result after being finished)

I do as it follows , but it doesn't run in the background and it keeps me waiting :

java -Xmx72G -cp //home/arian/rapidminer/lib/rapidminer.jar com.rapidminer.RapidMinerCommandLine -f //home/arian/RMRepository/testRemote.rmp &

Any idea that why it doesn't work ?

Thanks , Arian

Upvotes: 0

Views: 5875

Answers (4)

Cynical
Cynical

Reputation: 9588

I agree it should work as it is, but I had problems with Java running in background too. My solution was to use the screen utility (which normally is installed in most Linux distributions) where you can open a shell from which you can detach. If I remember well the commands are something like this (but there is a good manpage too)

screen -S myCustomName # runs a new shell called myCustomName
CTRL + D # detach from the current screen instance
screen -ls # list active screen instances
screen -r myCustomName # reattach to the screen instance.

Hope it will solve your problem.

Upvotes: 1

Alexandre Lavoie
Alexandre Lavoie

Reputation: 8771

You can use JSVC, this is an utility interresting to daemonize Java applications

http://commons.apache.org/daemon/jsvc.html

It will give you a var pid file, useful to create a real start/stop script.

EDIT : Other solution, maybe could help

Here is a very old start/stop script I've done for Slackware Linux on embedded systems :

#!/bin/sh

application_start() {
  cd /usr/local/YOURHOME
  /usr/lib/java/bin/java \
        -Xmx72G \
        -classpath //home/arian/rapidminer/lib/rapidminer.jar \
        com.rapidminer.RapidMinerCommandLine \
        -f //home/arian/RMRepository/testRemote.rmp &
  echo -n "Starting App daemon:  $CMDLINE"
  ps -Ao pid,command | grep java | grep com.rapidminer.RapidMinerCommandLine | awk '{print $1}' > /var/run/app.pid
  echo
}

application_stop() {
  echo -n "Stopping DataBaseSynchronizerClient daemon..."
  kill `cat /var/run/DataBaseSynchronizerClient.pid`
  echo
  sleep 1
  rm -f /var/run/DataBaseSynchronizerClient.pid
  killall DataBaseSynchronizerClient 2> /dev/null
}

application_restart() {
  application_stop
  sleep 1
  application_start
}

case "$1" in
'start')
  application_start
  ;;
'stop')
  application_stop
  ;;
'restart')
  application_restart
  ;;
*)
  echo "usage $0 start|stop|restart"
esac

Upvotes: 2

Anders R. Bystrup
Anders R. Bystrup

Reputation: 16080

What do you mean by "it keeps me waiting"? Does the RapidMinerCommandLineby any chance read from stdin or another stream?

If you want to run a process in the background and disconnect from the tty session you should use nohup, eg.:

nohup java -Xmx.... com.rapidminer.RapidMinerCommandLine &

(Do remember the & at the end!)

You may add ... 1> /dev/null before the & to disregard all stdout.

You could also consider the screen utility, which allows you to dis- and reconnect to the session, but that's more usable with (semi-)interactive sessions.

(Also, quite a hefty max heap size you're specifying?)

Cheers,

Upvotes: 2

ams
ams

Reputation: 25599

I don't know why it wouldn't work. It really should, in most shells.

Anyway, if you intend to disconnect you'll usually find that just putting the job in the background is not enough: the disconnect will close the console (which will break many programs, alone) and send a SIGHUP signal (which will cause just about any program to exit).

You should considered using nohup to run the program (with the &). Alternatively, if you ever do need to come back and interact with the program later then screen or byobu might fit the bill better. Yet another alternative might be to add the task to your crontab.

Upvotes: 2

Related Questions