linux 开机自启
方式一:
编写脚本,
vi /etc/rc.local
#!/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
方式二:
编辑My.service ,内容如下,里面的路径根据自己实际情况修改
[Unit] Description=My Server After=syslog.target After=network.target After=mysql.servic [Service] Restart=on-failure ExecStart=/root/server.sh start ExecRestart=/root/server.sh restart ExecStop=/root/server.sh stop PrivateTmp=true [Install] WantedBy=multi-user.target
server.sh脚本是服务的内容
#! /bin/bash curlPath=$(readlink -f "$(dirname "$0")") case "$1" in start) $curlPath/esauto.sh start echo "Biz start" ;; restart) $curlPath/esauto.sh restart echo "Biz restart" ;; stop) $curlPath/esauto.sh stop echo "Biz stop" ;; *) echo "start|stop|restart" ;; esac exit $?
这里是举例的es 启动
然后在将My.service放到
/usr/lib/systemd/system 下面
开启服务
systemctl start My.service
开启开机自启
systemctl enable My.service