Startup scripts for linux

How do I run a script at start up? [duplicate]

I need this script to run every time the system starts (even if no one logs in to the system). What do I need to do in order to make this happen?

5 Answers 5

You will need root privileges for any the following. To get root, open a terminal and run the command

and the command prompt will change to ‘#’ indicating that the terminal session has root privileges.

Alternative #1: Add commands to /etc/rc.local

with content like the following:

# This script is executed at the end of each multiuser runlevel /path/to/my/script.sh || exit 1 # Added by me exit 0 

Alternative #2: Add an Upstart job (for systems older than 15.04) (not recommended)

with content like the following

description "my job" start on startup task exec /path/to/my/script.sh 

Official statement from upstart website -> «Project is in maintaince mode only. No new features are being developed and the general advice would be to move over to another minimal init system or systemd.»

Alternative #3: Add an init script (obsolete)

Create a new script in /etc/init.d/myscript .

(Obviously it doesn’t have to be called «myscript».) In this script, do whatever you want to do. Perhaps just run the script you mentioned.

chmod ugo+x /etc/init.d/myscript 

Configure the init system to run this script at startup.

update-rc.d myscript defaults 

It is possible to do the same thing by writing a short Upstart job. Initscripts are still supported, however, and are easy to use.

You don’t need root, or to even login.

You can edit your crontab ( crontab -e ) and create an entry like this:

This way, you can run it as a regular user. @reboot just means it’s run when the computer starts up (not necessarily just when it’s rebooted).

P.S.: Regarding comments that this does not work properly

Some have said that this doesn’t work on Debian-based distros, such as Ubuntu. I have personally successfully used this method on both Ubuntu and Mint. There are a few things to consider, however.

The @reboot jobs will run when the cron daemon starts. I’ve found that on Debian-based distros, this may occur before the /home partition has been mounted. If the script you’re running is in your home folder, it will fail.

Additionally, this isn’t limited to Debian-based distros, but if your home folder is encrypted, it may not be decrypted until after you login. There is probably no way around this.

Also, your network interface may not be up yet, and if the command requires Internet access, it may fail.

Читайте также:  What is ssh daemon linux

Finally, again, this is not limited to Debian-based distros, but cron runs under a much more limited environment than your shell runs under. In particular, the PATH variable has much fewer paths. It is possible that the command being run isn’t found, if it’s in, for example, something like $HOME/.local/bin , which may be in your PATH in your shell session, but not under cron . It’s even possible that the command being run depends on some environment variable that’s not set in cron .

So, there are a number of reasons why your command will to run under cron, but it’s not because @reboot doesn’t work on your distro.

Источник

Run a Script on Startup in Linux

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

1. Overview

In this quick tutorial, we’ll explore different options for executing a script on startup in Linux. This comes in handy in plenty of situations, such as if we want to start a server application automatically.

2. Solutions

Without further ado, let’s create a simple script to execute:

#!/bin/sh echo "Last reboot time: $(date)" > /etc/motd

This piece of code sets the message of the day to be the last reboot time, so that each user can see it after their first login.

Then we’ll save our file and make it executable:

Now that our script is ready, let’s see how to schedule its execution.

2.1. Using cron

Let’s begin with the easiest solution, which involves using cron. In order to do this, we need to edit our crontab file:

Here we’ll add a line using the @reboot expression, which will execute our code once at startup:

@reboot sh /home/ec2-user/reboot_message.sh

This solution is quick and clean, since we don’t have to deal with additional configuration, but not every version of cron supports @reboot.

2.2. Using rc.local

Now let’s consider another solution that takes advantage of the /etc/rc.d/rc.local file. Since this file already runs at startup, we can append a line that invokes our script:

sh /home/ec2-user/reboot_message.sh

In order for this to work, we need to ensure that the rc.local file itself is executable:

2.3. Using init.d

Similar to the previous solution, the /etc/init.d folder contains lifecycle executables of the services managed by the system. We can also add our own by creating an LSB-compliant wrapper that starts our service:

#! /bin/sh # chkconfig: 345 99 10 case "$1" in start) # Executes our script sudo sh /home/ec2-user/reboot_message.sh ;; *) ;; esac exit 0 

This wrapper will launch our code when it’s invoked with the start argument. However, we must include a line with the chkconfig configuration, which contains the service runlevel and the start/stop priority.

After placing the wrapper in the init.d folder, we need to register our service for startup execution:

$ chkconfig --add service_wrapper.sh

Since the chkconfig command isn’t available on Debian systems, update-rc.d can be used as an alternative there:

$ update-rc.d service_wrapper.sh defaults

2.4. Using systemd

Finally, let’s see how to run a script with systemd. Similar to init.d, we need to create a service descriptor, called a unit file, under /etc/systemd/system:

[Unit] Description=Reboot message systemd service. [Service] Type=simple ExecStart=/bin/bash /home/ec2-user/reboot_message.sh [Install] WantedBy=multi-user.target 

The file is organized into different sections:

  • Unit – contains general metadata, like a human-readable description
  • Service – describes the process and daemonizing behavior, along with the command to start the service
  • Install – enables the service to run at startup using the folder specified in WantedBy to handle dependencies
Читайте также:  Отличия solaris от linux

Next, we’ll need to set the file permissions to 644, and enable our service by using systemctl:

$ chmod 644 /etc/systemd/system/reboot_message.service $ systemctl enable reboot_message.service

One thing to keep in mind is that, although many major distributions support systemd, it’s not always available.

3. Conclusion

In this article, we took a look at different ways of executing a script at startup in Linux. Each one of them has its pros and cons, but generally speaking, systemd and cron should be preferred when available. Consequently, rc.local and init.d should be used as fallbacks.

Источник

How to Auto Execute Linux Startup Scripts and Commands

Auto Execute Linux Startup Scripts and Commands

Linux startup scripts are tools or programs that are run by the kernel each time your system restarts. Users can leverage various Linux startup commands to configure programs or run certain tasks once the system has booted. Luckily, there are several ways to auto-execute startup scripts in Linux. In this guide, we will show you some simple but useful ways to tackle this. You should be able to set up personalized startup jobs once you have gone through this guide. So, stay with us and check out the following methods of executing auto startup scripts.

How to Configure Linux Startup Scripts?

You can configure startup commands in several ways. We will demonstrate the use of Linux CRON jobs and init tasks for running scripts at startup. We will also show you how to do this using Upstart.

Method – 1: Configure Startup Scripts using CRON

CRON is a simple but powerful job scheduler that can run certain tasks at system reboot. We can easily create a startup job using CRON. For example, assume you have a test script called test.sh. You can run it after each system restart by adding the following line to your crontab.

using cron for startup scripts

The @reboot directive tells CRON to run the custom script after a boot/reboot operation. Simply edit your crontab and add the above line. To edit the crontab for your user, use the below command in your favorite terminal emulator.

However, if you want to use CRON for scheduling startup scripts, you need to take care of two things. First, the CRON daemon must be running after your system reboots. This is usually the case for most Linux distributions. And, if your script uses environment variables, you must include those at your crontab.

Method – 2: Configure Linux Startup Scripts using init

One simple method of running jobs at reboot is to place them in the /etc.init.d directory. But first, make sure the script is executable. You can add execution permission to a script called test.sh using the following command.

Читайте также:  Kali linux комбинации клавиш

Now, copy it into the init.d directory using the following simple command.

linux startup commands

You need to create a symlink for this script into the rc2.d directory. You can do this by using the following command.

$ cd /etc/rc2.d $ sudo ln -s /etc/init.d/test.sh

Note that there are several rcN.d directories in Linux. The number followed by rc denotes the run level. We are symlinking our test script to run level 2 since it’s the default multi-user run level. You may need to change your run level based on your requirements. Now that we have symlinked the script, we need to rename it according to the rc naming scheme.

Note that we are adding an S followed by the number 70. The S stands for start, and you need to add it at the beginning f your script. Else init will not start your application after the boot process.

Method – 3: Configure Startup Commands using Upstart

The Upstart daemon provides immense customization and reduces many of the problems associated with system V init. Upstart doesn’t rely on arcane scripts for loading services on startup and offers a flexible event-driven system. Moreover, the configurations are done via adding a configuration file. For example, the following Upstart service runs the echo command at system reboot.

First, create the configuration file with a .end suffix in your /etc/init directory. Please do not mix it up with /etc/init.d/ since they’re different directories of your Linux file system.

description "testing Linux startup commands" start on runlevel [2345] stop on runlevel [!2345] expect fork respawn exec echo "This is a test run!"

Here are the essential details of these labels.

  • description – describes the purpose of this service
  • start – defines the run level as 2,3,4 and 5, which is the default
  • stop – exits the service whenever the system reaches a different run level
  • fork – detaches the service and makes it run in the background
  • respawn – defines auto-start in terms of process crash
  • exec – defines the command to run

Method – 4: Configure Startup Scripts using Startup Applications

Modern Linux distributions allow users to define startup scripts or commands using the GUI tool Startup Applications Preferences. You can find it by going to Activities > Type Startup > Select Startup Applications Preferences.

Linux startup apps options

Once it’s open, click on the Add button from the sidebar. It will open a new prompt and where you can add the startup script or command. Click on Browse to locate and select your script and add a description in the comment box. Don’t forget to put a name for the job. Finally, click on Add to complete the process.

add linux startup scripts

Ending Thoughts

Linux startup scripts can be a great way of running certain commands at startup. It increases productivity by allowing admins to focus on more important tasks. Plus, startup scripts are also an excellent method of adding automation to the system. Linux allows users to implement startup jobs using several methods. We have shown a handful of them for our readers in this guide. Hopefully, these will help you create personalized startup commands without any trouble. Let us know your thoughts about this guide in the comment section below.

Источник

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