3 common ways to set up commands or scripts for Debian series to start at boot
This article was last updated 133 days ago. The information in it may have developed or changed. If it is invalid, please leave a message in the comment section.

When we use the Linux system, we often encounter the need to run commands (or scripts) at startup. This article takes the startup of the tailscale relay server DERP on Debian 11 as an example to summarize the three most commonly used ways to run commands (or scripts) at startup.

# is added as a service
This is the Debian-recommended way to add a startup script. Registering the startup item as a service also facilitates management and operation.

Let's look at the format of defining the service file, using my configuration of the DERP service (see:Build tailscale DERP server (relay server) on cloud host) service as an example:

[Unit]
Description=derper.service
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/go/bin/runderper
ExecStop=/usr/local/go/bin/stopderper

[Install]
WantedBy=multi-user.target

Because after registering as a service, you can usesystemctl startandsystemctl stopSo the corresponding behaviors of start and stop need to be defined in advance in the service file. Generally, we can write two scripts to correspond to each other. In the example of DERP,ExecStart=The following script/usr/local/go/bin/runderperIt corresponds to start;ExecStop=The following script/usr/local/go/bin/stopderperCorresponding to stop. Start must be present, but stop is not necessary. If you do not need it, you can leave it undefined.

First create the runderper script:

vim /usr/local/go/bin/runderper

Paste the following content and save and exit:

#!/bin/sh cd /root/go/bin nohup ./derper -hostname=ab.cd.ef -c=derper.conf -a=:45678 -http-port -1 -certdir=/usr/local/ cert -certmode=manual -verify-clients -stun & echo $! > /usr/local/go/bin/app.pid

The derper program path, domain name, port, certificate storage path, and app.pid path can be modified according to your actual situation.

Then create the stopderper script:

vim /usr/local/go/bin/stopderper

Paste the following content and save and exit:

#!/bin/sh kill cat app.pid
rm -rf app.pid

Grant execution permissions to these two files respectively:

chmod +x /usr/local/gopath/bin/runderper
chmod +x /usr/local/gopath/bin/stopderper

Register the service file:

vim /etc/systemd/system/derper.service

Paste the following content and save and exit:

[Unit]
Description=derper.service
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/go/bin/runderper
ExecStop=/usr/local/go/bin/stopderper

[Install]
WantedBy=multi-user.target

Set the service to start at boot:

systemctl start derper systemctl enable derper

rc.local method

Loading scripts at startup through rc.local is an old method, but it is also a simple method that many people are used to. However, after Debian 9, the system no longer has the rc.local file by default, but only retains the rc.local service. For example, run the following command:

cat /lib/systemd/system/rc-local.service

image.png

And the service is closed by default, run the following command to view:

systemctl status rc-local

image.png

Because the location of the rc.local file has been defined in the rc-local.servive file, we only need to manually create the rc.local file first, as follows:

vim /etc/rc.local

Then paste the following content and save and exit:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

exit 0

Grant execution permissions to the rc.local file:

chmod +x /etc/rc.local

Start the rc-local service:

systemctl enable -now rc-local   # service settings start at boot

The following warning will pop up:

image.png

Don't worry about this warning, because this service is only associated with one file, rc.local.

Run the command again to view the service status:

systemctl status rc-local.service 

image.png

You can see that the service status is normal.

In the future, you can add commands that need to be started at boot time to the rc.local file before exit 0. If it is a boot script, add it before exit 0 using bash xxx.sh.

Take the DERP server setting for auto-startup in another article as an example (see:Build tailscale DERP server (relay server) on cloud host), there are 2 ways:

1. Use screen:
Enter the following command before exit 0:

screen_name1="derper" screen -dmSscreen_name1 cmd1="sleep 10;/usr/local/go/bin/runderper"; screen -x -Sscreen_name1 -p 0 -X stuff "cmd1" screen -x -Sscreen_name1 -p 0 -X stuff '\n' screen -x -Sscreen_name1 -p 0 -X stuff "exit"

This method requires the installation of the screen command. The advantage of this method is that you can use the screen command to switch to the corresponding terminal. This is very useful when there are multiple programs running in the background. For example, when setting up the Trinitycore 3.5.5 World of Warcraft server, worldserver and authserver need to be started automatically at boot (see:World of Warcraft Trinitycore 3.3.5 for Dummies).

2. Run the startup script directly
Enter the following command before exit 0:

bash /usr/local/go/bin/runderper

# uses the process daemon manager of the Linux panel
I use the Baota Linux panel, and there is a free program in the software center:

image.png

Install and enter the program, click Add Daemon in the red box:
image.png

image.png

This method is the simplest, so I won’t say much about it, but the prerequisite is that you have installed Baota Linux Bread or other panels with similar functions.

C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup

The content of the blog is original. Please indicate the source when reprinting! For more blog articles, you can go toSitemapUnderstand. The RSS address of the blog is:https://blog.tangwudi.com/feed, welcome to subscribe; if necessary, you can joinTelegram GroupDiscuss the problem together.
No Comments

Send Comment Edit Comment


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠(ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ°Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
Emoticons
Emoji
Little Dinosaur
flower!
Previous
Next
       
error:
en_US
Spring Festival
hapiness