【发布时间】:2020-05-30 23:36:44
【问题描述】:
我一直在尝试让程序在启动时自动启动。我尝试使用systemd、init.d、rc.local 和cron 来执行我的 bash 脚本以调用程序(例如打开 LXTerminal 的实例),但在启动时仍然看不到它。
systemd:我在/etc/systemd/system/ 中创建了一个服务,遵循this 模板:
[Unit]
Description=My service
After=network.target
[Service]
ExecStart=/usr/bin/python3 -u main.py
WorkingDirectory=/home/pi/myscript
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi
[Install]
WantedBy=multi-user.target
然后我在/etc/systemd/system 中创建了我的 bash 脚本并使其可执行,然后按照官方 Raspberry Pi 文档中的说明运行以下命令:
sudo cp myscript.service /etc/systemd/system/myscript.service
sudo systemctl start myscript.service
sudo systemctl stop myscript.service
sudo systemctl enable myscript.service
我被告知确实创建了一个符号链接,但启动服务后程序没有运行。
init.d:仿照this的例子,我在/etc/initd/下添加了一个init脚本,使脚本可执行,并注册了启动时运行的脚本,没有结果.
rc.local:根据here找到的官方文档,我将我的bash脚本添加到/etc/的rc.local文件中,但没有结果。
cron:根据找到的官方文档here,我运行带有-e 标志的crontab 来编辑cron 表:
crontab -e
然后,我给crontab添加了一个定时任务:
@reboot bash /home/pi/mybashscript.sh &
不幸的是,仍然没有结果。 我希望使用任何方法在 Raspbian Buster 上启动时自动运行程序。如果您有任何完成此操作的经验,或者遇到过同样的问题,请回复。感谢您的帮助!
【问题讨论】:
-
当您从 CLI 运行
crontab命令时会发生什么? -
然后我在 /etc/systemd/system 中创建了我的 bash 脚本 为什么? bash 脚本有什么作用?它与您创建的服务有什么关系?
-
这个问题缺乏重要的证据——即。脚本!
-
@Seamus,crontab 挂了没有结果,唯一的选择似乎是使用 Control+C 键盘中断退出。
-
@JaromandaX,bash 脚本是用几个参数运行我的程序,但我通过将“lxterminal -e bash /home/pi/myScript.sh”添加到我的 lxsession-autostart 来让它工作,根据 Botspot 的建议。我的服务的唯一目的是在启动时自动启动脚本。