Start tomcat on boot in Linux
As you all know, all startup scripts are found in /etc/init.d folder. To start tomcat on linux boot then below are primary steps:
- create tomcat script that starts,stop,restarts tomcat
- create symbolic link of tomcat in rc.d folders. Linux system will run all scripts defined in rc.d folders
Create Tomcat Auto startup script:
- login with root user
- create a file name with tomcat in /etc/init.d
-
cd /etc/init.d vi tomcat
-
- if you set the JAVA_HOME and CATALINA_HOME is bash_profile then you no need to set in /etc/init.d/tomcat script
- tomcat script is:
-
###export CATALINA_HOME="/usr/local/apache-tomcat-7.0.61" ## export JAVA_HOME="/usr/local/jdk7" export CATALINA_HOME="/usr/local/apache-tomcat-7.0.61" ERROR=0 case "$1" in start) echo $"Starting Tomcat" sh $CATALINA_HOME/bin/startup.sh ;; stop) echo $"Stopping Tomcat" sh $CATALINA_HOME/bin/shutdown.sh ;; restart) sh $CATALINA_HOME/bin/shutdown.sh sh $CATALINA_HOME/bin/startup.sh ;; *) echo $"Usage: $0 {start|stop|restart}" exit 1 ;; esac exit $ERROR
-
- chmod 775 tomcat
- create symbolic link of the tomcat script in rc.d directory
Add Tomcat script to rc.d directory
We need to create two symbolic links
- K{level}tomcat – K76tomcat means it kills the process after K75
- S{level}tomcat – S98tomcat means it starts the tomcat after S97.
Run the below Symbolic commands in Terminal:
- ln -s /etc/init.d/tomcat /etc/rc.d/rc6.d/K26tomcat
- ln -s /etc/init.d/tomcat /etc/rc.d/rc0.d/K26tomcat
- ln -s /etc/init.d/tomcat /etc/rc.d/rc3.d/S81tomcat
- ln -s /etc/init.d/tomcat /etc/rc.d/rc2.d/S81tomcat
- ln -s /etc/init.d/tomcat /etc/rc.d/rc5.d/S81tomcat
I used to prefer to start the tomcat after mysql start.
What are the rc.d directories?
you can found below list of directories in /etc/rc.d and represents run levels with different modes of operating system
- /etc/rc.d/rc0.d – 0 – HALT or shutdown the system – All symlinks with K extensions must go here
- /etc/rc.d/rc1.d – 1 – Single mode system
- /etc/rc.d/rc2.d – 2 – Multi user mode with out networking
- ,/etc/rc.d/rc3.d – 3 – Multi user mode with networking enabled. most of the all startup scripts will go here.
- /etc/rc.d/rc4.d – 4 – Not used
- /etc/rc.d/rc5.d – 5 – GUI enabled and starts system with normal mode. Add startup scripts in this.
- /etc/rc.d/rc6.d – 6 – Reboots the system and most scripts start with K level goes into this folder to kill the process
We need to create Symbolic links
- K{level}tomcat – create in rc0d, rc6.d
- S{level}tomcat – Create in rc3.d,rc5,d,rc2.d
4 thoughts on “start tomcat on boot in linux”
Leave a Reply
You must be logged in to post a comment.
Thanks!!!
Thanks for the article, but who sends the “start” or “stop” arguments to the script?
Thank you so much.
A doubt, if we ran the update-rc tomcat defaults command would it work too?
I entered the above, but I get “[FAILED] Failed to start tomcat.service” everytime I reboot. Systemctl Status:
8< – – – – – –
* tomcat.service
Loaded: loaded (/etc/init.d/tomcat; generated)
Active: failed (Result: exit-code) since
Docs: man:systemd-sysv-generator(8)
Process: 358 ExecStart=/etc/init.d/tomcat start (code=exitied, status=203/EXEC)
systemd[1]: Starting tomcat.service
systemd[1]: tomcat.service: Control process exited, code=exited, status 203/EXEC
systemd[1]: tomcat.service: Failed with result ‘exit-code’.
systemd[1]: Failed to start tomcat.service.
8< – – – – –
I put "/etc/init.d/tomcat start" in /etc/rc.local and it starts fine.
There is no rc.d directory, but I did add the S & K scripts to the appropriate rc?.d directories.
RASPIOS 10 (BUSTER) Lite version on Raspberry Pi Zero.