- mysql自启动:
-
将服务文件拷贝到init.d下,并重命名为mysqld # cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld 赋予可执行权限 # chmod +x /etc/init.d/mysqld 添加服务 # chkconfig --add mysqld 显示服务列表 # chkconfig --list
- nginx自启动:
-
init.d目录下添加nginx文件 # vi /etc/init.d/nginx
-
#!/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 所有用户可执行 # chmod a+x /etc/init.d/nginx
- 最后将nginx加入到rc.local文件中,这样开机的时候nginx就默认启动了。
-
打开编辑rc.local文件 # vi /etc/rc.local 添加 /etc/init.d/nginx start 保存并退出 下次重启就会生效,实现nginx的自启动。
- 上面的设置不行,可以直接在rc.local中添加
-
编辑rc.local文件 # vi /etc/rc.local 添加nginx中启动文件安装路径到rc.local中 # /usr/local/nginx/sbin/nginx 设置执行权限 # chmod 755 rc.local
- redis自启动:
-
将redis服务脚本redis_init_script移动到init.d目录下。ps: init.d目录---在系统启动的时候某些指定脚本将被执行 。 # mv /home/redis-3.2.8/utils/redis_init_script /etc/init.d 将服务脚本改名为redis并修改配置信息 # mv redis_init_script redis # vim redis 复制一份redis.conf文件到etc目录下面 # cp /home/redis/redis.conf /etc/
将服务脚本改名为redis并修改配置信息: (#内容也很重要)
-
#!/bin/bash
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value databaseREDISPORT=6379
EXEC=/home/redis/bin/redis-server
CLIEXEC=/home/redis/bin/redis-cliPIDFILE=/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。操作如下: # 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命令进行启动了。 如设置为开机启动命令: # systemctl enable config.service 启动服务命令 # systemctl start config.service 停止服务命令 # systemctl stop config.service 重新启动命令 # systemctl restart config.service 查看启动日志 # journalctl -u config.service
实时查看日志
# journalctl -u config.service -f
查看日期日志
#journalctl -u config.service --since 2020-07-08