6

I am quite new to Linux and want to know what to be done to launch a .sh file on machine startup. Basically, I have a server machine with Linux OS on it and want to run some jboss application when system restart/crashes

I added S99jBossServers in rc2.d with link to /etc/rc.d/init.d/jBossServers.sh and have given full permission(777) to jBossServers.sh

the content in jBossServers.sh is as follows

#!/bin/sh
#
# chkconfig: 2345 80 30 
# description: 
#

RETVAL=0

start () {
    /root/batch/startSikkimCA.sh
    /root/batch/startSikkimSp.sh
}

stop () {
    /root/batch/stopSikkimCA.sh
    /root/batch/stopSikkimSp.sh
    RETVAL=$?
}
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        start
        ;;
    *)
        start
;;
        RETVAL=$?
esac
exit $RETVAL

I am not sure whether its correct or not but the 2 paths been refered in

start() and stop()

are correct, but still the application doesn't start on machine's restart.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Varun
  • 111
  • 1
  • 1
  • 5

6 Answers6

12

Look at init which is the traditional process starter, and runs scripts under /etc/init.d, /etc/rc1.d, etc. and /etc/rc.local, which is probably the location you want.

Some distributions use upstart instead - if so, look at the docs for that.

In either case the exact paths may vary by distro, but this should be enough to get you started.

Useless
  • 4,800
  • 4
    Actually to add your own script with init system it's much better to use /etc/rc.local. – rush Mar 29 '12 at 12:17
  • @rush - only when you don't need it to respond to init state changes. Should be fine here, but not always. – Kevin Mar 29 '12 at 12:48
  • 1
    Cheers @Rush, @Kevin - agree rc.local seems like the best choice for the OP, updated answer accordingly. – Useless Mar 29 '12 at 14:53
  • unable to find rc.local :S – Varun Apr 18 '12 at 04:58
  • I did say this varies by distribution and you still haven't told us what you're using; can you either give that information, or look for references to init or upstart in your disto documentation? – Useless Apr 18 '12 at 11:18
  • rc.local is NOT the best place to put this, it's an application service not a configuration one liner. What happens when someone wants to restart the service? This needs to be done correctly. If the the OP provides his distro details, I'll throw up a proper example in an answer. – J. M. Becker Apr 20 '12 at 04:51
1

Try to check whether your scripts are being started (e.g. but putting touch /var/tmp/startup.script into them). Are all commands in your scripts in the search-patch during boot?

Runlevel 2 looks unusual to me. 3 should be normal for a Linux without GUI. What distributution and version are you using?

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
Nils
  • 18,492
0

In /etc/inittab look for a line like id:3:initdefault:. The number will probably be 3 or 5, but might be 2 or 4. The number in that line is the default runlevel. Most likely it is not 2, so your scripts in rc2.d will not run.

Put your script in the rcN.d directory that matches the number on the initdefault line.

In principle, rc.local is a better choice, but rc.local varies so much from distro to distro (sometimes it's a directory, sometimes it's a script, sometimes it's not set up at all) that without knowing your distro it's easier to just have you put it in the correct runlevel. You might try man rc.local and see what that says on your system. If your system supports rc.local as a directroy, then put your script there and don't worry about the runlevels.

Also, make sure your script is owned by root and has permissions either 755 (if you want anyone to be able to run it) or 744.

Old Pro
  • 1,306
0

You can use a cron job for this. The format is

@reboot myjob.sh   

If necessary you can put the cron job in the root crontab, but if possible put it in a user cronjob.

emory
  • 472
0

You can enable running script in Linux startup by creating symbolic link in /etc/rc<runlevel>.d; <runlevel> is numeric and obtained with this command (runlevel) It means that the run level of your Linux system.

For example:

# runlevel

output:

N 2

To set up customScript with priorty 99:

ln -s /etc/init.d/customScript /etc/rc2.d/S99customScript
  • S ==> start
  • 99 ==> priority of startup
Mat
  • 52,586
developer
  • 165
0

Adding a startup script

This method works with Red Hat, Fedora, Ubuntu (install package chkconfig and service), Suse.

You should copy your script in /etc/init.d (it's either the proper directory or a link to the proper directory so it will work the same)

sudo cp jBossServers.sh /etc/init.d/
sudo chmod 0755 /etc/init.d/jBossServers.sh

Then you need to add it to the system start-up process. Instead of creating all the required links, use chkconfig (install the package first on Ubuntu)

sudo chkconfig add jBossServers.sh

Now reading your script heading this script has already the information for chkconfig rgarding the runlevels and priority. So you are fine. Next time you reboot JBoos will be started.

But you could try to start right now

sudo service jBossServers.sh start

Note: if your system complains that it does not find chkconfig or service, try to call then with /sbin/ before. Example

sudo /sbin/chkconfig add jBossServers.sh
sudo /sbin/service jBossServers.sh start
Huygens
  • 9,345