Debian Series: 3 Common Ways to Set Commands or Scripts to Run at Startup
本文最后更新于 536 天前,其中的信息可能已经有所发展或是发生改变,如有失效可到评论区留言。
Article Abstract
Common methods for automatically running scripts or commands at startup in Debian systems include: defining startup items via systemd services, which requires writing scripts containing start/stop logic and configuring unit files; using the rc.local method, which requires enabling the corresponding service first and manually creating executable scripts; and quickly configuring via the process daemon function of Linux panels (such as aaPanel/Baota). Taking the tailscale DERP relay server as an example, the first two methods are suitable for scenarios requiring fine-grained control, while panel tools simplify the operation process. Different solutions adapt to different needs. If you need to manage multiple background processes, you can combine them with the screen tool in rc.local, or directly call the startup script.
Qwen3-14B · 2026-06-18

1 Introduction

When using Linux systems, a common requirement we often encounter is the need to run commands (or scripts) at startup. Taking running the Tailscale DERP relay server on Debian 11 at startup as an example, this article summarizes the 3 most common ways to run commands (or scripts) at startup.

2 Adding as a service

This is the recommended way in Debian to add startup scripts, registering startup items as services, which also facilitates management and operations.

Let's look at the format for defining a service file, taking the service I configured for the DERP service (see:Debian Series: A Foolproof Guide to Setting Up a Tailscale DERP Server (Relay Server)) 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

Description=derper.serviceAfter=network.target和[Service]Type=forkingExecStart=/usr/local/go/bin/runderperExecStop=/usr/local/go/bin/stopderper[Install]WantedBy=multi-user.targetBecause after registering as a service, you can useExecStop=/usr/local/go/bin/stopderpersystemctl startsystemctl stop

and other commands for management, so you need to define the corresponding behaviors for start and stop in the service file in advance. Generally, we can write 2 scripts to correspond to them respectively. In the DERP example,

vim /usr/local/go/bin/runderper

the script after it

#!/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

corresponds to start;

ExecStop=

vim /usr/local/go/bin/stopderper

the script after it

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

First, create the runderper script:

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

Paste the following content, save, and exit:

vim /etc/systemd/system/derper.service

the script after it

[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

cd /root/go/bin

systemctl start derper
systemctl enable derper

3 rc.local method

Please modify the path of the derper program, domain name, port, certificate storage path, and app.pid path according to your actual situation.

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

vim /usr/local/go/bin/stopderper

image.png

#!/bin/sh

systemctl status rc-local

image.png

rm -rf app.pid

cp /lib/systemd/system/rc-local.service /etc/systemd/system/rc-local.service

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

vim /etc/systemd/system/rc-local.service

Register the service file:vim /etc/systemd/system/derper.serviceSet the service to start on boot:

[Install]
WantedBy=multi-user.target

As shown in the figure below:

image.png

systemctl enable derper

vim /etc/rc.local

Then paste the following content, 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

#

chmod +x /etc/rc.local

#

systemctl enable rc-local   #设置服务开机启动

# Make sure that the script will "exit 0" on success or any other

image.png

# value on error.

#

systemctl status rc-local.service 

image.png

# bits.

#

# By default this script does nothing.Debian Series: A Foolproof Guide to Setting Up a Tailscale DERP Server (Relay Server)exit 0

Grant execution permissions to the rc.local file:
chmod +x /etc/rc.local

screen_name1="derper"screen -dmS"screen_name1cmd1=""sleep 10;/usr/local/go/bin/runderper";screen -x -S 'screen_name1 -p 0 -X stuff 'cmd1"

Start the rc-local service:TrinityCore Series: World of Warcraft TrinityCore 3.3.5 (24011) Server Setup Guide for Dummies)。

systemctl enable rc-local # Set the service to start on boot
The following warning will pop up:

bash /usr/local/go/bin/runderper

4 Using the Process Daemon Manager of a Linux panel

Run the command again to check the service status:

image.png

systemctl status rc-local.service
image.png

image.png

You can see that the service status is now normal.

View Related Categories · 3 Matches
📌 Content Structure Prompt:
This content is part of the following knowledge map, where you can view the complete content path: Blog Knowledge Map。
📎 Related Articles
Share this article
The blog content is original, please indicate the source when reposting! The RSS address of the blog is:https://blog.tangwudi.com/feed, welcome to subscribe; if needed, you can join theTelegram Groupto discuss questions together.
No Comments

Send Comment Edit Comment


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

👋 Welcome to “Wudi's Personal Blog”

Here, long-term exploration is mainly carried out around the following directions:

🧱 Personal Digital Infrastructure and Blog System Construction
☁️ Cloudflare and Network Architecture Practice
🧠 AI and Knowledge System Exploration
🛡️ Network Security and Access Optimization
🎵 Music and Sound Cognition
👁️ Cognitive Perspectives and Worldviews