23

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

1
  • 6
    How services start up is very conditional on the Linux distro. Please tell us what you are trying to accomplish (and on what distro and maybe Desktop Environment) , because right now I think you're probably not thinking about the problem right. Sep 12, 2010 at 14:24

7 Answers 7

23

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!

1
  • 2
    Note that while start, stop, and restart are usually sufficient for simple init scripts. You probably also want to include a 'status' action, which is easy in Debian and other distros that include status_of_proc() in its init-functions package. Well managed packages will include all of the functions required by the LSB (refspecs.freestandards.org/LSB_3.1.0/LSB-Core-generic/…). The init script distributed with tomcat in Ubuntu contains all of these actions, it is probably bet to just use that.
    – Steven D
    Sep 12, 2010 at 20:32
19

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.

7

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.

6

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
4
  • Does this apply to all startups, or just reboots?
    – WhyNotHugo
    Sep 12, 2010 at 18:34
  • It applies ONLY to fcron - not vixie-cron or any other cron AFAIK. Sep 12, 2010 at 20:04
  • @Maciej It definitely works for vixie-cron; that's what I use Sep 12, 2010 at 23:10
  • @Hugo All startups Sep 12, 2010 at 23:10
6

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.

3

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.

1

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

1
  • how to make my daemon appear in service --status-all list?
    – Necktwi
    Jun 26, 2013 at 5:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .