将一个py文件设置为开机自启动,服务启动时报错,显示找不到rclpy模块。
直接运行./autotest.sh没有问题,sudo ./autotest.sh报错如下。
1、打开两个终端,分别输入以下命令,如下图所示。
python3
import sys
sys.path
和
sudo python3
import sys
sys.path
2、查看上图打印结果,找到sudo python3那边没有的部分,如上图红框显示。
3、将缺少部分写在XXX.pth文件中,并保存于 /usr/local/lib/python3.10/dist-packages/路径下(路径根据自己实际情况修改,应该可以写在一个pth文件中吧,我也没查,就直接写了两个),如下图所示。
4、此时运行还会缺少一些so文件,报错如下:
Traceback (most recent call last): File “/home/tls/ws/firbot/ui_design/bringup.py”, line 8, in import rclpy File “/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/init.py”, line 49, in from rclpy.signals import install_signal_handlers File “/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/signals.py”, line 15, in from rclpy.exceptions import InvalidHandle File “/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/exceptions.py”, line 15, in from rclpy.impl.implementation_singleton import rclpy_implementation as _rclpy File “/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/impl/implementation_singleton.py”, line 32, in rclpy_implementation = import_c_library(‘._rclpy_pybind11’, package) File “/opt/ros/humble/lib/python3.10/site-packages/rpyutils/import_c_library.py”, line 39, in import_c_library return importlib.import_module(name, package=package) File “/usr/lib/python3.10/importlib/init.py”, line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) ImportError: librcl_action.so: cannot open shared object file: No such file or directory The C extension ‘/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/_rclpy_pybind11.cpython-310-x86_64-linux-gnu.so’ failed to be imported while being present on the system. Please refer to ‘https://docs.ros.org/en/{distro}/Guides/Installation-Troubleshooting.html#import-failing-even-with-library-present-on-the-system’ for possible solutions
解决方法为:找到你所缺文件(如librcl_action.so)路径,创建一下软链接就可以了。
sudo ln -s /opt/ros/humble/lib/lib* /usr/lib
1、开机自启动的autotest.sh文件放在了/etc/init.d/目录下,如下图所示。
2、进入 /etc/systemd/system/ 目录:
cd /etc/systemd/system/
3、创建一个名为 autotest.service 的服务单元文件:
sudo nano autotest.service
4、在打开的文件中,输入以下内容(请根据实际情况进行修改):
[Unit]
Description=Autotest Service
After=network.target
[Service]
User=talos
Group=talos
ExecStart=/etc/init.d/autotest.sh start
[Install]
WantedBy=default.target
上述配置假设你已经将 autotest.sh 脚本文件放在 /etc/init.d/ 目录下。如果脚本文件的位置不同,请相应调整 ExecStart 的路径。记得给权限!!!
5、使用以下命令重新加载 systemd 服务配置。
sudo systemctl daemon-reload
2、都设置完之后可以启动一下看一下是否启动成功,下图是启动成功的状态。
sudo service autotest.sh start 或 sudo systemctl start autotest.service
sudo service autotest.sh status
也可通过命令关闭。
sudo service autotest.sh stop