Linux 我的 rc.local 文件 (Ubuntu) 有什么问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10883589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's wrong with my rc.local file (Ubuntu)?
提问by kwikness
I have a python daemon process that gets started via rc.local. This same script, with the same permissions, is installed on a few other Ubuntu boxes I have. It runs without trouble on those installations. That is, after restarting the box, the daemon process is running.
我有一个通过 rc.local 启动的 python 守护进程。相同的脚本,具有相同的权限,安装在我拥有的其他几个 Ubuntu 机器上。它在这些安装上运行没有问题。即重启盒子后,守护进程正在运行。
With this particular installation though, the daemon process is not running by the time I log in and check for the existence of the process. The rc.local files between systems are identical (or at least close enough):
但是,使用此特定安装,当我登录并检查该进程是否存在时,守护进程并未运行。系统之间的 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
The permissions are:
权限是:
localaccount@sosms:~$ ls -la /etc/rc.local
-rwxr-xr-x 1 localaccount localaccount 370 Jun 3 11:04 rc.local
I tested if the rc.local process is getting executed by using this test 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$
And only the first test file (test1) gets created after restarting the box. I'm guessing it means that the python line is causing some kind of issue, but I get no output in /var/log/sosmsd/log/log:
并且只有第一个测试文件 (test1) 在重新启动盒子后被创建。我猜这意味着 python 行导致了某种问题,但我在 /var/log/sosmsd/log/log 中没有得到任何输出:
localaccount@sosms:~$ ls
test1
Update:
更新:
I then followed larsks' advice and determined that I was getting this error from launching the python script:
然后我遵循了 larsks 的建议,并确定我在启动 python 脚本时遇到了这个错误:
mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
Does this mean that rc.local is being executed before MySQL has had a chance to be initialized? Where do I go from here?
这是否意味着 rc.local 在 MySQL 有机会被初始化之前就被执行了?我从这里去哪里?
采纳答案by Yves Martin
You should create an init script /etc/init.d/mydaemon
for your daemon from available skeleton.
您应该/etc/init.d/mydaemon
从可用的骨架为您的守护程序创建一个 init 脚本。
Then you will be able to set its startup order so that MySQL is already available.
然后您将能够设置其启动顺序,以便 MySQL 已经可用。
Here is a good starting point.
这是一个很好的起点。
回答by larsks
Python exceptions -- and most other error messages -- go to stderr
, but you're only redirecting stdout
. You should be running your service like this:
Python 异常 - 以及大多数其他错误消息 - 转到stderr
,但您只是重定向stdout
. 你应该像这样运行你的服务:
python /var/www/myDaemon/Main.py > /var/log/somelog.txt 2>&1
The 2>&1
tells the shell to send stderr
output to the same place as stdout
. Do this and post any error messages you see if the problem doesn't end up being obvious.
该2>&1
告诉shell发送stderr
输出到同一个地方stdout
。如果问题最终不明显,请执行此操作并发布您看到的任何错误消息。
回答by codewaggle
From the mysql docs: C.5.2.2. Can't connect to [local] MySQL server
来自 mysql 文档: C.5.2.2。无法连接到 [本地] MySQL 服务器
The error (2002) Can't connect to ... normally means that there is no MySQL server running on the system or that you are using an incorrect Unix socket file name or TCP/IP port number when trying to connect to the server. You should also check that the TCP/IP port you are using has not been blocked by a firewall or port blocking service.
错误 (2002) Can't connect to ... 通常意味着系统上没有运行 MySQL 服务器,或者您在尝试连接到服务器时使用了不正确的 Unix 套接字文件名或 TCP/IP 端口号。您还应该检查您使用的 TCP/IP 端口是否被防火墙或端口阻止服务阻止。
Does the "/var/run/mysqld/mysqld.sock" file exist?
“/var/run/mysqld/mysqld.sock”文件是否存在?
Is the mysql server running on the same server as the python daemon process?
mysql 服务器是否与 python 守护进程运行在同一台服务器上?
Have you looked at the mysql log?
你看过mysql日志吗?
It should be located in /var/log/
它应该位于 /var/log/
Named something like:
mysql.log
mysql.err
mysqld.log
mysqld.err
命名是这样的:
mysql.log
mysql.err
mysqld.log
mysqld.err
$ sudo ls -l /var/log/
// when you have the name try
$ sudo tail -n 50 /var/log/LogName
One possibility is that you're out of disk space, try:
一种可能性是您的磁盘空间不足,请尝试:
$ sudo df -h
Look at the 4th column "Avail"
看第4列“Avail”
Another possibility is the folder permissions, try:
另一种可能是文件夹权限,请尝试:
$ sudo ls -l /var/lib/
You should see something like this:
您应该会看到如下内容:
drwxr-xr-x 33 mysql mysql 4096 May 22 10:02 mysql
You most likely want the owner and group to be "mysql", if they aren't, you could try:
您很可能希望所有者和组是“mysql”,如果不是,您可以尝试:
$ sudo chown -R mysql:mysql /var/lib/mysql
Make a note of the original settings first so you can change it back if it doesn't help. The "-R" means change ownership recursively for the sub folders and files.
首先记下原始设置,以便在没有帮助时将其更改回来。“-R”表示递归更改子文件夹和文件的所有权。
The permissions should be rwxr-xr-x (755), if not you can try:
权限应该是 rwxr-xr-x (755),如果不是,您可以尝试:
$sudo chmod 755 /var/lib/mysql
I wouldn't do that recursively, most of the files and folders should only be accessible to the mysql user, possibly the mysql group and only read/write not executable.
我不会递归地这样做,大多数文件和文件夹应该只能由 mysql 用户访问,可能是 mysql 组,并且只能读/写不可执行。
回答by hsanders
One thing the other people who answered missed is in your rc, init, etc. scripts that are run on startup the PATH variable may or may not be initialized (there's never a guarantee), meaning simplying doing "python somescript.py" instead of /usr/bin/python may not always work. ALWAYS use absolute paths for everything in init scripts and rc scripts.
其他人错过的一件事是在启动时运行的 rc、init 等脚本中,PATH 变量可能会或可能不会被初始化(永远无法保证),这意味着简单地执行“python somescript.py”而不是/usr/bin/python 可能并不总是有效。对于 init 脚本和 rc 脚本中的所有内容,始终使用绝对路径。
And yes, it is likely that rc.local is run before mysqld is started. You need to set this up as an init.d script instead and use insserv-style comments in the header to tell it what dependencies it needs. http://manpages.ubuntu.com/manpages/lucid/man8/insserv.8.html
是的,rc.local 很可能在 mysqld 启动之前运行。您需要将其设置为 init.d 脚本,并在标头中使用 insserv 样式的注释来告诉它需要哪些依赖项。http://manpages.ubuntu.com/manpages/lucid/man8/insserv.8.html
回答by fantaxy025025
my friends. I spend several days for this error. Fortunately I got a solution.
我的朋友。我花了几天时间来解决这个错误。幸运的是我得到了解决方案。
sudo -u www -i /the/path/of/your/script
Please prefer the sudo manual~ -i [command] The -i (simulate initial login) option runs the shell specified by the password database entry of the target user as a loginshell...
请首选sudo手册~ -i [命令] -i(模拟初始登录)选项运行目标用户的密码数据库条目指定的shell作为登录shell...
U can prefer what I post here: Run script with rc.local: script works, but not at boot
你可以更喜欢我在这里发布的内容:使用 rc.local 运行脚本:脚本有效,但在启动时无效
Good luck!
祝你好运!