Auto Start Service When Boot

We always need to auto start some service when we boot our system.(like tomcat)
We can set it to system auto start programs.

Create the init script in /etc/init.d/tomcat8 with the contents as per below (your script should work too but I think this one adheres more closely to the standards).

This way Tomcat will start only after network interfaces have been configured.

  • Init script contents:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash

### BEGIN INIT INFO
# Provides: tomcat8
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/Stop Tomcat server
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin

start() {
sh /usr/local/tomcat/bin/catalina.sh start
}

stop() {
sh /usr/local/tomcat/bin/catalina.sh stop
}

case $1 in
start|stop) $1;;
restart) stop; start;;
*) echo "Run as $0 <start|stop|restart>"; exit 1;;
esac
  • Change its permissions and add the correct symlinks automatically:
1
2
chmod 755 /etc/init.d/tomcat8
update-rc.d tomcat8 defaults

if you use

1
2
sh /usr/local/tomcat/bin/start.sh
sh /usr/local/tomcat/bin/stop.sh

all your projects that deploy on tomcat will disappear

And from now on it will be automatically started and shut down upon entering the appropriate runlevels. You can also control it with service tomcat8 <stop|start|restart>