如果在启动时自动执行,终端中的有效命令将不起作用

如果在启动时自动执行,终端中的有效命令将不起作用

我需要在 Ubuntu 启动时执行一个 Python 程序。该脚本可以从 Visual Studio Code 运行,也可以使用以下命令从终端运行:

bash -c "python3 /home/complete_path/script.py"

尽管如此,如果我使用“启动应用程序”放置相同的代码,代码会运行,但会返回错误。为了了解发生了什么,我尝试了许多其他方法来运行文件,例如修改 local.rc、使用服务或 crontab,但结果总是相同的。我还尝试自动启动终端并在终端启动时执行命令,但结果没有改变。在启动时运行命令或由终端自动运行命令与
手动插入命令有什么区别?我搞不清楚发生了什么。

该系统是安装了 Ubuntu 18.04 的 Jetson nano。

编辑1 将其作为服务运行似乎无法导入 Python 模块

-- Automatic restarting of the unit mything.service has been scheduled, as the result for
-- the configured Restart= setting for the unit.
gen 18 15:56:33 aisports-desktop systemd[1]: Stopped mything: do my own thing.
-- Subject: Unit mything.service has finished shutting down
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
-- 
-- Unit mything.service has finished shutting down.
gen 18 15:56:33 aisports-desktop systemd[1]: Started mything: do my own thing.
-- Subject: Unit mything.service has finished start-up
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
-- 
-- Unit mything.service has finished starting up.
-- 
-- The start-up result is RESULT.
gen 18 15:56:33 aisports-desktop mything.sh[7800]: Traceback (most recent call last):
gen 18 15:56:33 aisports-desktop mything.sh[7800]:   File "/home/aisports/Desktop/AISports/RGB_control_camera.py", line 11, in <module>
gen 18 15:56:33 aisports-desktop mything.sh[7800]:     import depthai as dai
gen 18 15:56:33 aisports-desktop mything.sh[7800]: ModuleNotFoundError: No module named 'depthai'
gen 18 15:56:33 aisports-desktop systemd[1]: mything.service: Main process exited, code=exited, status=1/FAILURE
gen 18 15:56:33 aisports-desktop systemd[1]: mything.service: Failed with result 'exit-code'.

但我仍然找不到 crontab 的日志,因为命令

grep CRON /var/log/syslog

回馈

Binary file /var/log/syslog matches

编辑2 我通过在 python 脚本顶部添加模块的路径解决了缺少模块的问题

import sys
sys.path.append('path_to_module')

现在出现的问题与启动应用程序时出现的问题相同:

gen 18 18:08:31 aisports-desktop systemd[1]: Stopped mything: do my own thing.
-- Subject: Unit mything.service has finished shutting down
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
-- 
-- Unit mything.service has finished shutting down.
gen 18 18:08:31 aisports-desktop systemd[1]: Started mything: do my own thing.
-- Subject: Unit mything.service has finished start-up
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
-- 
-- Unit mything.service has finished starting up.
-- 
-- The start-up result is RESULT.
gen 18 18:08:32 aisports-desktop mything.sh[7763]: [2022-01-18 18:08:32.050] [warning] VideoEncoder setDefaultProfilePreset: passing 'width'/ 'height' is deprecated. The size is auto-determined from first
gen 18 18:08:34 aisports-desktop mything.sh[7763]: Stack trace (most recent call last):
gen 18 18:08:34 aisports-desktop mything.sh[7763]: #2    Object "/lib/ld-linux-aarch64.so.1", at 0x7fb4f57a33, in
gen 18 18:08:34 aisports-desktop mything.sh[7763]: #1    Object "/usr/local/lib/python3.6/dist-packages/numpy/core/../../numpy.libs/libopenblasp-r0-32ff4d91.3.13.so", at 0x7fa055772b, in gotoblas_init
gen 18 18:08:34 aisports-desktop mything.sh[7763]: #0    Object "/usr/local/lib/python3.6/dist-packages/numpy/core/../../numpy.libs/libopenblasp-r0-32ff4d91.3.13.so", at 0x7fa06d4f54, in gotoblas_dynamic_
gen 18 18:08:34 aisports-desktop mything.sh[7763]: Illegal instruction (Illegal opcode [0x7fa06d4f54])
gen 18 18:08:35 aisports-desktop mything.sh[7763]: /usr/local/bin/mything.sh: line 2:  7779 Illegal instruction     (core dumped) python3 /home/aisports/Desktop/AISports/RGB_control_camera.py
gen 18 18:08:35 aisports-desktop systemd[1]: mything.service: Main process exited, code=exited, status=132/n/a
gen 18 18:08:35 aisports-desktop systemd[1]: mything.service: Failed with result 'exit-code'.

編輯 3 (最後) 我终于解决了这个问题。在 .bashrc 文件中,我根据处理器的类型添加了export OPENBLAS_CORETYPE=ARMV8该行。我以为这对于启动命令也足够了,但事实并非如此。在调用 python 脚本之前,将该行添加到启动脚本中解决了这个问题。感谢 @FelixJN 对日志文件的支持,这对我帮助很大。

相关内容