How do I run a script at start up?
Solution 1
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) (not recommended)
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
Official statement from upstart website -> "Project is in maintaince mode only. No new features are being developed and the general advice would be to move over to another minimal init system or systemd."
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
Solution 2
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.
Solution 3
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
.
Solution 4
Simply edit rc.local nano /etc/init.d/rc.local
as follows:
/path/to/my/script.sh || exit 1
exit 0
Solution 5
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
Related videos on Youtube
Rusty
Updated on September 18, 2022Comments
-
Rusty over 1 year
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?
-
eduardocereto over 11 yearsShouldn't people be using upstart now?
-
Rusty over 11 yearsThanks for the info. The website you mentioned has some good info!
-
jdthood over 11 yearsIt is possible to do the same thing by writing a short Upstart job. Initscripts are still supported, however, and are easy to use.
-
Geremia over 7 years
inittab
is another method (but maybe more for daemons than scripts). -
djb over 7 yearsUbuntu 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? -
Denis over 7 yearsInorder 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?
-
Dan Jones over 7 years@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.
-
Rafael Moni over 7 yearson alternative 2, don't forget to add the command sh before the script: "sh /path/to/my/script.sh" I was lazy enough to not even read the command! lol
-
Gunnar Hjalmarsson over 7 yearsPlease note that the OP explicitly wanted it to start at system startup before anyone logs in. Autostarted scripts are run only at login.
-
Menasheh almost 7 yearsIf #3 is obsolete, what should we do now?
-
12431234123412341234123 over 6 years
/etc/rc.local
will not work, it will run when you connect to the network, not at startup. -
NoOne over 4 yearsSo, will these methods run the commands as root?
-
shrimpwagon over 4 yearsThis does NOT work in Debian and variants; Ubuntu, Mint.
-
Phill Healey over 4 yearsI tried this method on a Debian based system (Rasbian) and it completely locked me out of SSH access. I had to mount the drive on another system so that I could remove this from cron. Fortunately this got me access to the machine via ssh. Not sure why it affected ssh when other services were still running fine.
-
Dan Jones over 4 years@PhillHealey I have no idea why that would happen on any system. I've used this method on dozens of different systems, running several different distributions and operating systems. It's worked fine on every system I've used it on. In fact, unless you're doing this as root, or something in your script is using
sudo
, there is no way it could affect system processes. -
Dan Jones almost 4 years@shrimpwagon This does work on Ubuntu and Mint. I've used it myself under both. I've added some more information to my reply to explain why it may seem not to work.
-
shrimpwagon almost 4 years@DanJones Good update. That makes sense and is probably what is going on. Thank you.
-
FutureJJ about 2 yearsI had the same issue others are having,
cron
task did not run on boot in Ubuntu. To fix this, I had to runcrontab -e
insidesudo -i