我有一个通过 rc.local 启动的 python 守护进程。这个具有相同权限的相同脚本安装在我拥有的其他几个 Ubuntu 机器上。它在这些安装上运行没有问题。也就是说,重启盒子后,守护进程正在运行。
但是,通过这个特定的安装,当我登录并检查进程是否存在时,守护进程并未运行。系统之间的 rc.local 文件是相同的(或至少足够接近):
localaccount@sosms:~$ cat /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
python /var/www/myDaemon/Main.py > /var/log/somelog.txt
exit 0
权限是:
localaccount@sosms:~$ ls -la /etc/rc.local
-rwxr-xr-x 1 localaccount localaccount 370 Jun 3 11:04 rc.local
我使用此测试 rc.local 测试了 rc.local 进程是否正在执行:
localaccount@sosms:/var/log/sosmsd$ cat /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
echo "test" > /home/localaccount/test1
/usr/bin/python /var/www/sosms/sosmsd/Main.py > /var/log/sosmsd/log/log
echo "test" > /home/localaccount/test2
exit 0
localaccount@sosms:/var/log/sosmsd$
并且只有第一个测试文件(test1)在重启盒子后被创建。我猜这意味着 python 行引起了某种问题,但我在 /var/log/sosmsd/log/log 中没有输出:
localaccount@sosms:~$ ls
test1
更新:
然后我听从了 larsks 的建议,并确定我在启动 python 脚本时遇到了这个错误:
mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
这是否意味着 rc.local 在 MySQL 有机会被初始化之前被执行?我从这里去哪里?