banner
xingli

xingli

猫娘爱好者

Set frp to start automatically on boot

Setting up frp auto-start on Linux#

First, create an sh script, for example:

[Unit]

Description=frps daemon

  

[Service]

Type=simple

# Replace /root/frp_linux_arm64 with the actual installation directory of your frps

ExecStart=/opt/module/frpc/frpc -c /opt/module/frpc/frpc.ini

  

[Install]

WantedBy=multi-user.target

Creating a service file#

  1. Log in to the server and enter sudo vim /lib/systemd/system/frps.service. This will create a new file called frps.service under root/lib/systemd/system.

  2. Write the script content inside the file, which is the example I provided above. After writing, save and exit to make the service available.

  3. Start frps by running sudo systemctl start frps.

  4. Enable auto-start by running sudo systemctl enable frps.

Other settings

  • Restart the application: sudo systemctl restart frps.
  • Stop the application: sudo systemctl stop frps.
  • View the logs: sudo systemctl status frps.

Additional configuration#

After testing, I found that when restarting and starting the frp startup script on a Linux system, it fails to start and encounters a network error, preventing it from connecting to the server and resulting in an error. Manual service restart is required.

To solve this issue, you can create an sh script that pings an address and restarts the service once the ping is successful.

You can use the sleep command to pause the script execution for a certain amount of time, waiting for the network to be ready. Specifically, you can add code like the following in the script:

#!/bin/bash

# Pause script execution for 10 seconds
sleep 10

# Write the command you need to execute here, with a 10-second delay.

In the above code, the number after sleep represents the time to pause execution in seconds. You can adjust it according to your actual situation.

Additionally, you can use network commands like ping to test if the network connection is available and execute the desired command only when the network is available. For example:

#!/bin/bash

# Ping the target address every 3 seconds. Replace the target address with the IP address of your frp server.
while ! ping -c1 -w1 "target address" &>/dev/null
do
  sleep 3
done

# Execute the command when the target address is available.

In the above code, the while loop uses the ping command to attempt to ping the target address every 3 seconds until it succeeds. Once it can successfully ping the target address, it exits the loop and starts executing the command below.

#!/bin/bash

while ! ping -c1 -w1 "4x.1x8.2x0.xx" &>/dev/null
do
  sleep 3
done

systemctl start frpc.service # Replace this with the startup code for your configured service.

In the above code, the .service file defines the service configuration, ExecStart specifies the command to execute when starting the service, Restart specifies automatic restart in case of service failure, and WantedBy specifies in which system levels the service should be automatically started.

The .sh file defines the script for waiting for network connection. Here, the while loop tests the specified IP address using the ping command. If it cannot be accessed, it waits for 3 seconds and tries again. Once it can be accessed, it starts the service defined in the .service file.

Note that to make the .sh file run automatically at system startup, you need to set it as a startup script and add it to files like /etc/rc.local. For example, adding the following content to the /etc/rc.local file will automatically start the script at system startup:
/path/to/wait-for-network.sh &

In the above code, /path/to/wait-for-network.sh should be replaced with the actual path where the .sh file is stored. The & symbol is used to start the script as a background process.

Assuming your script is located at /opt/module/frpc/start.sh, you can use the following code:

bash /opt/module/frpc/start.sh &

Setting up auto-start on boot#

You can set /opt/module/frpc/start.sh as a script that starts automatically on boot by following these steps:

  1. Use the following command to edit the /etc/rc.local file:
    sudo vim /etc/rc.local

    Note that if you are using a newer system version like Ubuntu 20.04 or Debian 11, the /etc/rc.local file may be disabled. In that case, you need to create a systemd service unit.

  2. Add the following line at the end of the file:
    bash /opt/module/frpc/start.sh &

    The above command runs the /opt/module/frpc/start.sh script in the background.

  3. Save and close the /etc/rc.local file.

  4. Make sure the /opt/module/frpc/start.sh script has executable permissions. You can grant it executable permissions with the following command:
    chmod +x /opt/module/frpc/start.sh

Arch Linux system#

For Arch Linux, you need to write the startup sh script as follows:

sudo vim /lib/systemd/system/frp_start.service

[Unit]
Description=FRP service startup script

[Service]
Type=simple
ExecStart=/bin/bash /path/to/sh/script/frp_start.sh

[Install]
WantedBy=multi-user.target

After writing this system service, you can enable it by running systemctl enable frp_start.service.

Now, when your system starts up, the /opt/module/frpc/start.sh script will run automatically. If you find that it is not working as expected, make sure the script contains the correct startup and configuration commands, and check the system logs for any error messages related to the script.

Based on my testing, you need to configure both the frpc system service and the sh script to start on boot for the frpc startup script to work properly. Otherwise, it may result in errors or the service not starting.

Remember to execute the systemctl enable frps command, where the specific command depends on the service name you have set.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.