nginx自启动

方法一:

1、建立启动文件

vim /usr/lib/systemd/system/nginx.service
[Unit]Description=nginx - high performance web serverAfter=network.target remote-fs.target nss-lookup.target [Service]Type=forkingExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confExecReload=/usr/local/nginx/sbin/nginx -s reloadExecStop=/usr/local/nginx/sbin/nginx -s stop [Install]WantedBy=multi-user.target

2、检查常用命令

sudo systemctl daemon-reload 重新加载这个文件sudosystemctl disable nginx.service 关闭开机自启sudosystemctl enable nginx.service 开启开机自启sudosystemctl status nginx.service 查看状态sudosystemctl restart nginx.service 重启服务sudosystemctl list-units --type=service 查看所有服务

方法二(ubentu已成功)

1、建立启动文件

在/etc/init.d/下创建nginx文件,sudo vim /etc/init.d/nginx

#! /bin/bash### BEGIN INIT INFO# Provides:XXX# Required-Start:# Required-Stop:# Default-Start: 2 3 4 5# Default-Stop:0 1 6# Short-Description: Start XXX daemon at boot time# Description: Start XXX daemon at boot time### END INIT INFO#chkconfig:35 85 15DAEMON=/usr/local/nginx/sbinPID=/usr/local/nginx/logs/nginx.pidcase "$1" instart)echo "Starting nginx daemon..."$DAEMON && echo "SUCCESS"#开启nginx;;stop)echo "Stopping nginx daemon..."$DAEMON -s quit && echo "SUCCESS"#从容的停止nginx;;reload)echo "Reloading nginx daemon..."$DAEMON -s reload && echo "SUCCESS"#平滑重启nginx;;restart)echo "Restarting nginx daemon..."if [ ! -f "$PID" ]; then ps -ef | grep nginx | awk 'NR<3{print "kill -9 "$2}' | sh sleep 3 $DAEMON && echo "SUCCESS"#开启nginxelse $DAEMON -s quit#从容的停止nginx sleep 3$DAEMON && echo "SUCCESS"#开启nginxfi;;status)if [ ! -f "$PID" ]; then#因为nginx启动后会生成进程文件nginx.pid,这里通过判断进程文件是否存在,判断nginx是否启动 echo "Nginx is not running..."else echo "Nginx is running..."fi;;*)echo "Usage:service nginx (start|stop|restart|reload|status)"exit 2;;esac

2、开权限和更新文件

#设置服务脚本有执行权限sudo chmod +x /etc/init.d/nginx#注册服务cd /etc/init.d/sudo update-rc.d nginx defaults

重启服务器看是否自动启动。

sudosystemctl status nginx.service 查看状态

mysql自启动

systemctl enable mysql.service 直接mysql自启动

如果不得参照以下操作:

在/lib/systemd/system/mysql.service建立文件。

sudo vim /lib/systemd/system/mysql.service
# MySQL systemd service file[Unit]Description=MySQL Community ServerAfter=network.target[Install]WantedBy=multi-user.target[Service]Type=notifyUser=mysqlGroup=mysqlPIDFile=/run/mysqld/mysqld.pidPermissionsStartOnly=trueExecStartPre=/usr/share/mysql/mysql-systemd-start preExecStart=/usr/sbin/mysqldTimeoutSec=infinityRestart=on-failureRuntimeDirectory=mysqldRuntimeDirectoryMode=755LimitNOFILE=10000# Set enviroment variable MYSQLD_PARENT_PID. This is required for restart.Environment=MYSQLD_PARENT_PID=1

自启动

# 重新加载服务systemctl daemon-reload# 设置开机自启动systemctl enable mysql.service

重启看是否已经启动。

python后端程序自启动

常用指令

sudo chmod +x /root/xcxpy.sh 增加权限sudo systemctl daemon-reload 更新文件sudo systemctl enable xxx.service 开启自启动sudo systemctl start xxx.service开启文件ps aux | grep python3 kill -9 pip

1、给脚本增加权限

chmod +x xxx.py

2、在/etc/systemd/system/建立自启动文件xxx.service

[Unit]Description=Auto Run Python ScriptAfter=network.target[Service]ExecStart=/usr/bin/python3 /root/xxx.py(py的绝对地址)WorkingDirectory=/root (文件目录)StandardOutput=inheritStandardError=inheritRestart=alwaysUser=username (设置用户名)[Install]WantedBy=multi-user.target

3、启动服务

sudo systemctl enable xxx.service

4、重启查看程序是否启动

ps aux | grep python3