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 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

Update 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 doesnt start on machine's restart

share|improve this question
    
possible duplicate of Bash Script on Startup? (Linux) –  Gilles Mar 29 '12 at 22:31
1  
What is your linux distribution? There are some differences for handling init-scripts there. –  Nils Apr 19 '12 at 19:16
4  
Do not make the script mode 0777. This is a huge security hole. Anyone who can log into the machine can add whatever they want to the script which is run as root. Make it mode 0755. –  camh Apr 20 '12 at 21:38
add comment

6 Answers 6

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.

share|improve this answer
3  
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
    
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 4: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
show 1 more comment

Try to check wether 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?

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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
share|improve this answer
add comment

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
share|improve this answer
add comment

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.