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
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityI 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
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!
Depending on init system, you create init script differently. Fedora gives you upstart and systemd to choose from, and of course SysV compatibility.
/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.
/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.
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.
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
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.
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.
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