Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add services when starting in Linux

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to add services when starting in Linux". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Sometimes you need to add custom scripts, commands, or services at startup. What should you do? You can do this in three ways.

In this article, we will show you how to use these methods with examples.

Method 1: how to use the / etc/rc.d/rc.local file to run scripts or commands at restart or startup

Traditionally, the / etc/rc.local file is executed after all normal computer services are started at the end of the process of switching to the multi-user runlevel.

This method is also applicable to systemd systems.

You need to add your script location to the / etc/rc.d/rc.local file to run at startup.

Make sure the file has permission to run:

# chmod + x / etc/rc.d/rc.local

As a demonstration, we will create a simple example script. You can create any script you need.

# vi / opt/scripts/run-script-on-boot.sh #! / bin/bashdate > / root/on-boot-output.txthostname > / root/on-boot-output.txt

After the script completes, set the executable permissions:

# chmod + x / opt/scripts/run-script-on-boot.sh

Finally, add the script to the bottom of the file:

# vi / etc/rc.d/rc.local / opt/scripts/run-script-on-boot.sh

Restart the system to check:

# reboot

Method 2: how to use crontab to execute commands or scripts on restart or startup

Cron automatically executes scheduled jobs in the background at a specific time. You can do this with the special string @ reboot in the cron task. Reboot is a special string that allows the user to run any command or script at startup.

This example runs the / opt/scripts/run-script-on-boot.sh file when the system is restarted. We will use the same script as above.

To do this, simply add the following entry to the crontab file:

# crontab-e @ reboot / opt/scripts/run-script-on-boot.sh

Restart the system to check:

# reboot

Method 3: how to use the systemd service unit to run commands or scripts at restart or startup

This method applies only to systemd systems. The method is very simple.

We will demonstrate using the same script above.

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

This is our example systemd startup unit script:

# vi sample-on-boot-script.service [Unit] Description=Run a Custom Script at StartupAfter=default.target [Service] ExecStart=/opt/scripts/run-script-on-boot.sh [Install] WantedBy=default.target

After placing the unit script in the systemd location, run the following command to update the systemd configuration file and enable the service:

# systemctl daemon-reload# systemctl enable sample-on-boot-script.service

Restart the system to check:

# reboot

Extra hint

If you want to run the script in the background, you need to add the & symbol at the end

/ Path/To/My_Script &

If you want to run commands as different users, use the following format:

Su-$USER-c / Path/To/My_Script "how to add services when starting in Linux" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report