简体   繁体   English

当 Jetson Nano(Ubuntu 18.04)上的互联网连接可用时,运行 python 脚本

[英]Run python script when internet connection becomes available on Jetson Nano (Ubuntu 18.04)

I need to setup a Jetson Nano device so that a Python script is launched everytime an Internet connection is available.我需要设置一个 Jetson Nano 设备,以便在每次 Internet 连接可用时启动 Python 脚本。

So, referring to this question , I did the following:所以,参考这个问题,我做了以下事情:

  • I created the 'run_when_connection_available' script:我创建了“run_when_connection_available”脚本:
#!/bin/sh

# create a dummy folder to check script execution
mkdir /home /user_name/dummy_folder_00

# kill previous instances of the system
pkill python3

# move to folder with python script and launch it
cd /home/user_name/projects/folder
/usr/bin/python3 launcher.py --arg01 --arg02 ...

# create another dummy folder to check script execution
mkdir /home /user_name/dummy_folder_01

  • I made this script executable and I copied it to /etc/network/if-up.d我使这个脚本可执行并将其复制到 /etc/network/if-up.d

Now, everytime I plug the ethernet cable out and in again, I can see the dummy folders are created in /home/user_name, but the python script isn't launched (at least, it doesn't appear in the system monitor).现在,每次我重新插入以太网电缆时,我都可以看到在 /home/user_name 中创建了虚拟文件夹,但 python 脚本没有启动(至少,它没有出现在系统监视器中)。 I tried running the command in the script from the terminal, and everything works fine, the python program starts as expected.我尝试从终端运行脚本中的命令,一切正常,python 程序按预期启动。 Am I doing something wrong?难道我做错了什么?

I'm trying to figure out something similar to you, but not quite the same..我正在尝试找出与您相似的东西,但不完全相同..

This solution got my script python script running upon internet connection , I can check the logs and everything is working fine:这个解决方案让我的脚本 python 脚本在互联网连接上运行,我可以检查日志,一切正常:

Raspbian - Running A Script After An Internet Connection Is Established Raspbian - 建立 Internet 连接后运行脚本

However, my script uses notify-send to send notifications to my window manager which I can't seem to get working with systemd - the script works when run inside of the user space so I assume it's something to do with systemd and Xorg.但是,我的脚本使用 notify-send 向我的 window 管理器发送通知,我似乎无法使用 systemd - 该脚本在用户空间内运行时有效,所以我认为它与 systemd 和 Xorg 有关。 Hopefully that shouldn't be a problem for you, I hope this solves your issue.希望这对您来说不是问题,我希望这可以解决您的问题。

You shouldn't need a bash script in the middle, I got systemd service to run my python script with chmod u+x <file>.py and putting #!/usr/bin/env python3 at the top of the python file so that it's executable directly under the.service file like so:你不应该在中间需要一个 bash 脚本,我得到了 systemd 服务来运行我的 python 脚本和chmod u+x <file>.py并把#!/usr/bin/env python3放在 Z23EEEB4347BDD25DFCZB 文件的顶部所以它可以直接在 .service 文件下执行,如下所示:

ExecStart=/path/to/file/file.py

Ok, I guess it was a matter of permissions, I solved it by running everything as user_name, so I modified the script as好的,我想这是权限问题,我通过以 user_name 运行所有内容来解决它,所以我将脚本修改为

sudo -user user_name /usr/bin/python3 launcher.py --arg01 --arg02 ...

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

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