-2

I normally run my java service on CentOS box using nohup like below:

nohup java -jar myapp-0.0.1-SNAPSHOT.jar server my-configure.yml > outfile &

However, if the server restarts the service won't restart on its own. Is there an automated way in CentOS to start this java service at boot?

030
  • 5,901
  • 13
  • 68
  • 110
birdy
  • 117
  • 5
  • 1
    What version of CentOS? – freiheit Nov 22 '14 at 19:47
  • Does `/etc/rc.d/rc.local` reside on your system? Could you answer to @freiheit question? – 030 Nov 23 '14 at 10:50
  • The best way to handle this is just like all the other services on your system are handled. If it's CentOS 7, then a systemd file; if it's CentOS 6 or lower, then an init script. – freiheit Nov 23 '14 at 23:09

1 Answers1

3

Copy the script to /etc/rc.d/rc.local, reboot and it will run at boot. E.g.

[vagrant@localhost ~]$ cat /etc/rc.d/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local

sudo touch /tmp/helloworld

creates the /tmp/helloworld file once the system has been rebooted:

[vagrant@localhost ~]$ ls /tmp/
helloworld 

30.3. Running Additional Programs at Boot Time

The /etc/rc.d/rc.local script is executed by the init command at boot time or when changing runlevels. Adding commands to the bottom of this script is an easy way to perform necessary tasks like starting special services or initialize devices without writing complex initialization scripts in the /etc/rc.d/init.d/ directory and creating symbolic links.

The /etc/rc.serial script is used if serial ports must be setup at boot time. This script runs setserial commands to configure the system's serial ports. Refer to the setserial man page for more information. 
030
  • 5,901
  • 13
  • 68
  • 110