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

#!/bin/sh
systemctl status rc-local

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:

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

# value on error.
#
systemctl status rc-local.service

# 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:

systemctl status rc-local.service


You can see that the service status is now normal.