How do I run a script at start up?

Description

I have a script in a folder:

/path/to/my/script.sh

I need this script to run every time the system starts (even if no one logs in to the system). What do I need to do in order to make this happen?

Answers

You will need root privileges for any the following. To get root, open a terminal and run the command

sudo su

and the command prompt will change to '#' indicating that the terminal session has root privileges.

Alternative #1. Add an initscript.

Create a new script in /etc/init.d/myscript.

vi /etc/init.d/myscript

(Obviously it doesn't have to be called "myscript".) In this script, do whatever you want to do. Perhaps just run the script you mentioned.

#!/bin/sh 
/path/to/my/script.sh

Make it executable.

chmod ugo+x /etc/init.d/myscript

Configure the init system to run this script at startup.

update-rc.d myscript defaults

update-rc.d updates the System V style init script links /etc/rcrunlevel.d/NNname whose target is the script /etc/init.d/name. These links are run by init when it changes runlevels; they are generally used to start and stop system services such as daemons.

If defaults is used then update-rc.d will make links to start the service in runlevels 2345 and to stop the service in runlevels 016.

Alternative #2. Add commands to /etc/rc.local

vi /etc/rc.local

with content like the following.

# This script is executed at the end of each multiuser runlevel 
/path/to/my/script.sh || exit 1 
# Added by me 
exit 0

Alternative #3. Add an Upstart job.

Create /etc/init/myjob.conf

vi /etc/init/myjob.conf

with content like the following

description "my job" 
start on startup 
task 
exec /path/to/my/script.sh

results for ""

    No results matching ""