### 开机启动 #### 复制程序到 /opt 目录下 ``` cp /root/works/console_app/console_app /opt/app ``` #### 创建启动脚本 * 切换目录, 并创建文件 ``` cd /opt vim run.sh ``` * run.sh 文件内容 ``` #! /bin/bash cd /opt/ ./app ``` #### 创建服务 * 切换目录, 并创建文件 ``` cd /etc/systemd/system vim console_app.service ``` * console_app.service 文件内容 ``` [Unit] Description=console app server [Service] Type=simple #ExecStart=/home/rpdzkj/works/console_app/console_app ExecStart=/opt/run.sh Restart=on-failure RestartSec=5s # 30秒后在启动服务 TimeoutStartSec=infinity ExecStartPre=/bin/sleep 30 [Install] WantedBy=multi-user.target ``` #### 重新加载 Systemd ``` sudo systemctl daemon-reload ``` #### 启动服务 ``` sudo systemctl start console_app ``` #### 开机启动 ``` sudo systemctl enable console_app ```