Linux常用自启动
Mysql自启动:
[root@localhost /]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld #将服务文件拷贝到init.d下,并重命名为mysqld [root@localhost /]# chmod +x /etc/init.d/mysqld #赋予可执行权限 [root@localhost /]# chkconfig --add mysqld #添加服务 [root@localhost /]# chkconfig --list #显示服务列表
Nginx自启动:
[root@localhost /]# vi /etc/init.d/nginx #init.d目录下添加nginx文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!/bin/bash # nginx Startup script for the Nginx HTTP Server # it is v.0.0.2 version. # chkconfig: - 85 15 # description: Nginx is a high-performance web and proxy server. # It has a lot of features, but it's not for everyone. # processname: nginx # pidfile: /var/run/nginx.pid # config: /usr/local/nginx/conf/nginx.conf nginxd=/home/nginx/sbin/nginx nginx_config=/home/nginx/conf/nginx.conf nginx_pid=/home/nginx/logs/nginx.pid RETVAL=0 prog="nginx" # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ ${NETWORKING} = "no" ] && exit 0 [ -x $nginxd ] || exit 0 # Start nginx daemons functions. start() { if [ -e $nginx_pid ];then echo "nginx already running...." exit 1 fi echo -n $"Starting $prog: " daemon $nginxd -c ${nginx_config} RETVAL=$? echo [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx return $RETVAL } # Stop nginx daemons functions. stop() { echo -n $"Stopping $prog: " killproc $nginxd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid } # reload nginx service functions. reload() { echo -n $"Reloading $prog: " #kill -HUP `cat ${nginx_pid}` killproc $nginxd -HUP RETVAL=$? echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) stop start ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|reload|status|help}" exit 1 esac exit $RETVAL
标记部分,需要将路径改为自己机器的相应路径。接着设置文件访问权限。
a+x参数表示 ========> all user can execute 所有用户可执行
[root@localhost /]# chmod a+x /etc/init.d/nginx
最后将nginx加入到rc.local文件中,这样开机的时候nginx就默认启动了。
打开编辑rc.local文件 [root@localhost /]# vi /etc/rc.local 添加 /etc/init.d/nginx start 保存并退出。下次重启就会生效,实现nginx的自启动。
上面的设置不行,可以直接在rc.local中添加
编辑rc.local文件 [root@localhost /]# vi /etc/rc.local 添加nginx中启动文件安装路径到rc.local中 /usr/local/nginx/sbin/nginx 设置执行权限 [root@localhost /]# chmod 755 rc.local
Redis自启动:
将redis服务脚本redis_init_script移动到init.d目录下。ps: init.d目录---在系统启动的时候某些指定脚本将被执行 。 [root@localhost /]# mv /home/redis-3.2.8/utils/redis_init_script /etc/init.d 将服务脚本改名为redis并修改配置信息 [root@localhost /]# mv redis_init_script redis [root@localhost /]# vim redis 复制一份redis.conf文件到etc目录下面 [root@localhost /]# cp /home/redis/redis.conf /etc/
将服务脚本改名为redis并修改配置信息: (#内容也很重要)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!/bin/bash # chkconfig: 2345 90 10 # description: Redis is a persistent key-value database REDISPORT=6379 EXEC=/home/redis/bin/redis-server CLIEXEC=/home/redis/bin/redis-cli PIDFILE=/var/run/redis.pid CONF="/etc/redis.conf" case "$1" in start) if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed" else echo "Starting Redis server..." $EXEC $CONF fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." $CLIEXEC -p $REDISPORT shutdown while [ -x /proc/${PID} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; restart) "$0" stop sleep 3 "$0" start ;; *) echo "Please use start or stop as first argument" ;; esac
增加文件执行权限 # chmod +x /etc/init.d/redis 加入服务,开机自启 # chkconfig --add redis 查看开机自启动项 # chkconfig --list 设置开机自启(关闭是off) # chkconfig redis on 启动: # service redis start # ps -ef|grep redis 停止: # service redis stop
Jar包项目自启动:
在/etc/systemd/system目录下面新建一个启动文件 ,名称以service结尾,我这里的服务名称为config.service。操作如下:
[root@localhost /]# vi /etc/systemd/system/config.service
[Unit] Description=config service After=syslog.target [Service] Type=simple ExecStart=/home/java/bin/java -jar -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -Xms1024m -Xmx1024m -Xmn256m -Xss256k -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails /mnt/admin-1.0.jar [Install] WantedBy=multi-user.target
当设置完成后就可以通过systemctl命令进行启动了。 如设置为开机启动命令: [root@localhost /]# systemctl enable config.service 启动服务命令 [root@localhost /]# systemctl start config.service 停止服务命令 [root@localhost /]# systemctl stop config.service 重新启动命令 [root@localhost /]# systemctl restart config.service 查看启动日志 [root@localhost /]# journalctl -u config.service
实时查看日志
[root@localhost /]# journalctl -u config.service -f
查看日期日志
[root@localhost /]# journalctl -u config.service --since 2020-07-08
来源:
https://blog.csdn.net/tianlong1569/article/details/100539200?utm_medium=distribute.pc_relevant.none-task-blog-baidujs-1 https://www.jianshu.com/p/276d59cbc529 https://blog.csdn.net/qq_42810276/article/details/81296012
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步