Hello, I’m Liang Xu.

At work, we often have a requirement to automatically start a script or service after the system is started. There are many ways to set up boot on Windows, but how do you do it on Linux?

Linux can also be set to boot, but we may need to type some commands (there may also be UI Settings, but I am not familiar with them, I am more playing commands). Here are three simple but feasible startup Settings.

Method 1: Modify the /etc/rc.d/rc.local file

The /etc/rc.d/rc.local file will be run after all Linux services have been started. So if you want your own script to be run after boot, you can add your own script path to this file.

However, first you need to make sure you have permission to run the file.

$ chmod +x /etc/rc.d/rc.localCopy the code

To demonstrate, we created a script that, when executed, writes a file with specific information to the home directory.

$ vim auto_run_script.sh #! /bin/bash date >> /home/alvin/output.txt hostname >> /home/alvin/output.txtCopy the code

Save and exit, and then grant it executable permission:

$ chmod +x auto_run_script.shCopy the code

Then we add the script to the last line of /etc/rc.d/rc.local:

$ vim /etc/rc.d/rc.local

/home/alvin/auto_run_script.shCopy the code

Now, we can try it out. Just restart the system:

$ sudo rebootCopy the code

After the restart, you should see the results of the script execution in your home directory.

Method 2: Use crontab

As you know, crontab is a scheduled task in Linux that automatically triggers certain scripts to run when the scheduled time is reached.

We can set our own scheduled task times and then write the corresponding scripts. However, there is a particular task, @reboot, which can be taken literally, which is to automatically run a script after the system restarts.

What script will it run? How do we set up this script? We can set this with crontab -e.

$ crontab -e

@reboot /home/alvin/auto_run_script.shCopy the code

Then, just restart. The result is similar to the above.

Method 3: Use the Systemd service

The two methods described above can be used on any Linux system. This method applies only to systemd systems. How to tell whether the system is systemd? This is as simple as running the ps aux command to check whether the process with pid 1 is systemd.

To do this, we need to create a systemd startup service and place it in the /etc/systemd/system/ directory.

The systemd startup service we created is as follows. Note that the suffix is.service, not.sh.

$ vim auto_run_script.service

[Unit]
Description=Run a Custom Script at Startup
After=default.target

[Service]
ExecStart=/home/alvin/auto_run_script.sh

[Install]
WantedBy=default.targetCopy the code

As you can see from the contents of the service, we will eventually call the /home/alvin/auto_run_script.sh script.

We then place the script in /etc/systemd/systerm/ and run the following two commands to update the systemd configuration file and start the service.

$ systemctl daemon-reload
$ systemctl enable auto_run_script.serviceCopy the code

When everything is ready, we can restart the system.

$ rebootCopy the code

Finally, recently, many friends asked me for Linux learning roadmap, so I stayed up for a month in my spare time according to my own experience, and sorted out an e-book. Whether you are interviewing or self-improvement, I believe will help you! The directory is as follows:

Free to everyone, just ask everyone to point to me!

Ebook | Linux development learning roadmap

Also hope to have a small partner can join me, do this e-book more perfect!

Have a harvest? Hope the old iron people come to a triple whammy, give more people to see this article

Recommended reading:

  • Dry goods | programmers advanced architect necessary resources free of charge
  • Artifact | support resource site search