How can I run scripts automatically when Ubuntu starts up so I don't have to run them manually after startup?
-
7If someone could also show both WHEN and WHERE that would be awesome. I say this because I know there are at least 2 ways to start a script that will fire before other applications have been started (like X11) – David Stocking Aug 04 '10 at 20:20
-
4This entire answer thread is a mess. The Stack Exchange format doesn't seem to be best suited for this question – Gabriel Fair Feb 25 '18 at 19:09
-
5+1 to @GabrielFair. A LARGE part of the problem is that the original question and answer are TEN YEARS OLD. I'd also add that there are too many ways to solve this problem. What happened to the simplicity of the Unix philosophy?! Request someone knowledgeable and with sufficient points rewrite this post, or add a new, up-to-date, definitive answer for modern os versions. – Malik A. Rumi Jun 14 '20 at 09:17
10 Answers
One approach is to add an @reboot cron task:
- Running
crontab -e
will allow you to edit your cron. Adding a line like this to it:
@reboot /path/to/script
will execute that script once your computer boots up.
-
115
-
28
-
7So... this wouldn't run if I lost power and the PC turned again when power is restored? – Mike Wills Jun 21 '12 at 19:27
-
32@siamii:
man 5 crontab
says that@reboot
is executed on startup (when cron daemon is started). – jfs Mar 29 '13 at 16:04 -
10This is awesome. So far this seems better than
rc.local
since the system seems more setup by this point (PATH, etc). It is odd that it is so hard to call something after system startup.. – Karthik T Oct 10 '13 at 07:17 -
1I tried this for a non-root user and it did not seem to work. I had to move this into an /etc/cron.d script. – Fabrizio Regini Nov 07 '13 at 14:03
-
-
-
3@amphibient Chances are you configured something wrong.
@reboot
is documented here: https://help.ubuntu.com/community/CronHowto – ceejayoz May 08 '14 at 20:36 -
2i did
sudo crontab -e
and then added@reboot /my/script
to it. when i just run/my/script
, it works fine – amphibient May 08 '14 at 20:41 -
@amphibient It may be something in
/my/script
itself. Scripts run from cron may not have the same$PATH
for example. If you try@reboot touch /tmp/something
I'll bet that works fine. – ceejayoz May 08 '14 at 21:42 -
you are right, that worked. my script says
ifconfig > /some/shared_vm/dir
. this is on a VM. i am trying to fetch the IP address of the VM when the system starts and print it to a file in a directory shared with the host. i think the problem is that when the VM first starts, this directory is not mounted yet, that's why. so i need to run the script maybe 5 sec after the system starts. any idea how to do that? – amphibient May 08 '14 at 22:03 -
1@reboot works. I had a problem when I ran my script in terminal it worked, but not with cron. So I added >> /var/log/mylog.log 2>&1 in crontab to see the log. After restart I noticed it was missing JAVA_HOME (my script was calling some java jars). – FrEaKmAn Aug 07 '14 at 11:08
-
@ceejayoz: My script has:
#!/bin/bash gnome-terminal --tab -t "MyPC" -e "sh -c 'ls;exec bash'" --tab -t "Mypc2" "
and it's not working. Any idea? I checked as u saidtouch
that works – Siddharth Trikha Dec 15 '14 at 05:14 -
@amphibient Maybe prepending a sleep 5 would give you the desired effect. – Ashhar Hasan Dec 07 '15 at 03:51
-
added skype and autokey there, only thing that happens at XFCE4 Login is an error message. – phil294 Aug 07 '16 at 17:51
-
-
-
@ceejayoz this is what i entered
@reboot /path/to/script/myscript.sh
– Shift 'n Tab Nov 24 '16 at 03:42 -
@ShiftN'Tab It's still extraordinarily unlikely they removed the feature. It's far more likely you've messed something up in the script itself. Did you get any errors or output in the logs? – ceejayoz Nov 24 '16 at 13:52
-
@ceejayoz inside of my script is a command that disables the key of the keyboard which is Alt i wrote this code
xmodmap -e 'keycode 64='
when i try to run the script manually it works perfectly fine. But not with this method. – Shift 'n Tab Nov 24 '16 at 15:22 -
@ceejayoz you can take a look here in my post http://askubuntu.com/questions/852198/disabling-keys-in-ubuntu-permanently?noredirect=1#comment1310339_852198 – Shift 'n Tab Nov 24 '16 at 15:25
-
@ShiftN'Tab That should probably go in your
~/.profile
instead of a@reboot
, as I suspect it affects just your current session. Cron's session isn't the same as your own session. I see someone noted it's deprecated, too. – ceejayoz Nov 24 '16 at 15:26 -
@ceejayoz yes but those link doesnt have a sound solution to my problem. Hope you could help – Shift 'n Tab Nov 24 '16 at 15:36
-
-
1
-
according to https://askubuntu.com/questions/735935/running-command-at-startup-on-crontab crontab @reboot does not do anything after a system shut-down, or cold boot. – user1709076 Jun 15 '17 at 16:20
-
However for anyone who's interested, I found that on AWS EC2, when I 'stop' the EC2 instance and start the EC2 instance again, that doing crontab reboot /home/ubuntu/mountefs.sh method works for mounting EFS - so the reboot method appears to work for 'restart', and 'stop' and 'start', so I would expect this method to work for any disaster recovery scenario? – user1709076 Jun 15 '17 at 16:31
-
This has worked for me (Ubuntu 16.04). I have a question though. How can i see the errors (if any) generated after running the script? (I have run a Django app after everytime i have started the Desktop) – ni8mr Aug 19 '18 at 02:43
-
Tried to add a path to bash script, tried to write the command itself - nothing worked. Tried to redirect whole output into a file - it's created but empty. Ubuntu 18.04 :(( – Stalinko Oct 30 '19 at 10:51
-
3
-
3Sadly, this does not work for me in 20.04.2. I feel really bad to downvote this because this answer is not for everyone. – Alexander Beatson Oct 19 '21 at 11:39
-
1@AlexanderBeatson No need to feel bad. I've gotten well more rep from this answer than it deserves, and subsequent answers are likely better here in 2021. – ceejayoz Oct 31 '21 at 02:08
-
1Didn't work in 22.04.1, unfortunately. I ended up using the other solution below, with the Startup Applications. – marcelocra Oct 10 '22 at 01:11
-
This worked for me on 22.04.x, the AWS EC2 AMI which appears in QuickStart. – t_motooka Mar 14 '23 at 08:29
Depending on what sort of scripts you need to run.. For services and the like you should use upstart. But for a user script these should be launched as session scripts by gnome! Have a look under System > Preferences > Startup Applications.
On a side note if you need some scripts to be run on terminal login you can add them to the .bash_login file in your home directory.
For 14.04 and older
A simple command (one which doesn't need to remain running) could use an Upstart job like:
start on startup
task
exec /path/to/command
Save this in a .conf
file in /etc/init
(if you need it to run as root when the system boots up), or in ~/.config/upstart
(if you need it
to run as your user when you log in).

- 14,617
-
63Considering how SO and StackExchange runs, could you give an example of an upstart script and where it would be placed? That would make this a much better answer. Your link says it's not being maintained and to look at the upstart cookbook, which is huuuge. I don't have too much of an idea where to start. – Ehtesh Choudhury Feb 04 '13 at 04:03
-
5
-
2@dopatraman The answer states that all processes with this are run as root. – AStopher Oct 24 '15 at 11:43
-
5Please update this answer to explain what to do on systems running systemd rather than upstart (Ubuntu 15.04+). – Jan 09 '16 at 18:52
-
can any please help in running below as upstart service
https://gist.github.com/jobsamuel/6d6095d52228461f3c53
– Rizwan Patel Mar 14 '16 at 14:19 -
In Ubuntu 14.04, if the command is a path to a script where an ifconfig is executed, it does not work. It must be placed, for instance, in .bashrc of the corresponding user. – aloplop85 Jul 07 '16 at 07:22
-
4This answer does not make sense to me. The applications listed in
system->pref->startup applications
can neither be found in/etc/init/
nor in~/.config/upstart
. So where are startup applications defined? – phil294 Aug 07 '16 at 17:49 -
1I want to run
wget -r http://tug.org/
on startup after network connection established. How can I do this? – alhelal May 09 '17 at 13:56 -
@EhteshChoudhury I found here a nice introduction to upstart: https://www.digitalocean.com/community/tutorials/the-upstart-event-system-what-it-is-and-how-to-use-it – Juan Calero May 03 '18 at 09:48
-
6
You can add commands to /etc/rc.local
:
sudo nano /etc/rc.local
This executes the commands as root.
To execute commands as a specific user, use sudo -i -u
(-i
to also run the login shell). For example, to establish a persistent SSH tunnel, where myhost
is definde in johndoe
s ~/.ssh/config
file:
sudo -i -u johndoe autossh -nNT -L 1234:localhost:1234 myhost
Note that if /etc/rc.local
did not exist (as is the case on Ubuntu since 16.04), you need to add a shebang line at the top (e.g. #!/bin/bash
), and ensure the file is executable:
sudo chmod a+x /etc/rc.local

- 197,895
- 55
- 485
- 740

- 2,803
-
27This most directly answers the question: how to simply execute some scripts when your system boots. upstart does a more complex task: starts daemon processes. – Dogweather Aug 07 '13 at 19:01
-
Do I need to put the path of my script or it also needs the sh before the path? – John John Pichler Apr 10 '15 at 21:31
-
1So upstart starts daemon processes while /etc/rc.local starts bash scripts? – Donato May 10 '15 at 18:51
-
7
-
7
-
6Note that if you make this file yourself (like I did), then you will have to change file to executable with
chmod 755 rc.local
, and add#!/bin/bash
to the first line. – psitae Aug 02 '17 at 16:42 -
Does the system wait for the script to finish before continuing the booting process? – Psddp Aug 13 '17 at 01:28
-
-
2
-
great! Thank you @paolo-granada-lim ! You solved my problem in old Ubuntu16.x: Using a LUKS with the usual /dev/mapper method led to noexec of this (home) partition. Did not care for "exec" in fstab (!), did not care for rwx of directories or so; SOLUTION: Just put "sudo mount -o remount,exec /home " in the /etc/rc.local (which already had an shebang). Now my encrypted mount works fine with exec permission thanks to the automatic remount. 10 years after .... THX ! – opinion_no9 Dec 13 '20 at 21:50
For 15.04 and later:
To run a (short-lived)1 command at startup using systemd
, you can use a systemd unit of type OneShot
. For example, create /etc/systemd/system/foo.service
containing:
[Unit]
Description=Job that runs your user script
[Service]
ExecStart=/some/command
Type=oneshot
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
Then run:
sudo systemctl daemon-reload
sudo systemctl enable foo.service
Essentially, this is just converting a typical Upstart job to a systemd one (see Systemd for Upstart users).
You can run multiple commands from the same service file, using multiple ExecStart
lines:
[Service]
ExecStart=/some/command
ExecStart=/another/command some args
ExecStart=-/a/third/command ignore failure
The command must always be given with the full path. If any command fails, the rest aren't run. A -
before the path tells systemd to ignore a non-zero exit status (instead of considering it a failure).
Relevant:
For user sessions, you can create the systemd unit in ~/.config/systemd/user
instead. This should work with 16.04 onwards, but not earlier releases of Ubuntu with systemd (since those still used Upstart for user sessions). User session units can be controlled with the same commands as with system services, but with the --user
option added:
systemctl --user daemon-reload
systemctl --user status foo.service
Shell syntax
Note that, unlike Upstart, systemd doesn't run the Exec*
commands through a shell. It performs some limited variable expansion and multiple command (separated by ;
) itself, but that's about it as far as shell-like syntax goes. For anything more complicated, say redirection or pipes, wrap your command in sh -c '...'
or bash -c '...'
.
1As opposed to long-lived daemons.
-
is it possible to set a priority on the job? or specify that it depends on another service to be started first? – r3wt Apr 25 '17 at 00:38
-
1@r3wt yes, there are different ways to do that. The
WantedBy
used here, for example, makes it start when themulti-user.target
is reached. You can useBefore
,After
,Requires
, etc. Seeman systemd.unit
– muru Jan 03 '18 at 06:07 -
-
You're welcome. — Btw, the
RemainAfterExit
depends on the service you start and its desired behaviour. For instance,/bin/df -h
wouldshould haveRemainAfterExit=no
. – PerlDuck Mar 05 '18 at 14:19 -
@PerlDuck There's nothing inherent in
df
that needsRemainAfterExit=no
. Unless you want to repeatedly execute the command each time you runsystemctl start foo
. – muru Mar 05 '18 at 14:32 -
1The difference comes when you issue
systemctl status foo
: with RemainAfterExit=yes it says active while with RemainAfterExit=no it doesn't. However. – PerlDuck Mar 05 '18 at 15:22 -
@PerlDuck that's a cosmetic change compared to the difference when starting the service multiple times. – muru Mar 05 '18 at 15:28
-
Does it mean that
sudo ls /etc/systemd/system/*.service
lists all the services that run on startup? – João Pimentel Ferreira Jun 12 '19 at 20:33 -
I followed your suggestion to read
man 5 systemd.service
and I got even more confused. Say I want to mount an sshfs filesystem at boot asuser
instead of root, so I can't do it withfstab
. However. I obviously need to make sure the network is up when the service starts. How would I do that? And is aOneshot
still the right way or should it be "long lived"? – Andyc Jul 29 '21 at 18:07 -
@Andyc in the case of sshfs, https://stackoverflow.com/a/19972859/2072269 or https://askubuntu.com/a/1117790/158442 in
/etc/fstab
to mount. (The relevant parts from my own/etc/fstab
include_netdev,users,IdentityFile=/home/muru/.ssh/id_rsa,idmap=user,allow_other,default_permissions,uid=1000,gid=1000
. Works fine enough.) If I did have to use systemd to mount something, I'd usesystemd.mount
instead. – muru Jul 30 '21 at 00:40 -
@muru I can't believe it. I spent all day yesterday trying to make the sshfs in fstab work, including the configurations from the answers you linked and a million aothers, to no avail. I couldn't ssh into the remote machine with
sudo
, which made me try thesystemd
route. Today I come here, I try tosudo ssh
into the remote machine, and it works fine. I have no clue how that happened. Maybe the computer itself needed a rest (although I didn't turn it off, I left it on all night). – Andyc Jul 30 '21 at 05:59 -
Note you can specify the user and group to run the script as with
User=xxxx
andGroup=xxxx
under theService
header. – Aaron Silverman Sep 04 '21 at 05:06 -
Its not possible
Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.
– Arturski Jul 05 '22 at 17:15 -
This worked for me. I just had to add one step - make the new service executable with
sudo chmod a+x foo.service
. – John Bonfardeci Apr 27 '23 at 02:59 -
@JohnBonfardeci service files definitely don't need to be executable (in fact, none of the service files shipped by default with Ubuntu are executable). – muru Apr 27 '23 at 03:54
There are different ways to automatically run commands:
The upstart system will execute all scripts from which it finds a configuration in directory
/etc/init
. These scripts will run during system startup (or in response to certain events, e.g., a shutdown request) and so are the place to run commands that do not interact with the user; all servers are started using this mechanism.You can find a readable introduction to at: http://upstart.ubuntu.com/getting-started.html the man pages
man 5 init
andman 8 init
give you the full details.A shell script named
.gnomerc
in your home directory is automatically sourced each time you log in to a GNOME session. You can put arbitrary commands in there; environment variables that you set in this script will be seen by any program that you run in your session.Note that the session does not start until the
.gnomerc
script is finished; therefore, if you want to autostart some long-running program, you need to append&
to the program invocation, in order to detach it from the running shell.The menu option System -> Preferences -> Startup Applications allows you to define what applications should be started when your graphical session starts (Ubuntu predefines quite some), and add or remove them to your taste. This has almost the same purpose and scope of the
.gnomerc
script, except you don't need to knowsh
syntax (but neither can you use anysh
programming construct).

- 16,507
-
12
- "This has almost the same purpose and scope of the .gnomerc script", except
– That Brazilian Guy Jan 23 '13 at 16:13.gnomerc
apparently runs *before* loading Unity, andStartup Applications
apparently runs *after* loading Unity. I had to run a program that sits on Unity's menu bar and it made a huge difference in this case! - "This has almost the same purpose and scope of the .gnomerc script", except
-
1@ruda.almeida Thanks for pointing that out. The answer was written in the pre-Unity days. – Riccardo Murri Jan 23 '13 at 16:48
-
1
sudo update-rc.d myscript.sh defaults
, where /etc/init.d/myscript.sh is your script, also runs it at startup. – Dan Dascalescu Aug 09 '16 at 06:16
$HOME/.config/autostart
contains the startup application list. .desktop
files in this folder will be executed on startup. It may need executable permission (chmod +x startup.desktop
).
Sample example for .desktop
file:
[Desktop Entry]
Type=Application
Exec="</path/to/script>"
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name=Startup Script
Here "</path/to/script>"
is replaced with path to your script.sh
If you place your script myscript
in /usr/local/bin
so that it can be executed directly by command, you can write myscript
instead of "</path/to/script>"
.
Sample example of myscript.sh
:
#!/bin/bash
<commands to be executed>
exit
Result:
.desktop
file will be launched from $HOME/.config/autostart
which execute script by Exec=
-
Doesn't work on Ubuntu 18.04.
$HOME/.config/autostart
contains but one file for dropbox, which BTW isn't executable. – MERose Jan 24 '20 at 07:26 -
1Worked for me on Ubuntu 18.04.1. Btw. I added the desktop entry via Search -> Startup applications. The outcome is exactly the same though. – Father Stack Mar 03 '20 at 11:10
-
yeah this will work for script that doesn't require
sudo
permission right? – Bo Chen Jan 08 '23 at 00:43 -
or you can hardcode your password in your script , like below Exec=gnome-terminal -- sh -c 'echo "YOURPASSWORD" | sudo -S sh PATH_TO_YOUR_SCRIPT && sleep 1 && printf "\n"' – Bo Chen Jan 08 '23 at 00:45
-
For me the chmod was necessary on the .sh script rather than the .desktop file – Alkanshel Feb 08 '23 at 02:25
-
@Bo Chen If you set the ".sh" as executable in properties or through chmod, you won't need sudo if it wasn't required for the script to run. – salmmus Sep 24 '23 at 15:27
For simple things you can add a command in System->Preferences->Sessions pointing to the location of your script.
Alternatively you can add it to /etc/init.d/rc.local or make an upstart job if it's a more low level stuff.
Take a look at https://help.ubuntu.com/community/UbuntuBootupHowto for more info

- 2,682
cron
answer implemented different from top voted
This answer still uses cron
but uses a different method than the top voted answer. This works since Ubuntu 16.04 but probably supported much sooner. It's just that I started using cron
to run jobs when computer boots up since 16.04.
When does cron
run?
In comments someone asked "when do they run?". You can tell in syslog / journalctl:
$ journalctl -b | grep cron
Jan 02 16:54:40 alien cron[919]: (CRON) INFO (pidfile fd = 3)
Jan 02 16:54:40 alien cron[919]: (CRON) INFO (Running @reboot jobs)
Jan 02 16:54:40 alien systemd[1]: Started Run anacron jobs.
Jan 02 16:54:40 alien anacron[949]: Anacron 2.3 started on 2018-01-02
Jan 02 16:54:40 alien anacron[949]: Normal exit (0 jobs run)
Jan 02 16:54:40 alien CRON[952]: pam_unix(cron:session): session opened for user root by (uid=0)
Jan 02 16:54:40 alien CRON[954]: pam_unix(cron:session): session opened for user root by (uid=0)
Jan 02 16:54:40 alien CRON[951]: pam_unix(cron:session): session opened for user root by (uid=0)
Jan 02 16:54:40 alien CRON[950]: pam_unix(cron:session): session opened for user root by (uid=0)
Jan 02 16:54:40 alien CRON[985]: (root) CMD ( /usr/local/bin/cron-reboot-cycle-grub-background)
Jan 02 16:54:40 alien CRON[954]: pam_unix(cron:session): session closed for user root
Jan 02 16:54:40 alien cron[919]: sendmail: Cannot open smtp.gmail.com:587
Jan 02 16:54:40 alien CRON[952]: pam_unix(cron:session): session closed for user root
Jan 02 16:54:40 alien cron[919]: sendmail: Cannot open smtp.gmail.com:587
Jan 02 16:54:40 alien CRON[950]: pam_unix(cron:session): session closed for user root
One thing to note is cron
can email you status of jobs run and @reboot
jobs run so early network manager and email won't be running unless you put a sleep
command into your script(s).
Where to put your scripts
Put your scripts in the directory /etc/cron.d
:
$ ll /etc/cron.d
total 44
drwxr-xr-x 2 root root 4096 Nov 26 19:53 ./
drwxr-xr-x 139 root root 12288 Dec 31 13:58 ../
-rw-r--r-- 1 root root 244 Dec 28 2014 anacron
-rw-r--r-- 1 root root 148 Feb 18 2017 cycle-grub-background
-rw-r--r-- 1 root root 138 Mar 5 2017 display-auto-brightness
-rw-r--r-- 1 root root 460 Nov 26 19:53 nvidia-hdmi-sound
-rw-r--r-- 1 root root 102 Feb 9 2013 .placeholder
-rw-r--r-- 1 root root 224 Nov 19 2016 touch-vmlinuz
-rw-r--r-- 1 root root 700 Aug 5 11:15 turn-off-hyper-threading
What does a script look like?
Here are a couple of scripts I have setup to run each boot:
$ cat /etc/cron.d/cycle-grub-background
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
@reboot root /usr/local/bin/cron-reboot-cycle-grub-background
$ cat /etc/cron.d/touch-vmlinuz
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
@reboot root touch "/boot/vmlinuz-"uname -r

- 2,426

- 102,282
-
1There are many different ways to add cronjobs, but the core of the highly voted answer and your answer is still the
@reboot
. – muru Jan 03 '18 at 05:40 -
Alternate methods for adding crontabs should be posted to https://askubuntu.com/q/2368/158442, which is explicitly about adding Cron jobs. – muru Jan 03 '18 at 05:47
-
1I beg to differ. The core of the answer in question utilizes
crontab -e
which some consider one of the black arts due to a vim-like interface. On the other hand this answer might appeal to those whose brains are wired a certain way. We are not all cast from the same mold. Then again this answer already has one down vote so we'll let democracy take her course. – WinEunuuchs2Unix Jan 03 '18 at 05:58 -
4
-
@muru Yes probably because you taught me and I learned to change editor to something like nano or a couple of other CLI's. But I'm in the gedit camp. Besides
crontab -e
brings up memories of asterisks ("*") for minutes, hours, etc. that I've always found I need to google instructions for. I still find using/etc/cron.d
and/etc/cron.daily
my go to choice. Especially since it mirrors/etc/udev/rules.d
and/etc/systemd/system-sleep
methods. It just seems like a nice fit. – WinEunuuchs2Unix Jan 03 '18 at 06:09 -
1This is much clearer than the highly voted answer. Every answer has hundreds of comments saying "doesn't work", because this task is more subtle than the original post suggests. – apg Mar 08 '20 at 08:03
You should use upstart for this. Upstart is used for Ubuntu processes that are automatically started. It is an enhanced solution like the old System-V init.d scripts. It also allows you to put in prerequisites to the start of your script (i.e. do you need the network running? etc.)

- 28,462
If you want your script to run before systemd right after kernel starts, AFAIK the way is adding init=/path/to/script
to the kernel command line in /boot/grub/grub.cfg
or more future proof make your own menu entry in /etc/grub.d/40_custom
by copying a menu entry from /boot/grub/grub.cfg
and making needed changes (and running update-grub
after that for grub
to add your custom file to /boot/grub/grub.cfg
).
linux /boot/vmlinuz-5.4.0-26-generic ... ro quiet splash
change to
linux /boot/vmlinuz-5.4.0-26-generic ... ro quiet splash init=/path/to/script
Take care to properly put e.g. #!/bin/bash
on the first line and exec /sbin/init
(if /sbin/init
exists on your system - on mine it points to systemd) at the end to avoid kernel panic.

- 191
-
@FindOutIslamNow, well, I've mentioned adding custom file. Do you claim system update cancel custom files? Where such info is from? – Martian2020 Mar 01 '23 at 14:22