简体   繁体   English

从cron或subshel​​l用绝对路径调用Python时不会退出

[英]Python won't exit when called with absolute path from cron or subshell

I have some python scripts that run via cron, and they no longer exit correctly when the script is called with an absolute path. 我有一些通过cron运行的python脚本,当使用绝对路径调用脚本时,它们不再正确退出。 They will hang until the process is terminated. 它们将挂起,直到该过程终止。 I believe it happened after I moved /var and /home to a different partition. 我相信这是在将/ var和/ home移至另一个分区之后发生的。

I checked into the environment variables and couldn't see anything obviously wrong, this happens either when run with cron or a bash subshell, but not when run directly. 我检查了环境变量,没有发现任何明显错误,这是在使用cron或bash子shell运行时发生的,但在直接运行时却没有发生。

If I run it as a subshell it hangs until I kill it (ctrl-c) and then gives me the output. 如果我将其作为子shell运行,它将挂起,直到我将其杀死(ctrl-c),然后将输出显示给我。

[wotstats@rock test]$ echo 'assert 0==1, "fails"' > test.py
[wotstats@rock test]$ /bin/bash -c "/usr/bin/python /var/home/wotstats/test/test.py"
^CTraceback (most recent call last):
  File "/var/home/wotstats/test/test.py", line 1, in <module>
    assert 0==1, "fails"
AssertionError: fails

If I don't call a script it terminates as expected without hanging: 如果我不调用脚本,它将按预期终止而不挂起:

[wotstats@rock test]$ /bin/bash -c "echo 'assert 0==1, \"fails\"' | /usr/bin/python"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError: fails

I do get an immediate log error when the exception occurs: 发生异常时,我确实会立即收到日志错误:

Dec  9 13:33:44 rock abrt: detected unhandled Python exception in '/var/home/wotstats/test/test.py'

I ran some tests based on the input, and found that this problem occurs even when called as /test.py and when run as root. 我根据输入进行了一些测试,发现即使以/test.py调用并以root身份运行时,也会出现此问题。 I also tried /root and /usr with the same effect. 我也尝试了/ root和/ usr具有相同的效果。

Similar or same question was asked here and resolved by reboot; 在这里提出了类似或相同的问题 ,并通过重新启动解决了; I'd rather figure it out and avoid the reboot if I can. 我宁愿找出来,并避免重新启动。

Ok, got it figured out. 好,知道了。 Thanks for the help. 谢谢您的帮助。

I ran an strace and found that it was hanging on the following: 我跑了一条路,发现它挂在下面:

socket(PF_FILE, SOCK_STREAM, 0)         = 4
connect(4, {sa_family=AF_FILE, path="/var/run/abrt/abrt.socket"}, 27^C
<unfinished ...>

That led to Bugzilla which indicated that SELinux was the problem. 导致Bugzilla指出SELinux是问题所在。 I had already changed SELinux to permissive mode (I screwed it up moving /var), but had not restarted abrtd. 我已经将SELinux更改为许可模式(我将其固定在/ var上),但没有重新启动abrtd。

Restarting the abrtd service resolved the problem. 重新启动abrtd服务可解决此问题。

Instead of: 代替:

/usr/bin/python /var/home/wotstats/test/test.py

use: 使用:

cd /var/home/wotstats/test/
/usr/bin/python test.py<br/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM