Running scripts in linux startup

How to Run a Script on Startup in Ubuntu?

Sometimes, we need to perform specific tasks every time the system starts or reboot the script is executed automatically on startup to avoid the user performing those tasks manually. The startup script allows the user to schedule and automate several tasks simultaneously.

To run the script on startup in Ubuntu, the executable files are put in the startup folder to perform a specific task when the system starts. This post will discuss how to modify the system files to run scripts on startup with the help of the following topics:

Method 1: Run Script on Startup Using Systemd File

We can modify the systemd file to run a script on startup by following the below steps:

Step 1: Create a Bash Script

Create a bash script file named “StartScript.sh” to get all the details about the user, which will run automatically on startup:

#!/bin/bash echo "Kernel Version: $(uname -a)" > /etc/kernelinfo.txt

Note: Make sure the script file is executable, using the following command:

Step 2: Create a systemd Service

Create a new startup Systemd File within the “/etc/systemd/system” system file having the “.service” extension.

For instance, /etc/systemd/system/ScriptService.service file is created using the following command in the nano editor:

$ sudo nano /etc/systemd/system/ScriptService.service

Write the below content to run the bash script “StartScript.sh” on startup:

[Unit] Description=Custom Startup Script [Service] ExecStart=/home/ubuntu/StartScript.sh [Install] WantedBy=default.target
  • Unit: It stores the metadata and other information you want to store related to the script.
  • Service: Tells the system to execute the desired service, which will run on startup.
  • Install: Allows the service to run the WantedBy directory at the startup to handle the dependencies.

Step 3; Set Permissions of the Service File

Set the user executable file permissions (in this case, binary mode of permissions) for the Service unit file using the following command:

Note: In binary mode, Read=4, Write=2, and Execute=1.

$ chmod 644 /etc/systemd/system/ScriptService.service

  • 6: Provides read and write permissions to the user.
  • 4: Provides read permissions to the group and others.

Step 4: Enable the Service

Use the below-stated command to enable the customized service (which allows the execution on each startup):

$ systemctl enable ScriptService.service

Every time the system starts/reboots, the script file will execute automatically and display all the user information in the /etc/kernelinfo.txt file.

Method 2: Run Script on Startup Using Cron Job

The cron job can be created to run on startup. For instance, to run a script that executes the user details when the system reboot or restarts, follow the below steps:

Читайте также:  Linux check what is swapping

Step 1: Open the crontab File

Open the crontab file by running this command in the terminal:

Note: If you are opening the crontab file for the first time, the system will ask you to specify the text editor in which the crontab will be opened. Select the desired editor by entering the number written in front of every editor.

Step 2: Add a cron Job

Add a new crontab job to execute on startup by entering the following code in the crontab file:

Note: StartScript.sh is the same script that was used in the last section; provide the full path of the script file.

$ @reboot sudo /home/ubuntu/StartScript.sh &

After adding the code, Press the “Ctrl + O” to save, then “Enter” to save the file name, and then press the “Ctrl + X” keys to exit the editor.

The cron job is successfully saved to run the script (print user details) on startup.

Method 3: Run Script on Startup Using rc.local File

The rc.local file can also be used to run the script on startup. To run the desired script on reboot/startup, use the following steps:

Note: The below first two steps will be performed in case there is no “/etc/rc.local” file which can be checked by using the “ls /etc/rc.local” command. In the absence of this directory, it will show blank output, else the directory path.

Step 1: Create rc.local File

Create the “/etc/rc.local” file in the nano editor using the below-mentioned command:

Press “Ctrl + O” to save the file, then press the “Enter” key to save the anime, and press the “Ctrl + X” key to exit the editor.

Step 2: Create an rc-local Service

Create the rc-local service file to manage this service in the default system file “/etc/systemd” by using this command:

$ sudo nano /etc/systemd/system/rc-local.service

Now, press the “Ctrl + O” to save, the “Enter” and “Ctrl + X” to exit.

Step 3: Make the rc.local File executable

Change the rc.local file permissions to make it executable by running the below command:

Step 4: Edit the rc.local File

Open the rc.local file in the nano editor (to add the startup script) by using the following command:

Add the following command in the rc.local file as shown in the below picture:

Note: Replace the “StartScript.sh” with your desired script file name.

sudo /home/ubuntu/StartScript.sh &

Press the “Ctrl + O” to save, the “Enter” and “Ctrl + X” to exit.

The script is saved in the rc.local file, which will automatically run every time the system reboots/starts.

Conclusion

To run the desired script on startup, several methods are used, which include modifying the “systemd”, “cron”, and “rc.local” files. In the systemd method, the desired script is created, added script as a service, and enabled the service to run on startup. Moreover, the script job is added to the crontab file, and the service is added to the rc.local file to run the scripts on startup. This post has addressed numerous methods to run a script on startup in Ubuntu.

Читайте также:  Скопировать группу файлов linux

Источник

How to Run Script on Startup in Linux

Sometimes you may want to run your shell script on the system startup or when the user logs in. For example when you need to run any desktop application, change screen resolution settings, start the remote desktop service, etc.

You can do this in a few ways. The first way — use the autostart feature in your desktop environment, for example, GNOME or KDE. The second way — use the Systemd initialization system which is used in most distributions nowadays. In this article I will explain how to run script on startup in Linux.

Creating Script for Autostart

Maybe you already have a script that you want to run on startup. But if it is not, or you want to practice with another bash shell script, it is pretty straightforward to create one. Let’s create an example script in the /usr/local/bin directory which prints «Hello world» into a file. This directory usually is used for custom binaries, so you can use it for storing your scripts too. In this article I will use the path to the script /usr/local/losst-script.sh. The following script creates file with the output in the current user home directory.

sudo vi /usr/local/losst-script.sh #!/bin/bash
echo «Hello world» > ~/file

After saving the script, make it executable using the following command:

sudo chmod ugo+x /usr/local/losst-script.sh

You can executable the shell script and ensure that it works. Now let’s have a look at how to add it into the system autostart.

How to Run Script on the System Boot

Systemd does not have any place where you can add scripts that you want to run on the system startup. But you can create systemd unit file which will run your script. Use the following command to do this:

sudo systemctl edit —force —full script.service

The command will open a text editor. Paste these lines into it:

[Unit] Description=My Script Service After=multi-user.target [Service] Type=idle ExecStart=/usr/local/losst-script.sh [Install] WantedBy=multi-user.target

You should set the path to the script or a required command into the ExecStart parameter. Note that I have used the Idle service type here, which means that the process will not fork. Now, let’s add this systemd service to autostart:

sudo systemctl enable srcipt

If Systemd can’t find your new service, update information about systemd configuration files using this simple command:

sudo systemctl daemon-reload

After this, the script will be started on the system startup. Reboot the system and find the file with the «Hello world» content in the /root directory to ensure that everything works fine.

If you prefer to use the old approach to autostart custom scripts with rc.local file, you can create the /etc/rc.local file, make it executable, and use the path to the file into the ExecStart parameter in the unit file. After this, you will be able to use this file as well as before Systemd came.

Читайте также:  Linux get my ip command line

How to Run Script on User Log In

1. GNOME Autostart in GUI

You can configure this using a graphical user interface. Run the Startup Applications utility from the main menu. Also, you can run this utility in the terminal:

Here press the Add button. Then, in a new modal window paste a full path to your script in the Command field. Also, you can choose the path to the script in the filesystem using the Browse button. After this press the Add button at the bottom of the modal window:

The script will be started after GNOME finishes loading. You can reboot the system and ensure that you have the file with the «Hello world» text in your home folder.

2. GNOME Autostart Manually

Everything described in the previous section can be done manually, without the Startup Applications utility. Shortcuts for applications that must be started automatically are located in two directories:

  • /etc/xdg/autostart/ — for all users;
  • ~/.config/autostart/ — for the current user.

Create a new shortcut with the *.desktop extension which will run your script in one of the folders above with the following lines:

vi ~/.config/autostart/script.desktop [Desktop Entry] Name=Script Type=Application Exec=/usr/local/losst-script.sh

Here you should paste the path to the script executable into the Exec field. Then paste the name of the shortcut into the Name field and save the changes. After this, your script will be started automatically as well as in the previous section. The Startup Applications can see this shortcut, so you can manage it there too.

3. Autostart using Systemd

The Systemd initialization system can start a dedicated set of services for each user. You can manage these services using the systemctl utility with the —user option. Use this command to create a new user unit file for your script:

systemctl edit —user —force —full script.service [Unit] Description=My Script Service After=default.target [Service] Type=idle ExecStart=/usr/local/losst-script.sh [Install] WantedBy=default.target

The unit file will be created only for the current user. For this case, it will be placed in the /home/sergiy/.config/systemd/user/script.service file. Pay attention, that multi-user.target here is not available, so you should use default.target. Now, add this unit to autostart:

systemctl enable —user script.service

After this, you can reboot the system and ensure that everything works fine.

Wrapping Up

Now, you have learned how to run a script on startup in Linux. It may be difficult for new users, but you also can read the article about Services Autostart in Linux and How to Manage Services in Linux to understand how it works better.

Found a mistake in the text? Let me know about that. Highlight the text with the mistake and press Ctrl+Enter.

Источник

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