Ubuntu and other GNOME based distributions come with an application simply called “Startup Applications”. It can be used for managing apps and scripts that run on a fresh system reboot or login.
Launch the “Startup Applications” app from the application launcher and click on “Add” button to add a new entry.
Fill up the “Name” and “Command” fields as per your needs and then click on “Add” button to finish creating a new entry. The entry created in the screenshot below will send a “Make a Backup” reminder as a system notification on every reboot / login. You can replace it with your own command or with full path of your bash script. You can also use any existing system command or executables usually located in various “bin” folders across the filesystem.
As stated above, a backup reminder is shown on every reboot.
Systemd is a daemon and service manager that contains various utilities to manage system processes and OS components. In its simplistic form, it is typically used to start and end services in a fresh boot cycle.
Systemd can be used to automatically launch an app or run a script on new a boot. To create the same backup reminder notification explained above, first you have to create the required folders and file by running the commands below:
$ nano ~/.config/systemd/user/backup_reminder.service
Replace “nano” with command of your favorite text editor. Replace “backup_reminder” with any other name that you prefer.
Paste the code below in the backup_reminder.service file created using the command above.
Description=Sends a backup reminder on every reboot
ExecStart=bash -c 'sleep 10; notify-send "Make a Backup"'
The code above is pretty straightforward. It sends a “Make a Backup” notification 10 seconds after the graphical session is loaded (once every reboot or login).
Run the commands below to enable the service so that it can automatically run on every reboot.
$ systemctl --user enable backup_reminder.service
$ systemctl --user daemon-reload
This is but a simple example of running a basic command on boot using systemd. You can also create advanced services with multiple conditions and multiple commands. For more information, refer to systemd man page by running the command below:
Note that this example explains creating a new service that doesn’t require root access and is suitable for auto-starting apps that doesn’t require root permissions. If you want to auto-start scripts requiring root access, you have to create a new systemd service in “/etc/systemd/system” directory instead of “~/.config/systemd/user” folder and omit “–user” switch in the commands mentioned above.
Cron is a tool that can periodically run scheduled tasks according the conditions specified by a user. These scheduled jobs are created in Crontab in a pre-defined format. In simple terms, Crontab tells Cron which jobs to run at what point of time.
Like systemd, crontab jobs can be used to launch apps and run scripts automatically on boot. To add a new cron job, run the command below:
Add the following lines at the end of text file (automatically launches GNOME terminal on every reboot):
@reboot sleep 30 && DISPLAY=:0 gnome-terminal
You can use your own command or supply full path to a shell script.
Note that unlike systemd, cron can’t detect if graphical session has been loaded or not. You have to specify some estimated wait period till the X server loads and a display identifier. You can know about your display ID by running the command below:
The delay before execution of command or script depends on your system configuration and boot time.
Another method to run scripts and commands at startup is to use “rc.local” file. Note that in my testing, I was not able to defer script execution till graphical session was live. Adding any sleep delay lead to delay in the showing of the login screen itself. Because of this, I had no success in running graphical apps on startup using rc.local file. Editing rc.local also requires root access, unlike all other examples explained above.
To add commands / scripts to rc.local file, run the command below (creates a new rc.local file if it doesn’t exist):
Add your commands between “#! /bin/bash” and “exit 0” lines, as shown below:
Make rc.local file executable by running the command below:
Just reboot for the startup script to take effect.
These are a few methods that can be used to automatically run scripts and apps on startup. If you are looking to run scripts that doesn’t require root access, I would recommend using “Startup Applications” GUI app. If you want to run apps and scripts with root access, I would suggest you to create a system level systemd service.