Autorun program in linux

How to Auto Start an App in Your Linux (Various Distros Support)

Want to automatically run an application, script or command on startup of your Linux Desktop? Here’s how to do the trick either via graphical configuration tool or by editing config file.

By following this tutorial, your specified apps will start automatically after login. If you want to run something even before user login, please create a systemd service instead.

And, this tutorial is tested in various Linux distributions, including Ubuntu, Manjaro, Fedora, Debian, Linux Mint, Ubuntu MATE, Kubuntu, XUbuntu, Linux Lite, Pop! OS, Zorin OS, and LUbuntu.

Add Startup Applications in Linux Desktop

The configuration varies depend on your Desktop Environments. If your system is not included in this post, try the last one to edit config file.

For GNOME Desktop (Ubuntu, Fedora, etc.)

GNOME Desktop, defaults in Ubuntu, Fedora Workstation, optional in Debian, Arch and Manjaro has the “Gnome Tweaks” tool that can do the job.

Firstly, search for and install Gnome Tweaks from Ubuntu Software, Gnome Software, Pamac or your system package manager.

Then, launch the tool and navigate to “Startup Applications” in left pane. Finally, click on the “+” button will open up the dialog, which allows to search or browse through applications.

Once you selected the desired app, click on ‘Add’ button and done!

For Ubuntu, Linux Mint, Ubuntu MATE, Zorin OS

Ubuntu has a specific tool to manage startup applications. As well, Linux Mint, Ubuntu MATE, and Zorin OS use either same or similar tools to do the job.

First, search for and open “Startup Applications” either from ‘Activities’ overview screen or start menu depend on your system.

In the pop-up dialog, you can simply click “Add” or “+” to start adding a startup program.

Sadly, this dialog is not so friendly to beginners in Ubuntu. User has to know the executable command for launching certain app. Or, browse in “Other Locations -> Computer -> usr -> bin” directory.

However, it’s a bit more powerful than the function in Gnome Tweaks. User can add a time delay via sleep command with how many seconds to wait until launching, or run a personal script.

Читайте также:  Импорт базы данных mysql linux

  • sh -c “sleep 10; firefox” – automatically launch Firefox with 10 seconds delay

For Linux Mint, the utility is more friendly as “+” button asks to either run custom command or choose an application. And, there’s an option to set the time delay.

For KDE Plasma Desktop (KUbuntu, Manjaro KDE, Ubuntu Studio, etc.)

The KDE Plasma desktop is more customizable than most other Linux desktops. Not only for running app on startup, it also makes easy to run script on logout.

Firstly, search for and open “System Settings” from start menu. Then, navigate to “Startup and Shutdown -> Autostart“.

Then click on “+ Add…” button in the bottom, to either add application, login script or logout script.

After adding an app or script, user can click the “Properties” icon for each app/script to open dialog to edit the permissions, command, etc.

For Xfce4 Desktop (Linux Lite, XUbuntu, etc.)

The lightweight Xfce4 desktop has a “Session and Startup” dialog allows to manage startup applications.

User can open it either by searching from start menu or by running xfce4-session-settings in terminal. When it opens, navigate to “Application Autostart” tab, and click on “+” button to create:

  • Name: something as you want.
  • Description: optional
  • Command: browse in Other Locations > Computer > usr > bin, or type manually.
  • Trigger: select run app or command at login, logout, shutdown, restart, suspend, hibernate or switch user.

For LXQt Desktop (LUbuntu, etc.)

For LUbuntu, Debian, Fedora, etc. with LXQt desktop, open start menu and navigate to “Preferences > LXQt Settings > Session Settings“.

Or run lxqt-config-session in terminal to open the tool. Then, navigate to “Autostart” tab in the left, and finally click “Add” button to add auto-start applications.

NOTE: Here, app under “Global Autostart” starts in all sessions (if any), and those under “LXQt Autostart” will start only in LXQt session.

Manually edit config file (Universal)

Auto-start apps, commands, or scripts usually have corresponding .desktop files either in “/etc/xdg/autostart” (for all users) or “.config/autostart” for current user only.

In case non of the previous steps work for your Linux PC, try to manually create .desktop file, by opening “Files”, pressing Ctrl+H to display hidden folders, and navigating to .config -> autostart (create if not exist).

Then create and write in following lines into the file:

[Desktop Entry]
Type=Application
Exec=your-command-here
Hidden=false
Name=whatever-name
Comment=Optional

User can add optional lines in the .desktop files, such as:

  • OnlyShowIn= – to only show it in specified sessions. The value can be: GNOME, KDE, Xfce, LXQt.
  • Terminal=false – whether needs to be run in a terminal or not
  • X-GNOME-Autostart-Delay=5 – delay app startup in seconds. For Ubuntu, Fedora, Linux Mint Cinnamon, & other Linux with GNOME.
  • Hidden = true – Disable the app auto-start without removing the file.
As mentioned, non-GNOME desktop can use Exec=sh -c "sleep 10; command-here" to add a delay before automatic launching the app.

Summary

In general, each Linux desktop environment has a graphical utility to manage auto startup applications. Though, user can manually create .desktop files under either ‘.config/autostart‘ or ‘/etc/xdg/autostart‘ directory that works in most Linux Distros.

Читайте также:  Git clear credentials linux

Источник

How to start program at Linux boot automatically

Linux startup consists of several stages, during which you can configure a program to start automatically. This can be a single command, a sequence of commands, or an executable shell script. However, startup procedures may vary between different Linux distributions and versions.

Modern Linux systems boot into systemd, while older Linux versions utilize System V init. Regardless, both systems will execute cron and rc.local before loading the desktop environment, such as GNOME or KDE. In contrast, server-based Linux distributions do not load a desktop environment and instead provide a login prompt at the console. After logging in, the default shell like Bash runs.

Methods to automatically run program on Linux startup:

Running a program automatically on Linux startup via systemd

systemd is the standard system and service manager in modern Linux, responsible for executing and managing programs during startup, among other tasks. Compatible programs will include service unit files used by systemd to manage the program’s execution.

To configure systemd to run a program automatically during Linux startup, follow these steps:

$ sudo systemctl list-unit-files --type=service [sudo] password for user: UNIT FILE STATE accounts-daemon.service enabled apparmor.service enabled apport-autoreport.service static apport-forward@.service static apport.service generated apt-daily-upgrade.service static apt-daily.service static atd.service enabled autovt@.service enabled blk-availability.service enabled bootlogd.service masked bootlogs.service masked bootmisc.service masked checkfs.service masked checkroot-bootclean.service masked checkroot.service masked cloud-config.service enabled cloud-final.service enabled cloud-init-local.service enabled cloud-init.service enabled console-getty.service disabled ##### snipped #####

You’ll have to create your own service unit if it’s a custom program or if your program doesn’t come with one during installation
Related: Creating and modifying systemd unit files

$ sudo systemctl is-enabled mysql disabled
$ sudo systemctl enable mysql Synchronizing state of mysql.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install enable mysql Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /lib/systemd/system/mysql.service.
$ sudo systemctl is-enabled mysql enabled

Running a program automatically on Linux startup via cron

cron is a daemon used to execute scheduled commands stored in the cron job table (crontab), which is unique for each user in the system. It starts during system boot, either via systemd or System V init, and you can schedule your program to run during system boot by following these steps:

You’re required to select an editor for the crontab if this is the first time the user uses the command.

$ crontab -e no crontab for user - using an empty one Select an editor. To change later, run 'select-editor'. 1. /bin/nano 

A crontab will be created for the user running the command and will be executed using the privileges of the user. If you need your program to run as the root user, run crontab -e as the root user itself.

# m h dom mon dow command @reboot
@reboot /sbin/ip addr | grep inet\ | tail -n1 | awk '< print $2 >' > /etc/issue && echo "" >> /etc/issue
$ crontab -e crontab: installing new crontab $
$ crontab -l # m h dom mon dow command @reboot /sbin/ip addr | grep inet\ | tail -n1 | awk '< print $2 >' > /etc/issue && echo "" >> /etc/issue

Running a program automatically on Linux startup via rc.local

rc.local is a legacy script from the System V init system, executed before displaying a login screen for the desktop environment or terminal login prompt. It is typically a Bash shell script capable of running any commands.

To configure your rc.local script, follow these steps:

As the root user, open or create the /etc/rc.local file using your preferred editor if it doesn't exist.

#!/bin/bash /sbin/ip addr | grep inet\ | tail -n1 | awk '< print $2 >' > /etc/issue echo "" >> /etc/issue exit 0

Running a program automatically on GNOME startup

GNOME is the default desktop environment for Linux distributions like Ubuntu and Red Hat. You can configure GNOME to run programs upon user login by following the instructions in the linked article:

Running a program automatically on KDE startup

KDE is another popular Linux desktop environment and the default for Kubuntu and openSUSE. It can also be configured to run programs when a user logs in, as detailed in the related article:

Running a program automatically on new Bash session

A new shell program will be spawned when you start your terminal session. Bash is the default shell for most Linux distributions, and when started, it will look for the following files in the particular starting a terminal session, a new shell program will be spawned. Bash is the default shell for most Linux distributions and, when initiated, looks for and executes the following files in order:

/etc/profile ~/.bash_profile ~/.bash_login ~/.profile

These files contain commands and logic for setting up environment variables and running required programs in the Bash language. They are also typically configured to execute other files, such as /etc/bashrc, /etc/bash.bashrc, and ~/.bashrc.

You can edit any of these files to run your program when a Bash session is started. Below is a part of a typical ~/.bashrc file:

PS1='$\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] \$ ' PATH=/home/user/bin:$PATH export EDITOR=/usr/bin/vim alias ll="ls -l"

Author: Mohd Shakir Zakaria
Mohd Shakir Zakaria, a proficient cloud architect, is deeply rooted in development, entrepreneurship, and open-source advocacy. As the founder of Simplified Guide, he combines these passions to help others navigate the intricate world of computing. His expertise simplifies complex tech concepts, making them accessible to everyone. Discuss the article:

Comment anonymously. Login not required.

Источник

Оцените статью
Adblock
detector