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?

share|improve this question
    
possible duplicated: askubuntu.com/q/814/62483 – Lucio Dec 12 '12 at 15:37
up vote 108 down vote accepted

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

sudo -i

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

Alternative #1: 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 #2: Add an Upstart job (for systems older than 15.04)

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

Alternative #3: Add an init script (obsolete)

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
share|improve this answer
2  
Shouldn't people be using upstart now? – eduardocereto Dec 12 '12 at 15:14
    
It is possible to do the same thing by writing a short Upstart job. Initscripts are still supported, however, and are easy to use. – jdthood Dec 12 '12 at 15:50
2  
Just added the Upstart-job method as a third alternative. – jdthood Dec 12 '12 at 15:58
    
inittab is another method (but maybe more for daemons than scripts). – Geremia Sep 9 '16 at 14:30
    
Ubuntu 16.04 now uses systemd. As per askubuntu.com/questions/765120/… one can use sudo systemctl enable rc-local.service to get /etc/rc.local compatibility or see unix.stackexchange.com/questions/47695/… for systemd native script (but /etc/rc.local has the benefit of a lot of simplicity). Maybe add "Alternative #4. Add an systemd job" here? – djb Sep 12 '16 at 21:07

You don't need root, or to even login.

You can edit your crontab (crontab -e) and create an entry like this:

@reboot /path/to/script.sh

This way, you can run it as a regular user. @reboot just means it's run when the computer starts up (not necessarily just when it's rebooted).

share|improve this answer
    
Inorder for crone jobs to work, doesn't the particular user need to log in? The question states even if the user doesn't log in. In that case, would this method still work? – Qwertylicious Jan 10 '17 at 13:08
1  
@Qwertylicious, no the user does not need to log in for cron jobs to run. This method works whether or not he's logged in, because cron runs a system process, and when running the user's cron jobs, it runs the job as that user. As long as the computer is turned on, cron will run, regardless of who is logged in. – Dan Jones Jan 10 '17 at 16:38

from terminal

  1. create file newshell.sh.desktop in ~/.config/autostart folder:

    gedit ~/.config/autostart/newshell.sh.desktop
    
  2. change Exec, Name and Comment value and add to file: first line

     [Desktop Entry]
     Type=Application
     Exec=/full/link/to/your/newshell.sh
     Name=newshell
     Comment=whatever you want
    
  3. save

or

you can do it from GUI:

  1. run "startup applications" tool in Ubuntu 14.04 you just write it in search box.
  2. add same Exec, Name and Comment.
share|improve this answer
1  
I know this is kinda late, but thank you for the help. – user525989 Oct 7 '16 at 0:19
3  
Please note that the OP explicitly wanted it to start at system startup before anyone logs in. Autostarted scripts are run only at login. – Gunnar Hjalmarsson Feb 7 '17 at 3:08

In your home, you have a file named .bashrc. This file is executed at the opening of your session.

Just put something like this at the end of the file:

sh /path/to/your/script.sh

EDIT: sorry, i didn't answer your question because my solution is executed when a user is logged in...

To execute something before the login, you can try rcconf or rc-file: http://www.debianadmin.com/manage-linux-init-or-startup-scripts.html

share|improve this answer
3  
.bashrc is called when a new terminal is opened. – termnml Dec 12 '12 at 14:50
    
Thanks for the info. The website you mentioned has some good info! – Rusty Dec 12 '12 at 15:21

Simply edit rc.local nano /etc/init.d/rc.local as follows:

/path/to/my/script.sh || exit 1 
exit 0
share|improve this answer

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.