Friday, November 15, 2024
HomeTutorialsHow to Auto Execute Linux Startup Scripts and Commands

How to 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.

@reboot /path/to/test.sh

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.

$ crontab -e

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.

$ sudo chmod u+x test.sh

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

$ sudo cp test.sh /etc/init/d

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.

$ sudo mv test.sh S70test.sh

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.

$ nano test.conf
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.

Mehedi Hasan
Mehedi Hasan
Mehedi Hasan is a passionate enthusiast for technology. He admires all things tech and loves to help others understand the fundamentals of Linux, servers, networking, and computer security in an understandable way without overwhelming beginners. His articles are carefully crafted with this goal in mind - making complex topics more accessible.

You May Like It!

Trending Now