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

# chkconfig: 2345 80 30 
# description: 


start () {

stop () {
case "$1" in
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.

  • possible duplicate of Bash Script on Startup? (Linux) Mar 29, 2012 at 22:31
  • 1
    What is your linux distribution? There are some differences for handling init-scripts there.
    – Nils
    Apr 19, 2012 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, 2012 at 21:38

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.

  • 4
    Actually to add your own script with init system it's much better to use /etc/rc.local.
    – rush
    Mar 29, 2012 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, 2012 at 12:48
  • 1
    Cheers @Rush, @Kevin - agree rc.local seems like the best choice for the OP, updated answer accordingly.
    – Useless
    Mar 29, 2012 at 14:53
  • unable to find rc.local :S
    – Varun
    Apr 18, 2012 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, 2012 at 11:18

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?


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.


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.


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

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


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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