Linux – How to run TomEE as a service on RHEL6

bashlinuxrhel6servicetomcat

cat /etc/passwd (last line):

tomee:x:990:987::/home/tomee:/bin/false

I need to make an init.d script to run Tomcat EE like service tomee start.

So I have 2 problems:

  1. How I can execute command (from root) as a tomee user (no bash login) ?

    sudo su – tomee -c "some command"

Doesn't work.

sudo - tomee -s "/bin/bash" -c "some command"

works, but I think it looks horrible.

  1. As far as I said I'm going to use it it /etc/init.d/tomee service bash script, and the problem here I need somehow to have JAVA_HOME envirioment variable set before executing the above command. (So how it could be done if tomee user is system and doesnt have shell?)

And I also have an issue (warning) that couldn't change dir to /home/tomee. Yes, it doesn't exist, but I created the user as a system one.

I'm using an RHEL (not the newest, so there isn't systemd). How do I run TomEE as a service on RHEL6?

UPDATE #1:

[root@localhost httpd]# su tomee /usr/local/tomee-webprofile-1.7.2/bin/startup.sh
[root@localhost httpd]#

And no results. Only

su - tomee -s "/bin/bash" /usr/local/tomee-webprofile-1.7.2/bin/startup.sh

works, but I don't want to see this awful -s "/bin/bash" part.

And moreover, according to JAVA_HOME, if I put it inside init.d script (tomee) so it has sense for root only as init.d scripts run as root, so there will be no JAVA_HOME for the tomee user?

UPDATE #2:

[root@localhost ~]# su tomee -s "/bin/bash" -c "echo hi"

It prints "hi" – it is OK.

[root@localhost ~]# su tomee -c "echo hi"

It prints nothing. Why?

UPDATE #3:

I finally made it working with:

su -s /bin/bash $TOMCAT_USER $TOMCAT_CATALINA_SH stop

I noticed oracle db's init scripts use the same approach, so I think it's ok.

BTW, I still can't understand why we need to specify shell while we executing a *.sh file as there is the first line where #!/bin/bash etc.

Best Answer

service start tomee is run by root to start the tomee service. It's not the actual command the init script would run. The init script for this is included below.

JAVA_HOME should be set in the init script.

/etc/init.d/tomee (needs to be marked executable):

#!/bin/bash
# description: TomEE Start Stop Restart
# processname: tomee
# chkconfig: 234 20 80
TOMEE_USER=tomee
JAVA_HOME=/usr/java/jdk1.6.0_33
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CATALINA_HOME=apache-tomee-webprofile-1.0.0

case $1 in
start)
/bin/su $TOMEE_USER $CATALINA_HOME/bin/startup.sh
;; 
stop)   
/bin/su $TOMEE_USER $CATALINA_HOME/bin/shutdown.sh
;; 
restart)
/bin/su $TOMEE_USER $CATALINA_HOME/bin/shutdown.sh
/bin/su $TOMEE_USER $CATALINA_HOME/bin/startup.sh
;; 
esac    
exit 0

Adapted from http://www.davidghedini.com/pg/entry/apache_tomee_on_centos_6.

Edit CATALINA_HOME above to be the absolute path of your TomEE install. Make sure it is readable by the tomee user and that the webapps, logs, temp, and work directories within it are writable by the tomee user.