How do I run a script at start up?
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
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).
P.S.: Regarding comments that this does not work properly
Some have said that this doesn't work on Debian-based distros, such as Ubuntu. I have personally successfully used this method on both Ubuntu and Mint. There are a few things to consider, however.
The @reboot
jobs will run when the cron daemon starts. I've found that on Debian-based distros, this may occur before the /home
partition has been mounted. If the script you're running is in your home folder, it will fail.
Additionally, this isn't limited to Debian-based distros, but if your home folder is encrypted, it may not be decrypted until after you login. There is probably no way around this.
Also, your network interface may not be up yet, and if the command requires Internet access, it may fail.
Finally, again, this is not limited to Debian-based distros, but cron runs under a much more limited environment than your shell runs under. In particular, the PATH
variable has much fewer paths. It is possible that the command being run isn't found, if it's in, for example, something like $HOME/.local/bin
, which may be in your PATH
in your shell session, but not under cron
. It's even possible that the command being run depends on some environment variable that's not set in cron
.
So, there are a number of reasons why your command will to run under cron, but it's not because @reboot
doesn't work on your distro.
from terminal
create file
newshell.sh.desktop
in~/.config/autostart
folder:gedit ~/.config/autostart/newshell.sh.desktop
change
Exec
,Name
andComment
value and add to file: first line[Desktop Entry] Type=Application Exec=/full/link/to/your/newshell.sh Name=newshell Comment=whatever you want
save
or
you can do it from GUI:
- run "startup applications" tool in Ubuntu 14.04 you just write it in search box.
- add same
Exec
,Name
andComment
.