Start tomcat at startup with administrative privileges

I need one process run before log in to system. How to run it like services? how do I make services in Linux?

In Ubuntu and Fedora? The service is customized tomcat

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

To run a service without or before logging in to the system (i.e. “on boot”), you will need to create a startup script and add it to the boot sequence.
There’s three parts to a service script: start, stop and restart.
The basic structure of a service script is:

#!/bin/bash
#
RETVAL=0;

start() {
echo “Starting <Service>”
}

stop() {
echo “Stopping <Service>”
}

restart() {
stop
start
}

case “$1″ in
start)
  start
;;
stop)
  stop
;;
restart)
  restart
;;
*)

echo $”Usage: $0 {start|stop|restart}”
exit 1
esac

exit $RETVAL

Once you have tweaked the script to your liking, just place it in /etc/init.d/
And, add it to the system service startup process (on Fedora, I am not a Ubuntu user, >D):

chkconfig -add <ServiceName>

Service will be added to the system boot up process and you will not have to manually start it up again.

Cheers!

Method 2

Depending on init system, you create init script differently. Fedora gives you upstart and systemd to choose from, and of course SysV compatibility.

Upstart

  • create service definition file as /etc/init/custom-tomcat.conf
  • put inside:
    start on stopped rc RUNLEVEL=3
    respawn
    exec /path/to/your/tomcat --and --parameters

And your Tomcat should start on system start.

Systemd

  • create service definition in /etc/systemd/system/custom-tomcat.service
  • put inside:
    [Service]
    ExecStart=/path/to/your/tomcat --and --parameters
    Restart=always
    
    [Install]
    WantedBy=multi-user.target

and enable your service using systemctl enable custom-tomcat.service. It will be started every normal boot.

Of course there are few more configuration options for both init systems, you can check those in their documentation.

Method 3

Tomcat is a fairly common service, I’d recommend looking at the init script provided by the distro already. Chances are it works with your customized binary, with little to no tweaking.

Method 4

If you have a cron daemon, one of the predefined cron time hooks is @reboot, which naturally runs when the system starts. Run crontab -e to edit your crontab file, and add a line:

@reboot /your/command/here

Method 5

For simply running a script after the computer started but before a user logs in, you can simply edit the script /etc/rc.local which is meant to solve exactly this task.

Method 6

You can make a more sophisticated script, which allows you to run under a specific user’s permissions, as follows:

#!/bin/sh
NAME=myservice
DESC="My Service"
USERGROUP="myservice:myservice"

#Helper functions
start() {
        start-stop-daemon --start --quiet --background --make-pidfile 
                --pidfile /var/run/$NAME.pid --chuid $USERGROUP 
                --exec /usr/local/bin/myservice
}

stop() {
        start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid 
                --exec myservice --retry 30
}

case "$1" in
  start)
        echo -n "Starting $DESC: "
        start
        echo "$NAME."
        ;;
  stop)
        echo -n "Stopping $DESC: "
        stop
        echo "$NAME."
        ;;
  restart)
        echo -n "Restarting $DESC: "
        #set +e
        stop
        #set -e
        #sleep 1
        start

        echo "$NAME."
        ;;
  *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart}" >&2
        exit 1
        ;;
esac

exit 0

The script goes in /etc/init.d/myservice, and you start the service by executing:

/etc/init.d/myservice start

Read the man page on start-stop-daemon to understand how it works.

Method 7

In Ubuntu or Debian like you can use, to add

update-rc.d your_service defaults

to remove

update-rc.d -f your_service remove

Bye! o

is nice to implements the functions status and force-reload to be LSB-compilant


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x