Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
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. –  xenoterracide Sep 12 '10 at 14:24

7 Answers 7

up vote 21 down vote accepted

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!

share|improve this answer
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 '10 at 20:32

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/you/tomcat --and --parameters

And you Tomcat should start on system start.

Systemd

  • create service definiton in /etc/systemd/system/custom-tomcat.service

  • put inside:

[Service]

ExecStart=/path/to/you/tomcat --and --parameters

Restart=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.

share|improve this answer

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

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.

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

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

share|improve this answer
    
how to make my daemon appear in service --status-all list? –  neckTwi Jun 26 '13 at 5:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.