Score:1

working command in terminal doesn't work if automatically executed at startup

uy flag

I need to execute a python program at Ubuntu startup. The script works either run from visual studio code, or from the terminal with the command:

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

Nevertheless, if I put the same code using the "startup applications", the code is run but it gives back an error. In order to understand what's going on, I tried many other methods in order to run the file such as modifying local.rc use service or crontab but the result is always the same. I tried also to automatically launch the terminal and execute the command at terminal startup but the result is not changed. Which kind of differences are there in a command run at startup or automatically by a terminal vs
manually inserting them? I cannot figure out what's going on.

The system is a Jetson nano with Ubuntu 18.04 installed.

EDIT 1 running it as a service it seems that it cannot import a python module

-- 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'.

while I still cannot find the log of crontab, since the command

grep CRON /var/log/syslog

gives back

Binary file /var/log/syslog matches

EDIT 2 I solved the missing module problem adding the path of that module at top of the python script through

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

now the problem that occurs is the same as in case of startup applications:

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'.

EDIT 3 (last) I finally solved the problem. in .bashrc file I had added export OPENBLAS_CORETYPE=ARMV8 because of the type of the processor. I though that was sufficient also for the startup commands, instead it was not. Adding that line to the startup script, before calling the python script solved the problem. Thank you @FelixJN for the support on having the log files, that helped me a lot.

FelixJN avatar
im flag
Does the script rely on some environment? E.g. display (does it start some GUI?), network connection, etc.? What did `systemd` or `cron` report in the logs when failing?
tode09 avatar
uy flag
the script basically acquire a video from a usb camera, save it in h265, do a conversion in mp4 compressing it through gstreamer ando so on. No GUI and no display. The only thing that is called outside is the gstreamer conversion through pexpect.
FelixJN avatar
im flag
And `python3` is installed where? Might this just be a `PATH` problem? Please add the error messages for your `CRON` and `systemd` implementations.
tode09 avatar
uy flag
I'm sorry but i don't know where to find those logs. The error that appear in syslog file when launched with startup application is the following: #2 Object "/lib/ld-linux-aarch64.so.1", at 0x7fad741a33, in #1 Object "/usr/local/lib/python3.6/dist-packages/numpy/core/../../numpy.libs/libopenblasp-r0-32ff4d91.3.13.so", at 0x7fa14da72b, in gotoblas_init #0 Object "/usr/local/lib/python3.6/dist-packages/numpy/core/../../numpy.libs/libopenblasp-r0-32ff4d91.3.13.so", at 0x7fa1657f54, in gotoblas_dynamic_init Illegal instruction (Illegal opcode [0x7fa1657f54]) Illegal instruction
FelixJN avatar
im flag
Please do not add this as comment - [edit](https://askubuntu.com/posts/1387746/edit) your question and add the info (with proper code formatting). `CRON` messages can be found in `syslog` (`grep CRON /var/log/syslog`), `systemd` info via `journalctl -xeu <servicename>` (add `--user` if it is a user service).
tode09 avatar
uy flag
ok, I will do it in few minutes. I found out that the same problem appears also if the code is launched by terminal with sudo. I don't know if it helps
tode09 avatar
uy flag
I edited the question, but still no idea on what is going on
waltinator avatar
it flag
It's a question of "execution context". Your terminal execution of the script runs as you (` echo "$USER"`) has the benefit of your startup files, `PATH` and other environment variables for Python. The system startup process runs as `root`, doesn't use your startup files, has its own `PATH`, and none of your environment variables. Run `echo "=== id ===";id;echo "=== set ===";set;echo "=== env ===";env | sort;echo "=== alias ===";alias` in each of your environments.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.