nginx 开机自启动
- 先创建开机自启脚本(nginx.service)
cd /etc/systemd/system
vi nginx.service
- nginx.service内容
# 仅修改 /usr/local/nginx/sbin/nginx 这个路径即可(修改为你的nginx路径)
[Unit]
Description=nginx service
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
3. 设置文件权限
chmod 755 nginx.service
4. 设置开机自启动
systemctl start nginx # nginx启动
systemctl stop nginx # nginx停止
systemctl enable nginx.service 开机自启动
5. 直接重启服务器即可(nginx就自动重启了) reboot
6. 备注
# 启动nginx服务
systemctl start nginx.service
# 重新启动nginx服务
systemctl restart nginx.service
# 查看nginx服务当前状态
systemctl status nginx.service
# 停止开机自启动
systemctl disable nginx.service
mysql 开机自启动(这次没有用systemd,后面会更新~)
- 我们设置开机启动需要将mysql.server 文件复制到 /etc/rc.d/init.d/ 目录下mysql 文件 我们的mysql.server 文件一般都在安装的根目录下的 support-files 目录下
cp /usr/local/mysql/mysql/support-files/mysql.server /etc/rc.d/init.d/mysql
- 复制成功后我们需要给赋予权限
chmod +x /etc/init.d/mysql
- 添加为服务
chkconfig --add mysql
- 查看服务列表 (看到3、4、5状态为开或者为 on 则表示成功。 如果是 关或者 off 则执行一下第二条)
chkconfig --list
chkconfig --level 345 mysqld on
- 重启计算机:reboot
再次查看服务列表或者查看3306端口号
redis开机自启动
- 在/etc/system/system位置下创建redis.service文件
vi /etc/systemd/system/redis.service
- 添加文件内容(具体文件位置以自己的实际情况为准)
[Unit]
Description=redis-server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
- 测试
#关闭redis-server
systemctl stop redis.service
#开启redis-server,如果服务是开启状态,使用此命令会启动失败。
systemctl start redis.service
- 设置开机自启
#设置开机自启
systemctl enable redis.service #注意后面不能跟空格
#查看服务运行状态
systemctl status redis.service
- 其他命令
#启动redis服务
systemctl start redis.service
#设置开机自启动
systemctl enable redis.service
#停止开机自启动
systemctl disable redis.service
#查看服务当前状态
systemctl status redis.service
#重新启动服务
systemctl restart redis.service
#查看所有已启动的服务
systemctl list-units --type=service
fastdfs 开机自启动
(坑:如果/etc/init.d中存在fdfs_trackerd,则不要覆盖直接执行第三步)
fdfs_tracker
1. cp /usr/bin/fdfs_trackerd /etc/init.d/
2. chkconfig –add fdfs_trackerd
3. chkconfig fdfs_trackerd on
fdfs_storafed
1. cp /usr/bin/fdfs_storaged /etc/init.d/
2. chkconfig –add fdfs_storaged
3. chkconfig fdfs_storaged on
tomcat 开机自启动
- 在/etc/system/system位置下创建tomcat.service文件
vi /etc/systemd/system/tomcat.service
- 填写文件内容
[Unit]
Description=tomcat service
After=network.target
[Service]
Type=oneshot
ExecStart=/home/wangkun/app/server/tomcat-9-8080/bin/startup.sh
ExecStop=/home/wangkun/app/server/tomcat-9-8080/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
- 测试
#关闭tomcat
systemctl stop tomcat
#开启tomcat,如果服务是开启状态,使用此命令会启动失败。
systemctl start tomcat
- 设置开机自启
#设置开机自启
systemctl enable tomcat#注意后面不能跟空格
#查看服务运行状态
systemctl status tomcat
- 其他命令
#启动redis服务
systemctl start tomcat
#设置开机自启动
systemctl enable tomcat
#停止开机自启动
systemctl disable tomcat
#查看服务当前状态
systemctl status tomcat
#重新启动服务
systemctl restart tomcat
#查看所有已启动的服务
systemctl list-units --type=service