Linux starts self-starting demo

Of course you can, here's a simple example:

  1. First, you need to create a startup script, say called myservice.sh, and place it /etc/init.d/in a directory. This script needs to contain the following:
#!/bin/bash
# chkconfig: 345 99 10
# description: My service

case "$1" in
    start)
        echo "Starting my service"
        # 这里写启动服务的命令
        ;;
    stop)
        echo "Stopping my service"
        # 这里写停止服务的命令
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
        ;;
esac

exit 0
  1. Next, you need to add this script to the system service, you can use the following command:
sudo chkconfig --add myservice.sh
  1. Then, you need to set the startup level of the service, you can use the following command:
sudo chkconfig myservice.sh on

In this way, your service will automatically start when the system starts. If you need to manually start or stop the service, you can use the following commands:

sudo service myservice.sh start
sudo service myservice.sh stop
sudo service myservice.sh restart

Hope this example can help you.

Guess you like

Origin blog.csdn.net/qq_41568648/article/details/130361944