写一段linux开机启动服务的脚本
时间: 2024-04-25 17:09:43 浏览: 38
#!/bin/bash
# Author: yourname
# Date: 2021-01-01
# Description: This script starts myservice on system boot
# Define the service name
SERVICE_NAME="myservice"
# Define the path to the service executable
SERVICE_PATH="/path/to/myservice"
# Define the command to start the service
SERVICE_CMD="$SERVICE_PATH start"
# Define the command to stop the service
SERVICE_STOP_CMD="$SERVICE_PATH stop"
# Define the function to start the service
function start_service() {
echo "Starting $SERVICE_NAME..."
$SERVICE_CMD
}
# Define the function to stop the service
function stop_service() {
echo "Stopping $SERVICE_NAME..."
$SERVICE_STOP_CMD
}
# Check if the service is already running
if pgrep -x "$SERVICE_NAME" >/dev/null; then
echo "$SERVICE_NAME is already running."
else
# Start the service
start_service
fi
# Add the service to the system startup
if [[ ! -f "/etc/rc.local" ]]; then
touch /etc/rc.local
fi
if ! grep -q "$SERVICE_CMD" /etc/rc.local; then
echo "$SERVICE_CMD" >> /etc/rc.local
fi
# Define the function to remove the service from the system startup
function remove_service_from_startup() {
sed -i "/$SERVICE_CMD/d" /etc/rc.local
}
# Check if the script is called with the argument "stop"
if [[ $1 == "stop" ]]; then
# Stop the service
stop_service
# Remove the service from the system startup
remove_service_from_startup
fi
exit 0
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)