nginx自启动
方法一:
1、建立启动文件
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target
2、检查常用命令
sudo systemctl daemon-reload 重新加载这个文件
sudo systemctl disable nginx.service 关闭开机自启
sudo systemctl enable nginx.service 开启开机自启
sudo systemctl status nginx.service 查看状态
sudo systemctl restart nginx.service 重启服务
sudo systemctl 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 15
DAEMON=/usr/local/nginx/sbin
PID=/usr/local/nginx/logs/nginx.pid
case "$1" in
start)
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" #开启nginx
else
$DAEMON -s quit #从容的停止nginx
sleep 3
$DAEMON && echo "SUCCESS" #开启nginx
fi
;;
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
重启服务器看是否自动启动。
sudo systemctl 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 Server
After=network.target
[Install]
WantedBy=multi-user.target
[Service]
Type=notify
User=mysql
Group=mysql
PIDFile=/run/mysqld/mysqld.pid
PermissionsStartOnly=true
ExecStartPre=/usr/share/mysql/mysql-systemd-start pre
ExecStart=/usr/sbin/mysqld
TimeoutSec=infinity
Restart=on-failure
RuntimeDirectory=mysqld
RuntimeDirectoryMode=755
LimitNOFILE=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 Script
After=network.target
[Service]
ExecStart=/usr/bin/python3 /root/xxx.py (py的绝对地址)
WorkingDirectory=/root (文件目录)
StandardOutput=inherit
StandardError=inherit
Restart=always
User=username (设置用户名)
[Install]
WantedBy=multi-user.target
3、启动服务
sudo systemctl enable xxx.service
4、重启查看程序是否启动
ps aux | grep python3