Fine-tuning autoload in Ubuntu Server. How to add a program to startup in Linux? Ubuntu startup programs

There are 2 main levels of autorun in Ubuntu - in the systemd init system and in the production environment. For custom software, autoloading in Ubuntu can be configured in a convenient graphical utility. You can add a program or your own script to autorun.

Startup in Ubuntu

The current Ubuntu 18.04 uses GnomeShell as its default desktop environment. To configure autorun in this version of the OS, you need:

Advice! In order not to be mistaken with the correctness of the command, you need to install the Main Menu utility from UbuntuSoftwareCenter.

In it, select the item of interest and open its properties. The correct command is copied from the line of the same name.

The launch of applications in Ubuntu that are present in the list and marked with a checkmark will occur every time the OS is started. All these are applications of this user, system programs and services, the utility does not display. In previous versions of the operating system with the Unity graphical shell, the startup configuration utility is also searched for by searching the Dash menu or opened using the command in the terminal:

gnome-session-properties.

How to add a script to Ubuntu startup

In addition to specific software, it is easy to add a self-written script to Linux startup to optimize your computer work. For this you need:

  1. Make the script executable with the command:

    sudochmodugo+x /home/user/script.sh

    (where user is the username and script is the name of the script, but the path to your .sh file may be different: it must be written in full, otherwise the script will not run).

  2. Open "Startup Applications" and click "Add".
  3. Enter the name of the script, and as a command - the full name with the path to the file, for example, /home/user/script.sh .
  4. Click on "Add".

There is another, classic but deprecated way to set up a script autorun in Ubuntu. For its implementation you need:


This method is also suitable for autoloading on UbuntuServer, where there is no graphics.

Deferring Autostart Programs in Ubuntu

If there are a lot of programs in Ubuntu startup, they need to be loaded in a certain order or run some time after the system boots - the delay function will help. To activate it, the sleep unix utility is used. You can use the capabilities of this tool in the already familiar program "Startup Applications". For this you need:

  1. Open the settings window for startup applications.
  2. Highlight with the cursor the program whose autorun needs to be delayed, and use the "Change" button.
  3. In the "Command" line, before the existing entry, enter:

where the number after the word "sleep" is equal to the number of seconds that the application is idle immediately after the system starts, before starting. There must be a space between this formula, at the end of which a semicolon is required, and the command to start the application.

In the above example of delayed program startup in Ubuntu, the application remains in a sleep state for 2 minutes before opening. For example, starting half an hour after the OS starts will look like sleep 3h "program name". Example where Transmission starts after 1 hour:

sleep 1h; transmission-gtk %U

Similarly, you can add delayed execution of a user script or launch of a media file to autoload in Linux.

Removing from Ubuntu Autostart

To remove software from Ubuntu startup, you can use one of 2 methods: through "Automatically Started Applications" or manually. In the first case, just run the utility, select the desired program and click "Delete". Simply unchecking the box disables the program and it will not start at system startup.

The manual way involves using the autostart folder in Ubuntu. It is located at /home/username/.config/autostart/ (to see this hidden folder, open the home directory in file manager and press Ctrl+H at the same time). It contains files with the .desktop extension for each program from autorun, which are automatically generated when added through a graphical utility. It is enough to delete the file so that the program no longer opens when you turn on the computer.

Conclusion

Startup in Ubuntu has many ways to fine-tune. Its possibilities are difficult to capture. Proper use of this tool makes the Linux OS more practical in everyday tasks.

We use many programs all the time and run them every time the system starts, and some, such as instant messengers or various utilities, must constantly run in the background in order to properly perform their task. To do this, operating systems have autoload. Programs run automatically after the OS or graphical shell has started.

In this article, we'll talk about how autoloading works in Ubuntu, what autoloading methods exist on this system, and how to add programs there.

AUTOLOAD IN UBUNTU

The first thing to say is that Ubuntu has several levels of startup. I will not talk about systemd and here, since I already covered this in one of the first articles. A program or script can be added to startup in the following ways:

  • Utility "Automatically launched applications";
  • Folder "Autostart";
  • rc.local file;
  • xinitrc file;

We will look at the first two methods, since the last two are more designed for scripts, not programs. First, autoload in Ubuntu using the standard utility.

Open the system menu and type "Auto" and open the program that will be the first in the list, Startup Applications:

This will list all applications that are currently starting automatically. To add another application to ubuntu startup, click the button "Add":

Here you need to enter the name of the new item, as well as the command to launch the application. You can select an application in file system if you don't remember the path by clicking the button "Review":

You can specify parameters in the command, if necessary. You can also fill in the field "Description", but this is no longer required. If you need a certain program from the main menu, but you don't know where to find it and how to write its command, you can look it up in the same menu. But for this we need to install the utility first "Main menu" from app center:

Here you need to pay attention to the line "Command", copy it and you can use it for autoload.

The next way is the startup folder. It is located at ~/.config/autostart. In fact, this is the same way, only it can be performed without a graphical interface. When you set up autoloading through an application, settings files called desktop. For example:

I remind you that in order to view hidden files you need to press the key combination Ctrl + H. Now let's try to create a new configuration file for one more program. Here is its syntax:


Type = Application
name= new_item_name
exec = command
icon= full_path_to_icon
Comment = comment
X-GNOME-Autostart-enabled= true

For example, let's make a file to launch the VLC player, it will look like this:

vi ~/.config/autostart/vlc.desktop


Type=Application
Name=VLC
Exec=vlc
Icon=/usr/share/icons/hicolor/16x16/apps/vlc.png
Comment=VLC Media Player
X-GNOME-Autostart-enabled=true

Done, and the most interesting thing is that if you open the startup application. Then this item will also appear there. This is such a way to configure without a graphical interface.

FINDINGS

In this short article, we looked at how autoloading ubuntu programs is configured. different ways- through the graphical interface and the terminal. You can use this knowledge to improve your system experience. Even though Ubuntu switched to Gnome, autoloading in ubuntu 16.04 still looks pretty much the same. If you have any questions, ask in the comments!

Related posts:


We create an empty file.

On the first line we write:

#!/bin/sh

This line specifies which command shell to use. Next are your commands.

Let's save it under the original name (so as not to coincide with the existing ones) in the /usr/sbin/ directory.

In order for the script to run at boot, you must write it in the /etc/rc.local file before the exit 0 line. If you do not have given file, create it and paste the following content into it:

#!/bin/sh -e #Insert a line with your script here. /usr/sbin/mescripts exit 0

A slightly more complicated way

Allows you to set the loading sequence and script behavior at different runlevels.

We study /etc/init.d/skeleton , on its basis we create a script /etc/init.d/my_script , which will start / stop our application.

These directories contain scripts for stopping services:

/etc/rc0.d/ /etc/rc1.d/ /etc/rc6.d/

in these - scripts for starting services:

/etc/rc2.d/ /etc/rc3.d/ /etc/rc4.d/ /etc/rc5.d/

We study them for sequence (the smaller the first digit at the beginning of the script, the earlier the application starts/stops). This can be critical if your application depends on some services.

After we have decided on the launch script and the order, we do the following:

Sudo update-rc.d my_script start 70 2 3 4 5 . stop 20 0 1 6 .

Here I have defined that my script should start later than the others (70), and stop early (20). The numbers 2 3 4 5 0 1 6 indicate the load levels.

If you don't like something, you can delete everything

sudo update-rc.d -f my_script remove

See man update-rc.d for details.

Executing a script on network on/off

There is a directory /etc/network/ with subdirectories if-down.d , if-pre-up.d , if-post-down.d , if-up.d . If you place the script in one of these subdirectories, then it will be executed, respectively, on shutdown, before turning on, after turning off or when the network is turned on.

Another way is to specify one of the following directives in the /etc/network/interfaces file: up , pre-up , post-up , down , pre-down , post-down . For example, the line

Post-up /path/to/script.sh

after turning on the network will execute the script script.sh. You can read more in man interfaces .

In any case, the script must have permissions set to allow execution, otherwise it will not be able to run.

And most importantly, completely free. operating system, built on the Linux kernel. Therefore, more and more computer owners are becoming its users. If you are among them, then you should, no, not learn - there is nothing to learn there - just understand and master the basics of management. For example, how to set up autostart of applications and services in Ubuntu.

Autoloading in Ubuntu is the same process as on other operating systems. You must remember and imagine how on Windows, when you turn on your PC, Skype, uTorrent and a number of other programs immediately launched. Ubuntu has several programs that are included with the computer in the same way. Their list can be changed as you like: remove unnecessary ones or add a few more.

Adding processes

Adding a new service, utility, or command to Ubuntu startup is not difficult at all. This is done using the menu.


You can add in this way not only utilities, but simply services. Only in the top line you need to select the appropriate type, if any, and also enter the desired command. The main thing is to know it.

Postponement

Ubuntu also has a great opportunity to delay switching on - just like on modern ovens or multicookers. It is done by introducing a small addition to the text of the command. Which looks like this: "sleep 20;" (without quotes). Instead of 20, you can enter any number that will mean the number of seconds that the process “sleeps” before starting. Thus, you can give the system time to fully boot, after which certain services or programs will already start to turn on.

Removal

If you have already tried adding programs to automatic start, then you understand that it will be very easy to remove them from there.

  1. Again, go to the autoload management utility.
  2. We find those processes that we do not need.
  3. Select them, click "Delete".

Now unnecessary processes will be removed from Ubuntu autorun. It is better to have there only the essentials.

It should be understood that if you add a lot of applications to Ubuntu startup, the OS will boot much slower. Therefore, it is better not to overdo it and properly adjust the automatic start settings. The function is really very convenient and, together with the delay start, gives good tool for automatic control of system actions.

After starting the computer, as a rule, we have to run some programs. which we use most often. Also, after starting the system, we may need to perform some specific actions, for example, create the necessary files or set some parameters. It is very inconvenient to do it manually. To solve such problems, there is an autoload.

In this article, we will look at how Linux autoload works, how to add startup programs, where it would be more correct to add them, and what types of autoload there are in this operating system.

To understand how autoloading works, you first need to remember what happens during . Once the kernel has completed its initialization and is ready for further work, it will transfer control to the initialization system. The initialization system is the main process, it is he who starts all other processes in the system.

There are processes that an init system like systemd starts by default, but you can also configure it to start the processes you want. Also, many child processes execute script files or have some ability to run the programs you need. Most desktop environments have this feature as well.

Consider the main autoload levels that you can use:

- you can specify any program that will run after the kernel starts instead of the initialization system;- launching the main system services, additional services, as well as your scripts at the stage of system initialization;- obsolete method of loading scripts, performed before starting the graphical environment;- you can execute your scripts or commands after starting the login manager, but before starting the environment;- launch desired programs or scripts immediately after the start of the X server;- most environments support autoloading programs, you can even configure delayed start and other parameters there;- the latest option is autoloading at the level of a separate command shell, you can execute the necessary commands automatically as soon as the terminal is launched.

Autoloading at the kernel level is unlikely to be very useful to you for everyday use, but I decided to mention it because it is possible. With the init kernel parameter, you can specify which program should be run immediately after the kernel has finished initializing. For example, you can load the Bash shell instead of Systemd. To do this, just tweak the kernel startup line in the Grub configuration file or during startup. Add the init parameter to the end:

sudo vi /boot/grub2/grub.cfg

linux /vmlinuz-4.8.0-22-generic root=/dev/mapper/systems-ubuntu ro quiet init=/bin/bash

But it is usually more convenient to change this value temporarily, in the Grub menu. Read more about this in the article. This does not autoload a linux program, but it can sometimes be useful nonetheless.

Most often, when it says autoloading Linux, it means autoloading services using the initialization system. systemd is a very advanced system. It supports dependency resolution, parallel launch, deferred launch, and restart on error. In Systemd terminology, all launched programs are represented by unit files, each unit describes the parameters of the program, its executable file, as well as additional requirements for launching.

The systemctl command is used to add or remove services from autostart. To add a service to autostart run:

sudo systemctl enable service_name

And to disable its autorun linux:

sudo systemctl disable service_name

For example, if you want to add Apache to startup, you need to run:

sudo systemctl enable apache

You can also check if the service has already been added to startup:

sudo systemctl is-enabled httpd

If you do not know the exact name of the service file, but you know its first letter, then you can use autocompletion, as you would anywhere else in the terminal, using the Tab button:

You can also view all services that have been added to startup using the command:

systemctl list-unit-files | grep enabled

It used to be customary to place all scripts that run by default in the /etc/rc.local file. This file still exists, but it is a holdover from the SysVinit init system and is now kept for compatibility only. Scripts only need to be loaded using Systemd.

To do this, it is enough to create a simple unit file and add it to autoload, like any other service. Let's create this file first:

sudo vi /lib/systemd/system/runscript.service


Description=My Script Service
After=multi-user.target


type=idle
ExecStart=/usr/bin/local/script.sh


WantedBy=multi-user.target

In section unit we give a short description to our file and say with an option After that you need to run this script in multi-user mode (multi-user). Section Service the most important, here we indicate the type of service - idle, which means that you just need to run and forget, there is no need to monitor, and then in the parameter ExecStart specify the full path to our script.

It remains to set the correct rights:

sudo chmod 644 /lib/systemd/system/runscript.service

Then update the configuration and add a new script to Linux startup:

sudo systemctl daemon-reload
$ sudo systemctl enable myscript.service

After the next reboot, this script will run automatically. Please note that for each script that you are going to run, the rights must be correctly set, namely, the execution flag must be set. To do this, use the chmod command:

sudo chmod u+x /usr/local/bin/script

In the parameters, we pass the address of the script file to the utility. Executability is a required parameter for all methods.

Often, lightweight desktop environments and window managers use autoloading at the X server level to autoload programs. All scripts launched in this way will start after the X server is started. To set up such Linux autoloading, you just need to add the path to the desired script to the file ~/.xinitrc or /etc/X11/xinit/xinitrc. The syntax for the added line is:

exec script_path &

If the ~/.xinitrc file does not already exist, then you need to create it:

cp /etc/X11/xinit/xinitrc ~/.xinitrc

Then, to add a script to linux autoload, edit the file as you need. For example, let's start Firefox:

exec /usr/bin/firefox &

Powerful desktop environments such as Gnome and KDE have their own startup applications. For example, in Gnome the program is called "Apps that start automatically". You can launch it from the system's main menu:

Here you can see all the programs that have already been added to startup. You can disable each of them simply by unchecking the box.

To add new program or script to autoload click "Add":

Here you need to fill in three fields:

  • Name- an arbitrary name for the command by which you can find it;
  • Command- full path to the program's executable file;