I have a Java program that I want to start up with every boot, but I'm unsure how to do it.
There are two bootup scripts that start manually (script1.sh and script2.sh), and when the server gets shutdown, we have another script that we run (shutdownscript.sh) so that the DB does not get corrupted.
On 11/21/10 6:19 PM, Kill Script wrote:
I have a Java program that I want to start up with every boot, but I'm unsure how to do it.
There are two bootup scripts that start manually (script1.sh and script2.sh), and when the server gets shutdown, we have another script that we run (shutdownscript.sh) so that the DB does not get corrupted.
The RedHat/Centos way of doing things is to have init scripts in /etc/rc.d/init.d that take at least start, stop, and restart as arguments for each program that should start automatically. Then for the runlevels where you want them to start you have a symlink where the name starts with S and the rest is a number to make it sort alphabetically into the order that things should start in /etc/rc?.d (where the ? is the runlevel). Likewise add links starting with 'K' in the levels where the process should be stopped. There is a convention for comments in the scripts so that 'checkconfig program on' can make the links for you. Look through some of the other scripts to see how they work.
Les Mikesell wrote:
The RedHat/Centos way of doing things is to have init scripts in /etc/rc.d/init.d that take at least start, stop, and restart as arguments for each program that should start automatically. Then for the runlevels where you want them to start you have a symlink where the name starts with S and the rest is a number to make it sort alphabetically into the order that things should start in /etc/rc?.d (where the ? is the runlevel). Likewise add links starting with 'K' in the levels where the process should be stopped. There is a convention for comments in the scripts so that 'checkconfig program on' can make the links for you. Look through some of the other scripts to see how they work.
Sorry for the stupid question here, but does the /etc/initd./scriptname file "know" about these symlinks because of a particular comment in there?
Where and how is the best way to make these symlinks once you have the "correct" file configuration.
On 24 November 2010 14:20, killscript killscript@gmail.com wrote:
Les Mikesell wrote:
The RedHat/Centos way of doing things is to have init scripts in /etc/rc.d/init.d that take at least start, stop, and restart as arguments for each program that should start automatically. Then for the runlevels where you want them to start you have a symlink where the name starts with S and the rest is a number to make it sort alphabetically into the order that things should start in /etc/rc?.d (where the ? is the runlevel). Likewise add links starting with 'K' in the levels where the process should be stopped. There is a convention for comments in the scripts so that 'checkconfig program on' can make the links for you. Look through some of the other scripts to see how they work.
Sorry for the stupid question here, but does the /etc/initd./scriptname file "know" about these symlinks because of a particular comment in there?
Copied from the man file for chkconfig:
RUNLEVEL FILES Each service which should be manageable by chkconfig needs two or more commented lines added to its init.d script. The first line tells chkconfig what runlevels the service should be started in by default, as well as the start and stop priority levels. If the service should not, by default, be started in any runlevels, a - should be used in place of the runlevels list. The sec- ond line contains a description for the service, and may be extended across multiple lines with backslash continuation.
For example, random.init has these three lines: # chkconfig: 2345 20 80 # description: Saves and restores system entropy pool for \ # higher quality random number generation. This says that the random script should be started in levels 2, 3, 4, and 5, that its start priority should be 20, and that its stop priority should be 80. You should be able to figure out what the description says; the \ causes the line to be continued. The extra space in front of the line is ignored.
Basically, create your file by taking one of the files as a sample and place it in /etc/init.d. For example, I've copied /etc/init.d/vncserver to /etc/init.d/sample and ran [root@hakan init.d]# chkconfig --add sample [root@hakan init.d]# chkconfig --list|grep sample sample 0:off 1:off 2:off 3:off 4:off 5:off 6:off
Now my sample is there but won't run at all but it's all there. [root@hakan init.d]# chkconfig sample on [root@hakan init.d]# chkconfig --list|grep sample sample 0:off 1:off 2:on 3:on 4:on 5:on 6:off
Now it's on for all of the levels in the comment included in the file. Nevertheless, I could have overriden that with the ckconfig --level <levels> <name> on command options to run on other levels.
[root@hakan etc]# find rc* -iname *sample*|sort rc.d/init.d/sample rc.d/rc0.d/K35sample rc.d/rc1.d/K35sample rc.d/rc2.d/S91sample rc.d/rc3.d/S91sample rc.d/rc4.d/S91sample rc.d/rc5.d/S91sample rc.d/rc6.d/K35sample
and the file comment looks like below which matches the above, startup priority is 91, kill priority is 35. It will run on all normal levels since it's not defined, excluding 1 (single user), 0 (shutdown) and 6 (reboot).
# chkconfig: - 91 35
I better remove this sample from my startup :)
On 11/24/10 8:20 AM, killscript wrote:
Les Mikesell wrote:
The RedHat/Centos way of doing things is to have init scripts in /etc/rc.d/init.d that take at least start, stop, and restart as arguments for each program that should start automatically. Then for the runlevels where you want them to start you have a symlink where the name starts with S and the rest is a number to make it sort alphabetically into the order that things should start in /etc/rc?.d (where the ? is the runlevel). Likewise add links starting with 'K' in the levels where the process should be stopped. There is a convention for comments in the scripts so that 'checkconfig program on' can make the links for you. Look through some of the other scripts to see how they work.
Sorry for the stupid question here, but does the /etc/initd./scriptname file "know" about these symlinks because of a particular comment in there?
The script itself doesn't need or do anything with the comments. And I don't know if the format is documented anywhere but you can probably figure it out from the examples or just copy something that starts/stops where you want.
Where and how is the best way to make these symlinks once you have the "correct" file configuration.
You can do it manually if you want, but chkconfig is easier. Here's an article with some background. http://www.linuxjournal.com/article/4445.
From: killscript killscript@gmail.com
Sorry for the stupid question here, but does the /etc/initd./scriptname file "know" about these symlinks because of a particular comment in there?
Where and how is the best way to make these symlinks once you have the "correct" file configuration.
Les already answered this
There is a convention for comments in the scripts so that 'checkconfig program on' can make the links for you. Look through some of the other scripts to see how they work.
You need to add a line in your script, something like this...(starts with #). # chkconfig: 2345 90 05
Add the program to chkconfig, and simply turn it "on" for runlevel you want. Chkconfig will manage the symlinks for you.
Thanks Sheraz _______________________________________________ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
2010/11/21 Jorge Fábregas jorge.fabregas@gmail.com:
On Sunday 21 November 2010 20:19:59 Kill Script wrote:
I have a Java program that I want to start up with every boot, but I'm unsure how to do it.
Put the call to your script on this file:
/etc/rc.d/rc.local
HTH, Jorge
It may be tempting to use the rc.local, but that's the quick and dirty way and not good for the long-term sustainability and management of a system. There's no way to individually control any service running from there, and no way to stop it on shutdown.
On Mon, Nov 22, 2010 at 9:36 AM, Brian Mathis brian.mathis@gmail.comwrote:
It may be tempting to use the rc.local, but that's the quick and dirty way and not good for the long-term sustainability and management of a system. There's no way to individually control any service running from there, and no way to stop it on shutdown.
Yeah, thank you. I talked with the person who wrote the Java program, and he essentially said the same thing.
I'm looking through his suggestions now, and he suggested a shutdown script. I others who have examples with pid.txt and "dirname $0". Not sure what these are and am googling them now.
On 11/22/2010 1:37 PM, Kill Script wrote:
It may be tempting to use the rc.local, but that's the quick and dirty way and not good for the long-term sustainability and management of a system. There's no way to individually control any service running from there, and no way to stop it on shutdown.
Yeah, thank you. I talked with the person who wrote the Java program, and he essentially said the same thing.
I'm looking through his suggestions now, and he suggested a shutdown script. I others who have examples with pid.txt and "dirname $0". Not sure what these are and am googling them now.
Standard init scripts use some common functions to write the process ID of the running program in a standard place so the 'stop' operation can find it. A side effect of this is that if you have multiple instances of a program running you have to have separate init scripts with different names to control them. Also, you generally need root access to start/stop with this facility. If your program doesn't otherwise need root access and you want an ordinary user to have control you may want to do it some other way.
On Monday 22 November 2010 10:36:31 Brian Mathis wrote:
It may be tempting to use the rc.local, but that's the quick and dirty way and not good for the long-term sustainability and management of a system. There's no way to individually control any service running from there, and no way to stop it on shutdown.
I totally agree. My suggestion was based on the assumption that the OP didn't have much system-administration experience and using rc.local was definitely the easiest way out.
I should have warned him of the alternate correct method though...Fortunately he has been nicely informed by others.
2010/11/22 Jorge Fábregas jorge.fabregas@gmail.com
I totally agree. My suggestion was based on the assumption that the OP didn't have much system-administration experience and using rc.local was definitely the easiest way out.
Exactly...
The OP was recommended that by someone else but thought better than to put that in production!
On 11/21/10 4:19 PM, Kill Script wrote:
I have a Java program that I want to start up with every boot, but I'm unsure how to do it.
There are two bootup scripts that start manually (script1.sh and script2.sh), and when the server gets shutdown, we have another script that we run (shutdownscript.sh) so that the DB does not get corrupted.
centos uses sysVinit.
you would create a script in /etc/rc.d/init.d/ called yourjavathing, which takes the parameter {start|stop} and optionally {reload|restart|status} ... this script would include the header lines for chkconfig. its run by init as the root user (but without any user login profiles). If you want your javathing to run as a different user, you should invoke your script1.sh and script2.sh via su someuser -c "/path/to/script1.sh" ...
once this script exists, and you test it by invoking it as "/etc/rc.d/init.d/yourjavathing start" (and "... stop", etc), then
chkconfig --on yourjavathing
will configure the OS to run it automatically at the runlevels that your chkconfig header lines specify. and,
service yourjavathing start
(or stop, etc) will manually start and stop the service.
On Sun, Nov 21, 2010 at 8:18 PM, John R Pierce pierce@hogranch.com wrote:
you would create a script in /etc/rc.d/init.d/ called yourjavathing, which takes the parameter {start|stop} and optionally {reload|restart|status} ... this script would include the header lines for chkconfig. its run by init as the root user (but without any user login profiles). If you want your javathing to run as a different user, you should invoke your script1.sh and script2.sh via su someuser -c "/path/to/script1.sh" ...
Thank you! This was very helpful.
once this script exists, and you test it by invoking it as "/etc/rc.d/init.d/yourjavathing start" (and "... stop", etc), then
chkconfig --on yourjavathing
will configure the OS to run it automatically at the runlevels that your chkconfig header lines specify. and,
service yourjavathing start
(or stop, etc) will manually start and stop the service.
Googling, I found this example:
http://www.linuxjournal.com/article/4445
and I think that I can easily modify this Oracle init.d example
http://www.linuxjournal.com/files/linuxjournal.com/linuxjournal/articles/044...
On 11/21/10 6:20 PM, Kill Script wrote:
and I think that I can easily modify this Oracle init.d example
http://www.linuxjournal.com/files/linuxjournal.com/linuxjournal/articles/044...
note that example is suffering from some HTML formatting issues. Specifically, the lines like...
su-$ORA_OWNER -c $ORA_HOME/bin/dbstart
should be
su - $ORA_OWNER -c $ORA_HOME/bin/dbstart
(noting the spaces on either side of the first dash ...)
On Mon, Nov 22, 2010 at 8:03 PM, John R Pierce pierce@hogranch.com wrote:
su-$ORA_OWNER -c $ORA_HOME/bin/dbstart
should be
su - $ORA_OWNER -c $ORA_HOME/bin/dbstart
So, for a Java program, would you suggest creating a different user, giving that user just enough privileges, and then running the script so that it exited if the wrong user tried to use it?
(Just trying to think what's the best long term solution for something like this)
On 11/22/10 10:34 PM, Kill Script wrote:
On Mon, Nov 22, 2010 at 8:03 PM, John R Pierce <pierce@hogranch.com mailto:pierce@hogranch.com> wrote:
su-$ORA_OWNER -c $ORA_HOME/bin/dbstart should be su - $ORA_OWNER -c $ORA_HOME/bin/dbstart
So, for a Java program, would you suggest creating a different user, giving that user just enough privileges, and then running the script so that it exited if the wrong user tried to use it?
(Just trying to think what's the best long term solution for something like this)
Yes, especially if it runs a network protocol with the possibility of remote exploits. It is an easy way to limit the damage if anything goes wrong. But, the init scripts start as root so they need to su to the right user before starting the app.
On Sun, Nov 21, 2010 at 6:19 PM, Kill Script killscript@gmail.com wrote:
I have a Java program that I want to start up with every boot, but I'm unsure how to do it. There are two bootup scripts that start manually (script1.sh and script2.sh), and when the server gets shutdown, we have another script that we run (shutdownscript.sh) so that the DB does not get corrupted.
Have you looked at Java Service Wrapper:
http://wrapper.tanukisoftware.com/doc/english/introduction.html
Ultimately, you will be creating an init.d script as discussed in this thread, but this provides some java-specific features.
-- Jeff