Linux commands at boot

How To List Startup Services At Boot In Linux

By default, some important system services are started automatically when the system boots. For instance, the NetworkManager and Firewalld services will be automatically started at system boot. The startup services are also known as daemons in Linux and Unix-like operating systems. They will keep running in the background and do their job without any user intervention. In addition to the system services, some other third-party applications will also add themselves to the startup. In this brief guide, let us see how to find and list startup services at boot time in Linux and Unix-like systems.

List Startup Services At Boot In Linux

Finding the list of startup services will differ depending upon the init system. Systemd is the default init system for the major newer versions of Linux distributions.

If your systems runs with systemd system manager, you can list all services with the following command:

Sample output:

List all services in Linux with systemd

As stated above, this command shows the list of all services (both enabled and disabled at system boot) in your Linux system. You can verify it by looking under the STATE section in the above output. The services that are started at boot are marked as enabled, and the services that are not started are marked as disabled.

To list only the enabled services at system boot, run:

Sample output:

To list all disabled services at system boot, run:

Like I already said, some older Linux distributions may use either SysV or Upstart as their default init system.

If your system uses sysv , run the following command to list all services:

Sample output:

Here, the + indicates the service is running, and indicates a stopped service. If you see ? in the output, the service state cannot be determined (for some reason).

To list all services which are enabled at boot, run:

This command will list status of each service on each run level. A sample output of the above command will be:

In the above command, «on» means the service is started at boot.

You can also view the status of a specific service at different run level like below:

If you Linux system uses upstart , run this command to list all startup services:

The above command will show all Session jobs.

If you want to show all System jobs, run:

To list all services and show their statuses at each run level, run:

To show the state of a specific service, run this command:

Читайте также:  Что обозначает ошибка windows

Disable startup services in Linux

The more applications you install on your computer, the longer it takes for your system to boot. In order to improve your Linux system’s boot time, you need to find the unnecessary services and disable them.

Say for example, if you don’t want a service called unattended-upgrades.service to load at startup, you can disable it using command:

To know if a service is enabled at boot time, run:

Источник

How to execute a command or script at reboot or startup

Well-known services in Linux can be added on boot time without any problems, because most of them come with their own script, which is done using ‘chkconfig’ and ‘systemctl’ commands.

For example, to add ‘Apache httpd’ service on boot, run one of the following commands based on your system manager. Similarly you can add any well known service as required at startup.

For Sys V init system:

For systemd system:

Sometimes you may need to add a custom script or command or service at startup/boot. If so, how do you do it?

In this article, let’s understand how to use the following three methods to implement it:

  • Using /etc/rc.d/rc.local file
  • Using crontab file
  • systemd service unit

Method-1: Using /etc/rc.d/rc.local file

The “/etc/rc.local” file is traditionally executed after all normal computer services have been started at the end of the process of switching to a multiuser runlevel.

This method also works on the systemd system, and you need to add the location of your script to the ‘/etc/rc.d/rc.local’ file to run on boot.

Make sure the file has executable permission to run.

To understand this in detail, we will create a simple script as shown below, but you can create any script as needed:

Once the script is ready, set the executable permission as shown below:

Finally add the script to the bottom of the file:

Restart your system to verify this:

Method-2: Using crontab file

cron executes scheduled jobs automatically in the backend at a specific time.

This can be easily accomplished using a special string called «@reboot» with cron job.

@reboot is a special string and allows the user to run any command or script at startup (boot time).

This example runs the ‘/opt/scripts/run-script-on-boot.sh’ file on the system restart.

We will use the same script which was created in the above example. To do so, just add the following entry in the crontab file:

Restart your system to check this:

Method-3: Using systemd service unit

This method only works on the systemd system and it is very straightforward.

To do so, you need to create a systemd startup script and place it in the “/etc/systemd/system/” directory.

This is our sample systemd startup unit script:

Once you place the unit script in the systemd location, run the following command to update the systemd configuration files and enable the service:

Restart your system to check this:

Bonus Tips:

If you want to run a script in the background, you need to add the trailing ampersand «&» symbol.

If you want to run the command as a different user, use the following format:

Читайте также:  Windows error ошибка исполнения функции при установке 1с

Conclusion

This tutorial briefly discusses how to add a custom script or command or service at startup.

Feel free to leave a comment if you have any questions.

Источник

Ubuntu — How to run a terminal command on boot?

I want to execute a command when Ubuntu boots. The commands in particular are:

I want the computer to shut down as soon as it starts for testing purposes. I plan on switching the computer back on with a bit of circuitry I am testing.

How can I execute this command automatically at system start up?

3 Answers 3

Place it in /etc/rc.local . (It is run as root, so sudo is not needed there.)

Also, you may be interested to read additional info about runlevels: http://en.wikipedia.org/wiki/Runlevel

There are different ways to automatically run commands:

The upstart system will execute all scripts form which it finds a configuration in directory /etc/init . These scripts will run during system startup (or in response to certain events, e.g., a shutdown request) and so are the place to run commands that do not interact with the user; all servers are started using this mechanism. You can find a readable introduction to at: http://upstart.ubuntu.com/getting-started.html the man pages man 5 init and man 8 init give you the full details.

A shell script named .gnomerc in your home directory is automatically sourced each time you log in to a GNOME session. You can put arbitrary commands in there; environment variables that you set in this script will be seen by any program that you run in your session. Note that the session does not start until the .gnomerc script is finished; therefore, if you want to autostart some long-running program, you need to append & to the program invocation, in order to detach it from the running shell.

The menu option System -> Preferences -> Startup Applications allows you to define what applications should be started when your graphical session starts (Ubuntu predefines quite some), and add or remove them to your taste. This has almost the same purpose and scope of the .gnomerc script, except you don’t need to know sh syntax (but neither can you use any sh programming construct).

Источник

How do I run a single command at startup using systemd?

I’d like to startup an Apache Spark cluster after boot using the following command:

Then run this command when the system prepares to reboot/shutdown:

How can I get started? Is there a basic template I can build on?

I’ve tried to use an extremely simple (file: /lib/systemd/system/spark.service ):

Which doesn’t work.

2 Answers 2

Your .service file should look like this:

Now, take a few more steps to enable and use the .service file:

Place it in /etc/systemd/system folder with say a name of myfirst.service

Make sure that your script executable with:

Enable it to run at boot:

Notes

You don’t need to launch Spark with sudo in your service, as the default service user is already root.

Look at the links below for more systemd options.

Moreover

Now what we have above is just rudimentary, here is a complete setup for spark:

To setup the service:

Further reading

Please read through the following links. Spark is a complex setup, so you should understand how it integrates with Ubuntu’s init service.

Читайте также:  Linux jq set value

Copy-paste this into a terminal (as root) to create /root/boot.sh and run it on boot:

To modify the parameters, for example to use a different $bootscript , just set that variable manually and skip that line when copying the commands.

Источник

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

I have a script in a folder:

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:

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

with content like the following

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.

Make it executable.

Configure the init system to run this script at startup.

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.

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.

Источник

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