我有一台Azure的服务器,上面跑了一个Python的服务,但这个服务器经常被墙,我重启之后还要手动开一下这个服务。

最近在网上看到了个方法,好像还真的可以,而且很简单:

把启动程序的命令添加到/etc/rc.d/rc.local文件中。

首先要切到root用户,然后vim /etc/rc.d/rc.local

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
/usr/bin/python3 /home/xxx/lhh.py

比如我要运行/home/xxx/lhh.py这个脚本,我就加了上面的最后一行。

加进去之前,最好试试root用户能不能运行这个脚本,因为有些包在所有用户间不是通用的。

参考链接:https://blogtt.readthedocs.io/en/latest/linux/chkconfig/chkconfig-centos.html